Without inheritance we can’t achieve method overriding. Without method overriding we can’t achieve polymorphism. So Without inheritance we can’t achieve Polymorphism.

Method overriding Rules :

Step 1 : Method name should be same as that of super class method name.

Step 2 : Return type should be same as super class method incase of
primitive . We can go for same type (OR)sub class type in case of Derived.

Step 3 : Method signature should be same.

Step 4 : Access level should be same as that of super class method. Access level
(OR) wider than that, should not (the private) be narrow.

Step 5 : Definition should be different (OR) Body of the method should be Changed(OR) different.

Step 6 : In case of throws,super class may/may not be available while overriding. If we keep throws in sub class it should have same type of super class throws exception type (OR) sub class to that exception type and also order must be same.

Output:

int-A m2-A m4-A int-B m2-B m3-B m4-B
int-B m2-B m4-B

Explanation :

The above program have different methods like m1( ),m2( ), m3( ), m4( ),which acts differently based on their calling mechanism.
if m1( ) is called by using a1 object then it acts based on the behaviors of class A, if we call m1( ) by using b1 it acts
based on the behaviors of class B .And the same manner is also applicable for the m2( ), m3( ), m4( ) methods .

About Java Padho

Einstein said, "It is the supreme art of the teacher to awaken joy in creative expression and knowledge." Great teachers are scarce, as they hold nothing back and give everything away. Javapadho is the brainchild of such an extraordinary teacher, i.e. Mr. Praveen Kumar Chandaliya, the founder of SDJ Infosoft.