Comparison Between Inheritance and Polymorphism
Introduction
What is Inheritance?
The ability of a class to use the properties and characteristics of another class is called as inheritance.
Basically,In inheritance two types of classes are involved Superclass and subclass,
Subclass : The class which uses the properties and characteristics of other class is called a subclass.
Superclass : The class whose properties and characteristics are used by other classes is called a superclass.
extends keyword is used in java for performing inheritance.
Syntax :
class BaseClass_Name { //methods and fields of BaseClass } class SubClass_Name extends BaseClass_Name { //Methods and fields of SubClass }
Why use inheritance ?
The most important use of inheritance is code reusability, we can reuse the code (methods and fields) written in super class by using base class as well.
Method overriding is also called as a runtime polymorphism, in this way we can achieve polymorphism using inheritance.
Types of inheritance :
Single Inheritance : when there exists a single subclass and superclass and subclass inherits from the superclass, then its is called as single inheritance.
Multilevel Inheritance : A subclass inherits from a superclass and also act as a superclass for another subclass is called multilevel inheritance.
Hierarchical inheritance : Multiple classes inherit from a single superclass is called as Hierarchical inheritance.
Multiple inheritance : A single subclass inherits from multiple superclasses is called as multiple inheritance.
Hybrid Inheritance : It contains the combination of two or more types of inheritance.
Polymorphism
What is polymorphism ?
Polymorphism means a thing which has many forms, it occurs when we have many classes connected by inheritance.
Polymorphism in java can be achieved by following methods : 1. Method Overloading 2. Method Overriding
Method Overloading :
When there are multiple functions in a class with the same name but having different no of parameters or different types of parameters then it's called method overloading.
Example :
Class pattern { // Method without parameter public void display(); { System.out.print("*"); } //Method with parameter public void display(String s ) { System.out.println(S); }
}
Method Overriding : In inheritance, When same method is present in both superclass and base class, then it is called as the method in superclass is overridden by subclass. In this case , the method performs different operations when called from objects of super class and base class
Example : Class Animal { public void name() { System.out.print("I am animal"); } } Class dog extends Animal { public void name() { system.out.println("I am dog"); } } Public static void main (Strings Arg[]) { Animal A = new Animal(); A.name(); dog D = new dog(); D.name(); }
Output : I am animal I am dog
Comparison :
Inheritance is creating a new class using the properties of other classes and polymorphism is basically a common thing for multiple forms.
Inheritance deals with classes and polymorphism deals with functions or methods.
Inheritance reduces code length and supports code reusability where polymorphism allows the object to decide which form of function to implement at compile time in overloading and at runtime in overriding.
Inheritance is of multiple types like single , multiple, hybrid , multilevel,hierarchical etc and polymorphism is of two types which are compile time ( method overloading ) and runtime (method overriding).
Let's take a example of animal of , let's consider dog is a subclass and animal is superclass and legs and sound are the two methods where legs method is only present in animal class and sound method is present in both the classes,dog inherit the feature legs of animal class as its a animal but the sound is different for every animal so it will be decided at compile time and runtime.
Great Work
ReplyDelete