.


Divya

Java method overriding


Parent class method by default available to child class method through inheritance some times child class is may not satisfy with parent class method .Then his child all to redefine that method based on its requirement this process is called overriding.

Parent class method which is over ridden is called overridden method.

Child class method which is overriding is called overriding method.

Syntax of Method Overriding

class className1
{
// methods
return type1 method Name(cparam1)
{
// body of method
}
}
class class Name2 extends className1
{
// methods
return type1 method Name(cparam1)
{
// body of method
}
}

Example

class Teacher
{
void show() { System.out.println("Teacher is teaching to all student"); }
}
// Inherited class
class Student extends Teacher
{
@Override
void show() { System.out.println("Student is reading"); }
}
class Main
{
public static void main(String[] args)
{
Teacher obj1 = new Teacher();
obj1.show();
Teacher obj2 = new Student();
obj2.show();
}
}

Output :
Teacher is teaching to all student
Student is reading



.