Overriding

Polymorphism in Java

The ability to change form is known as polymorphism. Java supports different kinds of polymorphism like oveloading and overriding.

 

Overloading

The same method name (method overloading) or operator symbol (operator overloading) can be used in different contexts.

In method overloading, multiple methods having same name can appear in a class, but with different signature. And based on the number and type of arguments we provide while calling the method, the correct method will be called.

Rules for Overriding

  1. Only non-static visible instance methods can be overriden:

    • Private methods are not visible and hence not overriden.

    • Protected methods can be overriden by a subclass within the same package.

    • Static methods are not overriden, but they are inherited.

  2. When you refer to a child class object using a Parent reference (e.g.  Parent p = new Child()) and invoke a method, the overriden child class method will be invoked.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!