OOPS CONCEPT
Overriding in JAVA
Every parent works hard because have to look after their child. so they earn money and assets.
now observe, they give property like cash and land and gold. most of the parents arrange a marriage for their kids. here parents defined two methods one is the property and another one is married. let's move to write code.
class P{
public void property()
{
System.out.println("cash$ +Land + Gold" );
}
public void marry ()
{
System.out.println("Mahalaxmi");
}
}
whatever method parent class has, by default available in child class through inheritance here, we are very satisfied with the property implementation but not satisfied with the marry implementation. then the child is allowed to redefine the method based on its requirements.
class C extends P {
public void marry ()
{
System.out.println("anupama / pranitha");
}
}
so child class is not satisfied with parent class implementation. then the child is allowed to redefine the method based on its requirements. this process is called Overriding.
the parent class method which is overridden is called overridden method. and the child class method which is overriding is called the overriding method.
class Test
{
public static void main (String args [])
{
P p = new P ();
p.marry(); // Parent method
C c = new C();
c.marry(); // Child method
P p1= new C();
p1.marry(); // child method
}
}
In overriding method resolution always tasks care by JVM based on runtime object and hence overriding is also considered as runtime polymorphism or dynamic polymorphism or late binding
0 Comments