Please enable JavaScript.
Coggle requires JavaScript to display documents.
OOP Java (1) What are the main features of the Java Programming Language?,…
OOP Java
1) What are the main features of the Java Programming Language?
1.1) Cross platform: "Write once, run anywhere" (WORA)
Java files are complied to .class files and packaged as .jar files which
can be run on any device equipped with a Java Virtual Machine (JVM).
1.2) Object Oriented: Supports OOP concepts
Objects, classes, inheritance, polymorphism, abstraction, encapsulation...
new, class, extends / implements,
Override
/ Overloading,
abstract class / interface, access modifiers (private, public...)
1.3) Automatic memory management: Garbage collection
The main objective of Garbage Collector is to free heap memory by
deleting unreachable, unreferenced, objects.
There are other features; I have just picked the ones I find most important.
3) What is the difference between an instance and a class variable?
3.1) An instance varaible belongs to a particular instance of a class.
e.g. public class ClassName { public int instanceInteger; }
3.2) A class variable belongs to a class itself and is shared by all instances.
e.g. public class ClassName { public static int classInteger; }
3.3) Both are defined at the class level, not method level.
The difference is the scope. One is class level scope, the other is instance level scope.
6) What are the access modifiers in Java?
6.1) Private: access to the class level.
6.2) Public: permits acesss to all levels.
6.3) Protected: access up to the different package, same subclass level.
6.4) Default: access up to package level.
2) What is the importance of the main() method in Java?
2.1) The main method is the starting point for a Java program.
The main method acts like a 'control center'; Ideally, it contains minimal
program logic and simply calls methods defined in other classes.
4) Is Java pass by value or pass by reference?
4.1) Java is pass by value but we access those values via references... See example:
public class PassByValueDemo {
public static void main(String[] args) {
// Creates new value and points the reference 'myObject' to it.
MyObject myObject = new MyObject();
// See 'Change' class.
Change.name(myObject);
System.out.println(myObject.name); // >>> Original
}
}
public class MyObject {
public String name = "Original";
}
public class Change {
public static void name(MyObject obj) {
// Points the reference 'obj' to a new value, doesn't overwrite the old value.
obj = new MyObject();
// Changes new value... Is immediately garbage collected.
obj.name = "Changed";
}
}
5) What is the final Keyword used for in Java?
5.1) Ensures that the value can never be changed after being initialised.
Useful for mathematical constants like pi.
7) What is a static import in Java?
7.1) A static import imports public static field and methods from a class
so they can be used without specifying the class they were defined in.
e.g import static java.lang.Math.* ----> sout(Math.PI) >>> 3.141...