Hi All,
In previous post , we checked that how overriding works , between parent and child classes. Now in this post we will take a close look on inheritance rules and cases .
public class InheritanceDemo1 {
public static void main(String [] args){
Child child = new Child();
child.m1();
}
}
class Parent {
public Object m1(){
System.out.println("This is parent class m1 method");
return null;
}
}
class Child extends Parent {
public String m1(){
System.out.println("This is child class m1 method");
return null;
}
}
public class InheritanceDemo1 {
public static void main(String [] args){
Child child = new Child();
child.m1();
}
}
class Parent {
public Object m1(){
System.out.println("This is parent class m1 method");
return null;
}
}
class Child extends Parent {
public String m1(){
System.out.println("This is child class m1 method");
return null;
}
}
In previous post , we checked that how overriding works , between parent and child classes. Now in this post we will take a close look on inheritance rules and cases .
Case -I
From 1.5 version it is not important that return type of Parent class method and child class overridden method must be same , but its co-variant can also be the return types. So return types child class also allowed. For better understanding let's take a look:
public static void main(String [] args){
Child child = new Child();
child.m1();
}
}
class Parent {
public Object m1(){
System.out.println("This is parent class m1 method");
return null;
}
}
class Child extends Parent {
public String m1(){
System.out.println("This is child class m1 method");
return null;
}
}
Output will be :
This is child class m1 method
Explanation:
In this example we can see that the return type of m1 () of Parent class and m1() of Child class , both are not same . But return type of m1 of child class is co-varient of return type of m1() of parent class.As String is that sub class of Object . So it will be compile happily from 1.5 version. Before 1.5 version this concept is not supported.
Case -II
The return type of child class method can be co-variant of return type of parent class method. But , return type of child class method must be sub class of return type of parent class method,if we try it's VICE VERSA it will not compile. For Ex: we all know that
Object is Parent class and String is its Child Class, so Parent class method return type should be Object and Child class method return type can be any subclass of Object super class (in our case I have used String). But if we use String as a return type of Parent class and Object as a return type of Child class , it will show compile time error. Let's take a look with below code:
public static void main(String [] args){
Child child = new Child();
child.m1();
}
}
class Parent {
public Object m1(){
System.out.println("This is parent class m1 method");
return null;
}
}
class Child extends Parent {
public String m1(){
System.out.println("This is child class m1 method");
return null;
}
}
Output will be :
error: m1() in Child cannot override m1() in Parent
return type Object is not compatible with String
No comments:
Post a Comment