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
Relationships 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.
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
Encapsulation in Java language
Abstract class in Java languag
Method overloading in Java language
Method overriding in Java language
Constructor overloading in Java language
difference to constructor and method in Java
Encapsulation in Java language
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…