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