Without inheritance we cant achieve overriding.In overriding we can give same datatype for two different class. In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.In overriding method and return type should be same.
Reference id:
Program:
superclass:
class woodenchair{
String color=”blue”;
void seating(){
System.out.println(“wood”);
}
public static void main(String args[]){
woodenchair w=new woodenchair();
System.out.println(w.color);
}
}
subclass:
class cusion extends woodenchair{
String color="white";
//int color=2;
@Override
void seating(){
System.out.println("sponge");
}
public static void main(String args[]){
cusion c=new cusion();
c.seating();
}
}
Output:
sponge