DEV Community

Akash
Akash

Posted on

Method Overriding in java

  • Method overriding is also called as Runtime Polymorphism
  • child class provides its own implementation of a method that already exists in the parent class. *
=> Parent Class
public class Parent
{

public static void main(String[]args)
{

Parent Parent=new Parent();
Parent.fixmar();

}

public void fixmar(){

System.out.println("We chose perfect partner for you  ");

}

}
=> Child Class
public class Child extends Parent
{

public static void main(String[] args)
{

Child Child=new Child();
Child.fixmar();
Parent Parent=new Parent();
Parent.fixmar();
}

public void fixmar()
{

System.out.println(" I have my opinion");
}
}

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

*=> Rules for Method Overriding
*

  • child class must inherit from the parent class using (extends)
  • Method name must be the same
  • Parameter must be the same
  • Return type must be the same
  • Access should not be Reduced
  • Method Overriding requires inheritance (extends)

Top comments (0)