Introducing Java Polymorphism Concept


Polymorphism

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.

After overriding a method of a Super Class in a Sub Class, Upcasting and running the Sub Class Method through the Super Class Reference.


class Biscuit extends Products {

  void store() {

    //store code

  }

}


class Cake extends Products {

  void store() {

    //store code

  }

}


class Chocolate extends Products {

  void store() {

    //store code

  }

}


class SystemRun {

psvm (String[]args) {

Biscuit b = new Biscuit();

Cake c = new Cake();

Chocolate cl = new Chocolate();

1.b.store();

2.c.store();

3.cl.store();

  }

}

GitHub Projects

Comments

Popular posts from this blog

Introducing Java Encapsulation Concept

Introducing Java Interfaces Concept

Introducing Java Method Overriding And Super Keyword