Method Overriding
In
inheritance when we declare same method in
Parent class
as well as in child class is called as method
overriding
suppose we
declare method
void
display(); in super class A
and take new
class B extends class A
and declare
the same
method void
display();in child class B
Then same
method we declare in class B
Override the
method declare in class A
Example
class A
{
void
display() // method declaration
{
System.out.println("abc");
}
}
class B
extends A
{
void
display() // again we declare same method
{
System.out.println("xyz");
}
}
class
example
{
public
static void main(String [] args)
{
B obj=new
B();
obj.display();
}
}
What is
actually method overriding
Here in
above program there are two similar
Methods
void
display();
so what
output we get from this program
method in
child class override the method in parent
class
hence we
get output xyz
Its called
as method overriding
No comments:
Post a Comment