Java Method Overriding

Method overriding is a feature which allow a subclass to rewrite the implementation of a method already provided by the super class. The overriding is used to define a behavior that is specific to the sub class. When a class inherits a method then there is possibility to override the functionality of that method which means it was not marked final in super class.

Method Overriding Example

class Employee{

//defining office timing for all employees

public void OfficeTiming(){

System.out.println(“The office timing is 9am to 5pm”);



public class CustomerServiceRepresentative extends Employee{

//redefining office timing for customer service representatives

public void OfficeTiming(){

System.out.println(“The office timing is 6am to 2pm”);


public static void main(String args[]){

CustomerServiceRepresentative emp_csr = new CustomerServiceRepresentative();





The office timing is 6am to 2pm

Method Overriding Rules

  • The method name should be exactly the same as of overridden method
  • The method return type should be same or sub type of overridden method’s return type
  • The argument list and type should be exactly same as of overridden method
  • The access level should not be more restrictive than access level of overridden method
  • The method should not be written final

Java Polymorphism

Polymorphism is a feature which allows a method to perform different things based on the object on which it is acting upon. Which mean you can have multiple implementations of a single interface. Any object which can pass multiple (more than one) IS-A test is considered a polymorphic object.

In java an object can only be accessed through a reference variable which can be of a single type. The type of a reference variable cannot be changed. The reference variable type determines which methods it can invoke on an object.

Java Polymorphism Example

class Bike{
int maxSpeed = 110;

class SuzukiBike extends Bike{
int maxSpeed = 120;

public static void main(String args[]){
Bike b = new SuzukiBike();
System.out.println(“The maximum speed of bike is = ” + b.maxSpeed);


The maximum speed of bike is = 110

Java Upcasting

When an object of a sub class type is assigned to the reference variable of a super class is called upcasting. Example:

class JavaParent{
//parent class body
class JavaChild extend JavaParent{
//child class body

JavaParent p = new JavaChild();

The method overloading and method overriding demonstrates different types of polymorphism in java. These concepts were discussed previously.

Java Abstract Class & Abstract Method

Abstraction is a feature which allows you to hide the implementation details from user and show only functionality. In Java abstraction is achieved through abstract classes and interfaces. Here we will discuss abstract class.

Java Abstract Class

In java the abstract class is declared with abstract keyword. The abstract class can have non-abstract and abstract methods. The abstract class cannot be instantiated. It is inherited by another class to provide the implementation of the abstract methods. When abstract class is inherited you need to provide implementations of every abstract method it contain.

Abstract Class Example

public abstract class Vehicle

//non-abstract methods

//abstract methods


Abstract Methods

In java abstract methods are declared with abstract keyword and they don’t have implementations. If you want to have a method in particular class but the actual implementation is to be determined by child class you can define it as abstract in parent class. A semi colon ( ; ) is used at the end of abstract method.

Abstract Method Example

abstract class Vehicle

abstract void run();


class SuzukiBike extends Vehicle{

void run(){

System.out.println(“The suzuki bike is running at 90km/h”);


public static void main(String args[]){

SuzukiBike bike = new SuzukiBike();;




The suzuki bike is running at 90km/h