Java Programming
Object & Class
Unified Modeling Language (UML) notation


Reference Variables and Reference Types and Instant Variables
Variables: Primitive Type and Reference Type
Static & Nonstatic
Visibility Modifier & Data field Encapsulation(private)

Immutable & mutable
String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String.
- All data fields must be private.
- There can’t be any mutator methods for data fields.
- No accessor methods can return a reference to a data field that is mutable.
Class Abstraction & Encapsulation
Four Class Relationship
- Association: general binary relationship that describes an activity between two classes. eg. (Student <--> Course <--> Faculty)
Aggregation: an ownership relationship between two objects. eg. (Student <-- Aggregation --> Address)
Composition: exclusively owned by an aggregating object. eg.(Student<-- Composition --> Name)
Inheritance
Wrapper Class(Method Requires) & Primitive Type(Performance)
Boxing & Unboxing:
Integer intObject = newInteger (2); // boxing
Integer intObject = 2; // auto boxing
Double.MIN_VALUE = 1.4E–45;
Double.MAX_VALUE = 1.79769313486231570e+308d
Inheritance
Superclass & Subclass
- subclass
extendssuperclass privatedata fields is superclass is accessible by outside classprotected- Java not allowed Multiple inheritance
- More information is subclass
Constructor Chaining
super()invokes the no-arg constructor of its superclass- In any case, constructing an instance of a class invokes the constructors of all the superclasses along the inheritance chain
Overriding vs Overloading
Overloading
- occurs when two or more methods in one class have the same method name but different parameters.
- An be either in the same class or different classes related by inheritance.
- the same name but a different parameter list.
Overriding: (Polymorphism)
@override- having two methods with the same method name and parameters. one in parent class, one in subclass. child class has specific implements.
- are in different classes related by inheritance
- have the same signature and return type
Object & toString()
- Every class in Java is
extendedfrom thejava.lang.Objectclass. - we should
@override public String toString()otherwise output address of Object System.out.print(Object) == System.out.print(Object.toString())- Output Array by one line
System.out.print(Arrays.asList(array).toString());
Polymorphism
Polymorphism means that a variable of a supertype can refer to a subtype object
Declared Type & Actual Type
List<Integer> list = new ArrayList<>();
Method Matching & Dynamic Binding
Declared type decide matching by parameter type, number, order in Compile time
The JVM dynamically binds the implementation of the method at Runtime, decided by the actual type of the variable
Implicit casting & Explicit casting
Student b = (Student)o; // explicit
Object o = new Student(); // implicit
Upcasting & instanceof
Upcasting : It is always possible to cast an instance of a subclass to a variable of a superclass
Instance of: The declared type decides which method to match at compile time
Object myObject = new Circle();
...
// Some lines of code
/** Perform casting if myObject is an instance of Circle */
if (myObject instanceof Circle) {
System.out.println("The circle diameter is " + ((Circle)myObject).getDiameter());
...
}
Abstract and Interface
| abstract Class | Interface(Comparable & Cloneable) | |
|---|---|---|
| when to use | share code among several closely related classes | specify the behavior of particular data type |
| constructor | protected, only used by subclass | no constructor(cannot be instantiated) |
| keywords | extend | implement |
| Variables | may contains non-final variables | implicitly all is public static final |
| Implementation | can implement Interface | cannot implement abstract class |
| Multiple implementation and extend | implements multiple interface | extend multiple interfaces |