CodesJava

Easy learning with example program codes

Method overriding in java


If a subclass provides a method with the same signature (name and parameter) as in its super class, then subclass overrides the method of its super class. This process of overriding a super class method by subclass is known as method overriding.

Conditions for method overriding:

  1. Method in subclass must have same signature as in its super class.
  2. Two classes must follow IS-A relationship.

Example:

/**
 * This program is used for simple method overriding example.
 * @author CodesJava
 */
class Student {
      /**
       * This method is used to show details of a student.
       * @author CodesJava
       */
      public void show(){
            System.out.println("Student details.");
      }
}
 
public class CollegeStudent extends Student {
      /**
       * This method is used to show details of a college student.
       * @author CodesJava
       */
      public void show(){
            System.out.println("College Student details.");
      }
 
      //main method
      public static void main(String args[]){
       CollegeStudent obj = new CollegeStudent();
       //subclass overrides super class method
       //hence method of CollegeStudent class will be called.
       obj.show();
      }
}

Output:

College Student details.

Download this example.

Can static method be overridden?

No, Static methods can’t be overridden because they are associated with class not with the object.

Role of access modifiers in method overriding:

Access modifier of overridden method in subclass can’t be more restrictive than in super class. Otherwise it will throw an exception.

 Example:

/**
 * This program is used for simple method overriding example.
 * @author CodesJava
 */
class Student {
      /**
       * This method is used to show details of a student.
       * @author CodesJava
       */
      public void show(){
            System.out.println("Student details.");
      }
}
 
public class CollegeStudant extends Student {
      /**
       * This method is used to show details of a college student.
       * @author CodesJava
       */
      protected void show(){//compile-time error
            System.out.println("College Student details.");
      }
 
      //main method
      public static void main(String args[]){
       CollegeStudant obj = new CollegeStudant();
       //subclass overrides super class method
       //hence method of CollegeStudant class will be called.
       obj.show();
      }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
Cannot reduce the visibility of the inherited method from Student
at com.codesjava.business.CollegeStudent.show(CollegeStudent.java:22)
at com.codesjava.business.CollegeStudent.main(CollegeStudent.java:30)

Download this example.
 
Next Topic: Dynamic method dispatch or Runtime polymorphism in java with example.
Previous Topic: Method overloading in java with example.

Related Topics:

Method overloading in java with example.
Method overriding in java with example.
Dynamic method dispatch or Runtime polymorphism in java with example.
Association in java with example.
Inheritance in java with examples.
Aggregation in java with example.

 

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki