package org.helpimport java.util.Integer //same package then not neededclass A { Integer x;}A obj = new A();
obj here is called reference variable pointing to memory reference similar to pointer in C++
new A() is the actual object created
new keyword creates the instance aka java allocates memory with this
A() the constructor of class being called to initialize the object
Static
In a class there would be 2 types of members one is instance members and other is static
Instance members belongs to the object while static members belong to the class itself
This means multiple objects created but they all share the same static member
To access instance member = obj.x while static member = A.x
Real life example would be Utility class can have static method for factorial etc. if it was not static then user would have to create object to access it every time which is costly
Final
final keyword means it can only be initialized once
combining static and final means the same member is available to all instance and is fixed
In real life scenario it could be some kind configuration given to the variable which needs to be shared among all instances and it cannot be changes
Wrapper class
Wrapper classes are used to wrap primitive data types into objects
Using Integer instead of int will give you extra methods that belong to Integer class
Inheritance
we use extends keyword to perform inheritance
Multiple parent class (super class) not possible aka no multi inheritance
Multiple child classes (sub class) possible aka hierarchical inheritance and multilevel also possible
Initialization block
public class Example { private int value; // Instance initialization block // as name suggests for initialization or setup { value = 10; System.out.println("Instance Initialization Block"); } public Example() { System.out.println("Constructor"); } public static void main(String[] args) { Example example = new Example(); System.out.println("Value: " + example.value); }}Instance Initialization BlockConstructorValue: 10
Polymorphism
Occurring in different forms
Method overloading - Compile time
Method overloading - Run time
// Overloadingpublic class A { public void overload(int a) { System.out.println("class A"); }}public class B extends A { public void overload(int a, int b) { System.out.println("class A"); }}int a= 10, b = 20;B obj = new B();B.overload(a);B.overload(a,b);//Overridingpublic class A { public void overload(int a) { System.out.println("class A"); }}public class B extends A { public void overload(int a) { System.out.println("class A"); }}int a= 10;B obj = new B();B.overload(a); //class B method overrid class A method
If same name same no of args and same args then not allowed inside same class
this, super and encapsulation
this would refer to current instance of the class while super to the superclass of current object
public class A { private int a; //field itself is private public int setA(int a) { this.a = a; //current instance assigned value from arg } public void help() {}}public class B { public void help() { super.help(); // will call superclass method }}
Encapsulation in Java refers to integrating data (variables) and code (methods) into a single unit.
Using getter and setter instead of directly accessing is one example
How it helps is first we can add validations or other logic in the getter and setter itself secondly we are not exposing the field itself
If we access directly then in future if we want to add some validation or other logic this would require change everywhere while with getter and setter we just change inside them and others are unchanged