The Art of Inheritance, Overriding and Method Overloading

 

The Art of Inheritance, Overriding and
Method Overloading

Inheritance👇


The process of connecting one class with another class is called Inheritance. Inheritance in Java is implemented using the "extends" keyword. Inheritance is a powerful mechanism in Java that promotes code reuse and supports polymorphism, enabling more flexible and maintainable code.

Example-:


Advantage of inheritance✊

👉Inheritance allows classes to inherit fields and methods from a superclass.

 👉Inheritance supports polymorphism, which is the ability of objects of different classes to be treated as objects of a common superclass.

👉Subclasses can override methods inherited from the superclass to provide specialized implementations.

👉Inheritance allows for the extension of existing classes to create new classes with additional functionality.

👉Inheritance facilitates the organization of classes into hierarchies, which can represent real-world relationships or conceptual hierarchies.

👉Inheritance can simplify maintenance and updates to codebases by promoting a modular approach.

👉Objects of the superclass are inherited by the subclasses, but objects of the subclasses are not inherited by the superclass.

Method Overloading👇

 

When you want to keep multiple methods with the same name in the same class, you can keep those methods as separate methods by changing the parameters of those methods.  This process is called method overloading.  This process is also known as signature changing.Multiple methods in a class with the same name but with different types or numbers of parameters. Method overloading provides flexibility and makes code more readable. It allows developers to use the same method name for logically similar operations, even if the parameters differ.

Example-:


Method Overriding👇

The process of changing the body of a method inherited from a superclass to a subclass is called method overriding. To override, the class must inherit.  

Rules for Method Overriding in Java




Example-:

Different between Overloading and Overriding💁


Reference-:

https://www.geeksforgeeks.org

Tutorials List - Javatpoint

W3Schools Online Web Tutorials

 

Article by-: Rasanjali Herath

E-Mail-:rasanjaliherath899@gmail.com







 

Comments

Popular posts from this blog

Unveiling the Trinity of Standards, Objects, and Classes

A Guide to Object Generating, Data Types, Parameters and Arguments

Getting Started with Design Patterns - Part 1