Oop Java

Method overriding in Java programming language

Method overriding in Java programming language

We already (Earlier tutorial) had a look at as method overloading in Java. In this tutorial, we will learn about Method overriding in Java programming language.

When the derived(sub class) class has a method in the same name as the parent class (super class), it is called method overriding.

Method overriding is an object-oriented programming technique, which is similar to method overloading.

Conditions for Java method overriding

  • Method must have the same signature as the Base class.
  • Method must have the same parameter as the Base class.
  • Must have an IS-A relationship with extends(Inheritance).

Explanation of method overriding in Java

A class with some methods

 

Another class with the same methods

Relationships between both classes

The relationship between both classes

Examples

program 1

class school1{  //parent class
    void teacher()
        {
    System.out.println("This is my teacher");
        }

}
class tuition1 extends school1{   //child class
        public static void main (String args[]){
        tuition1 t=new tuition1();   //create an instance of child class
        t.teacher();
        
        
        }

}

 

When the above codes compiled and executed, it produces the following results:

This is my teacher

 

Above program shows a simple inheritance program. It contains two classes:  school is the parent class and tuition is the child class. Tuition class inherits the property of School class.

 

Example for method overloading

Program 2

class school{    //parent class
    void education()
        {
    System.out.println("morning education center");;
        }

}
class tuition extends school{  //child class inherits properties of parent class
    void education()
        {
    System.out.println("evening  education center");
        }
        public static void main (String args[]){
        tuition t=new tuition();  //create an instance of chile class
        t.education();
        
        
        }

}

 

When the above codes are compiled and executed, it produces the following results:

Evening education center

 

Above program is a single inheritance program. It contains two classes:  school is the parent class and tuition is the child class. Every class has own methods. Tuition class inherits the property of school class.

An object is created inside the main method in the tuition class. When the method is called through the object, derived class overrides the base class.

Program 3

class Vehicle_1{
    void car(){
    System.out.println("Car is a family vehicle");
            }
    void van(){
    System.out.println("van is a transport vehicle");
            }
            }
            
class Vehicle_2 extends Vehicle_1{
    void car(){
    System.out.println("Car is a costly vehicle");
            }
    void van(){
    System.out.println("van has many seats inside");
            }
    public static void main(String args[]){
    Vehicle_2 V=new Vehicle_2();
    V.car();
    V.van();
    }
}

 

When the above code is compiled and executed, it produces the following results:

car is a costly vehicle
van has many seats inside

 

Program 4

class parent_class{
int b;
parent_class(int b){
this.b=b+10;
}
public void display(){//display  method inside the parent class
System.out.println("print b value is:"+b);
}
}
class child_class extends parent_class{//child class
int a;
child_class(int a,int b){
super(b);

this.a=a;
}
public void display(){//display method inside the child class
System.out.println("print b value is:"+b);
System.out.println("print a value is:"+a);
}
}
class override{
public static void main(String args[]){
child_class C=new child_class(34,56);

C.display();
}
}

 

When the above code compiled and executed, it produced the following results

print b value is:66
print a value is:34

 

 

The explanation for the above program:

class parent_class{// base class
int b;//assign a global variable int a to base class
parent_class(int b){//passing a parameter int b
this.b=b+10;//assign local variable to global variable
}//using this key word
public void display(){//method for display (1)
System.out.println("print b value is:"+b);
}
}
class child_class extends parent_class{//child class
int a;// assign a global variable for child class
child_class(int a,int b){//passing parameter int a ,int b
super(b);//assign b to child from the immediate parent 
//using super kerword
this.a=a;//assign local variable to global variable
}//using this key word
public void display(){//method for display (2)
System.out.println("print b value is:"+b);//print b value
System.out.println("print a value is:"+a);//print a value
}
}
class override{//main class
public static void main(String args[]){//main method
child_class C=new child_class(34,56);
//create object for child class and pass argument
C.display();// call method using object
}
}

 

There are other Java language keywords that are similar to this post

extends keyword in Java

implements keyword in Java

interface keyword in Java

Abstract keyword in Java

class keyword in Java

public keyword in Java

 

Suggested for you

method in Java language

Encapsulation  in Java language

Abstract class in Java languag

Method overloading in Java language

Constructor in Java Language

Method overriding in Java language

Constructor overloading in Java language

difference to constructor and method in Java

Encapsulation in Java language

 

Encapsulation in Java programming language
Abstract class in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

2 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

4 weeks ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

4 weeks ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago