Java OOPS - few points to remember
Definitions, Tips & Rules related to Java OOPS
Class & Constructor
- A class in Java can contain: fields, methods, constructors, blocks, nested class and interface.
- Object is an instance of a class.
- Constructor in java is a special type of method that is used to initialize the object.
- If there is no constructor in a class, compiler automatically creates a default constructor.
- There is no copy constructor in java. But, we can copy the values of one object to another like copy constructor in C++.
- A constructor can perform other tasks instead of initialization like object creation, starting a thread, calling method etc.
- You can perform any operation in the constructor as you perform in the method.
static Keyword
- The static can be: variable (class variable), method (class method), block & nested class.
- Java static property is shared to all objects.
- A static method belongs to the class rather than object of a class.
- A static method can be invoked without the need for creating an instance of a class.
- A static method can access static data member and can change the value of it.
- The static method can not use non static data member or call non-static method directly.
- this and super cannot be used in static context.
- The main method is static because object is not required to call static method if it were non-static method, jvm create object first then call main() method that will lead the problem of extra memory allocation.
- A static block is used to initialize the static data member. It is executed before main method at the time of classloading.
this Keyword
- In Java, this is a reference variable that refers to the current object.
- Call to this() must be the first statement in constructor.
- this can be used to: refer current class instance variable, invoke current class method and constructor.
- this canpassed as an argument in the method and constructor call.
- this can be used to return the current class instance from the method.
- It is better approach to use meaningful names for variables. So we use same name for instance variables and parameters in real time, and always use this keyword.
Inheritance 继承
- Inheritance (IS-A) is a mechanism in which one object acquires all the properties and behaviors of parent object.
- The extends keyword indicates that you are making a new class that derives from an existing class.
- Multiple inheritance is not supported in Java through class. We can use Interface to perform it.
- To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
- If a class have an entity reference, it is known as Aggregation (HAS-A relationship).
- Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.
Inheritance is one of the key features of Object Oriented Programming. Inheritance provided mechanism that allowed a class to inherit property of another class. When a Class extends another class it inherits all non-private members including fields and methods. Inheritance in Java can be best understood in terms of Parent and Child relationship, also known as Super class(Parent) and Sub class(child) in Java language.
Inheritance defines is-a relationship between a Super class and its Sub class. extends and implements keywords are used to describe inheritance in Java.
class Vehicle
{
......
}
class Car extends Vehicle
{
....... //extends the property of vehicle class
}
Now based on above example. In OOPs term we can say that,
- Vehicle is super class of Car.
- Car is sub class of Vehicle.
- Car IS-A Vehicle.
Method Overloading 重载
- If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
- There are two ways to overload the method in java : by changing number of arguments, by changing the data type.
- In Java, Method Overloading is not possible by changing the return type of the method only because of ambiguity.
- Compile Time Error is better than Run Time Error. So, java compiler renders compiler time error if you declare the same method having same parameters.
- We can also overload Java main() method, but JVM calls main() method which receives string array as arguments only.
- One type is promoted to another implicitly if no matching datatype is found. eg. byte can be promoted to short, int, etc.
- If there are no matching type arguments method, and each method promotes similar number of arguments, there will be ambiguity.
- One type is not de-promoted implicitly for example double cannot be depromoted to any type implicitly.
Method overloading by changing data type of arguments.
class Calculate
{
void sum (int a, int b)
{
System.out.println("sum is"+(a+b)) ;
}
void sum (float a, float b)
{
System.out.println("sum is"+(a+b));
}
Public static void main (String[] args)
{
Calculate cal = new Calculate();
cal.sum (8,5); //sum(int a, int b) is method is called.
cal.sum (4.6f, 3.8f); //sum(float a, float b) is called.
}
}
Sum is 13
Sum is 8.4
Method overloading by changing number of argument.
class Demo
{
void multiply(int l, int b)
{
System.out.println("Result is"+(l*b)) ;
}
void multiply(int l, int b,int h)
{
System.out.println("Result is"+(l*b*h));
}
public static void main(String[] args)
{
Demo ar = new Demo();
ar.multiply(8,5); //multiply(int l, int b) is method is called
ar.multiply(4,6,2); //multiply(int l, int b,int h) is called
}
}
Result is 40
Result is 48
Method Overriding 重写
- If subclass (child class) has the same method as declared in the parent class, it is known as method overriding.
- Method must have same name and parameters as in the parent class for overriding.
- Method overriding is used to provide specific implementation of a method that is already provided by its super class. Also used for runtime polymorphism.
- We cannot override static method (not also main method) because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.
- Method Overriding with Access Modifier: if you are overriding a method, overridden method (i.e. declared in subclass) must not be more restrictive.
- Covariant Return Type: It is possible to override method by changing the return type if subclass overrides any method whose return type is Non-Primitive but it changes its return type to subclass type.
Example of Method Overriding
class Animal
{
public void eat()
{
System.out.println("Eat all eatables");
}
}
class Dog extends Animal
{
public void eat() //eat() method overridden by Dog class.
{
System.out.println("Dog like to eat meat");
}
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
}
}
Dog like to eat meat
super Keyword
- The super keyword is a reference variable which is used to refer immediate parent class object.
- super can be used to refer immediate parent class instance variable or invoke immediate parent class method and constructor.
- super() is added in each class constructor automatically by compiler if there is no super() or this().
Instance initializer block
- Instance Initializer block is used to initialize the instance data member.
- It is created when instance of the class is created.
- It runs each time when object of the class is created.
- It is invoked after the parent class constructor is invoked (i.e. after super() constructor call).
- The instance Initializer block comes in the order in which they appear.
final Keyword
- The final keyword in java is used to restrict the user.
- You cannot change the value of final variable(It will be constant).
- If you make any as , You cannot override a final method.
- If you make any class as final, you cannot extend it.
- Final method is inherited but you cannot override it.
- A final variable that is not initialized at the time of declaration is known as blank final variable.
- We can initialize a blank final variable only in constructor.
- A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.
- If you declare any parameter as final, you cannot change the value of it.
- A constructor cannot be declared final because it is never inherited.
Runtime Polymorphism
- Polymorphism is a concept by which we can perform a single action by different ways.
- There are two types of polymorphism in java: compile time polymorphism and runtime polymorphism.
- We can perform polymorphism in java by method overloading and method overriding.
- If you overload static method in java, it is the example of compile time polymorphism.
- In Runtime polymorphism (Dynamic Method Dispatch), an overridden method is resolved at runtime rather than compile-time.
- A Virtual Method is an inheritable and overridable method for which dynamic dispatch is facilitated.
- All non-static, non-final and non-private methods are Virtual Methods by default.
- When reference variable of Parent class refers to the object of Child class, it is known as upcasting.
- Method is overridden not the datamembers, so runtime polymorphism can't be achieved by data members.
- Connecting a method call to the method body is known as binding.
- There are two types of binding : Static binding (early binding) and Dynamic binding (late binding).
instanceof Keyword
- instanceof operator is used to test whether the object is an instance of the specified type (class/subclass/interface).
- When Subclass type refers to the object of Parent class, it is known as downcasting.
- If we perform downcasting directly, there is compile error.
- If we perform downcasting by typecasting, ClassCastException is thrown at runtime.
- If we use instanceof operator, downcasting is possible!
Abstract Class 抽象类
- A class that is declared as abstract (keyword) is abstract class. It can have abstract and non-abstract methods.
- Abstraction is a process of hiding the implementation details and showing only functionality to the user.
- There are two ways to achieve abstraction in java : Abstract class (0 to 100%) and Interface (100%).
- A method that is declared as abstract and does not have implementation is abstract method.
- Any method with a body is non-abstract method.
- An abstract class can have data member, abstract method, method body, constructor and even main() method.
- If there is any abstract method in a class, that class must be abstract.
- If extending any abstract class that have abstract method, we must either provide the implementation of the method or make this class abstract.
- Abstract class can also be used to provide some implementation of an interface. Then, the end user extending thtis abstract class is free to skip implementing that method while overriding all the methods of the interface.
Interface
- An interface in java is a blueprint of a class. It has static constants and abstract methods.
- Since Java 8, we can have method body in interface. But we need to make it default or static method.
- The interface is a mechanism to achieve abstraction. It represents IS-A relationship.
- By using interface, we can support multiple inheritance.
- It can be also used to achieve loose coupling (coupling is degree of direct knowledge that one element has of another).
- The Java compiler adds public & abstract before the interface method. Adds public, static & final before data members.
- A class extends another class, an interface extends another interface but a class implements an interface.
- Multiple inheritance is not supported by class because of ambiguity. But, supported by interface because there is no ambiguity as implementation is provided by the implementation class.
- An interface with no member is called marker/tagged interface. For example: Serializable, Cloneable, Remote etc.
- Marker interface are used to provide essential information to JVM, so that JVM may perform some useful operation.
- An interface can have another interface i.e. known as nested interface.
Package
- A java package is a group of similar types of classes, interfaces and sub-packages.
- Java package is used to categorize the classes and interfaces, provides access protection and removes naming collision.
- The package keyword is used to create a package. Package inside the package is called the subpackage.
- If you import a package (package. ), subpackages will not be imported.
- To import subpackage, use import package.classname.
- Use fully qualified name to access only the declared class of a package.
- Sequence of the program must be package then import then class.
- The standard of defining package is domain.company.package. eg - com.oracle.database
- There can be only one public class in a java source file and it must be saved by the public class name.
Access Modifiers
- There are two types of modifiers in java: access modifiers and non-access modifiers.
- There are 4 types of java access modifiers: private, default, protected & public.
- There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc.
- The private access modifier is accessible only within class.
- If you make any class constructor private, you cannot create the instance of that class from outside the class.
- If we don't use any modifier, it is treated as default. Default modifier is accessible only within package.
- A Class cannot be private or protected except nested class.
- The protected access modifier is accessible within package and outside the package but through inheritance only.
- The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.
- If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive.
Encapsulation 封装
- Encapsulation is a process of wrapping code and data together into a single unit.
- To create a fully encapsulated class, make all data members of the class private, & use setter/getter methods to access data.
- By providing only setter or getter method, you can make the class read-only or write-only.
Miscellaneous
- The Object class is the parent class of all the classes in java by default.
- The Cloneable interface must be implemented by the class if we want to create a clone of an object.
- Wrapper class is used to convert primitive into object and object into primitive.
- Autoboxing and unboxing feature converts primitive into object and object into primitive automatically.
- There is only call by value in java, not call by reference.
- A method in java that calls itself is called recursive method.