examples of override in java


here you can find examples of overrides in java

What is meant by Method Overriding?

Answer: Method overriding happens if the sub-class method satisfies the below conditions with the Super-class method:

  • Method name should be the same
  • The argument should be the same
  • Return type should also be the same

The key benefit of overriding is that the Sub-class can provide some specific information about that sub-class type than the super-class.


public class Manipulation{ //Super class
public void add(){
Public class Addition extends Manipulation(){
Public void add(){
Public static void main(String args[]){
Manipulation addition = new Addition(); //Polimorphism is applied
addition.add(); // It calls the Sub class add() method

addition.add() method calls the add() method in the Sub-class and not the parent class. So it overrides the Super-class method and is known as Method Overriding.

All Contributions

Overrides example in java:


Overrides example in java
class Parent{
  public void method(int i) 
  { System.out.println("parent:"+i);}

class Child extends Parent {   
  public void method(int i) 
  { System.out.println("child:"+i*2);}
public class Main
	public static void main(String[] args) {
	    Parent k = new Child(); //polymorphism
	  Parent p = new Parent();
	  Child c = new Child();


total contributions (1)

New to examplegeek.com?

Join us