Comparison Between Inheritance and Polymorphism
Introduction
What is Inheritance?
Inheritance is the ability of a class to utilize the features and traits of another class.
Inheritance consists of two types of classes are involved Superclass and subclass:
SubClass : A subclass is a class that utilizes the traits and characteristics of other classes.
Superclass : Other classes use the characteristics of this class.
Syntax :
class ParentClass_Name {
//methods and fields of ParentClass
}
class SubClass_Name extends ParentClass_Name
{
//Methods and fields of SubClass
}
Why use inheritance ?
By using inheritance we can reuse the code written in parent class like function etc.
Runtime polymorphism, often known as method overriding, is a way for obtaining polymorphism via inheritance.
Types of inheritance :
Single Inheritance :
when there is a single subclass and superclass and subclass inherits from the superclass, then it is called as single inheritance.
Example:
class Car{
void speed() {
System.out.println("Speeding…");
}
}
class SuperCar extends Car {
void boost() {
System.out.println("Boosting...");
}
}
class Inheritance {
public static void main(String args[]) {
SuperCar sc=new SuperCar();
sc.boost();
sc.speed();
}
}
Output:
Boosting...
Speeding...
Multilevel Inheritance :
Multilevel inheritance occurs when a child class inherits from a parent class and also serves as a superclass for another subclass.
Example:
class Car{
void speed() {
System.out.println("Speeding…");
}
}
class SuperCar extends Car {
void boost() {
System.out.println("Boosting...");
}
}
class HyperCar extends SuperCar {
void performance() {
System.out.println("High...");
}
}
class Inheritance {
public static void main(String args[]) {
HyperCar hc = new HyperCar();
hc.performance();
hc.boost();
hc.speed();
}
}
Output:
High…
Boosting...
Speeding...
Hierarchical inheritance :
Multiple classes inherit from a one superclass is called as Hierarchical inheritance.
Example:
class Car{
void speed() {
System.out.println("Speeding…");
}
}
class SuperCar extends Car {
void boost() {
System.out.println("Boosting...");
}
}
class HyperCar extends Car {
void performance() {
System.out.println("High...");
}
}
class Inheritance {
public static void main(String args[]) {
HyperCar hc = new HyperCar();
hc.performance();
hc.speed();
//hc.boost(); //Error
}
}
Output:
High…
Speeding…
Multiple inheritance :
When a single class inherits from numerous superclasses, this is known as multiple inheritance.
Note: Multiple inheritance is not supported in Java.
Hybrid Inheritance :
It is made up of two or more different types of inheritance.
Example:
class C
{
public void disp()
{
System.out.println("C");
}
}
class A extends C
{
public void disp()
{
System.out.println("A");
}
}
class B extends C
{
public void disp()
{
System.out.println("B");
}
}
class D extends A
{
public void disp()
{
System.out.println("D");
}
public static void main(String args[]){
D obj = new D();
obj.disp();
}
}
Output:
D
Polymorphism
What is polymorphism ?
Polymorphism is a term used to describe a phenomena that occurs when numerous classes are related through inheritance.
Polymorphism in Java is performed using the following methods:
1 . Method Overloading
2 . Method overriding
Method Overloading :
When a function of the same name exists in a class for more than one time and each function has different parameters then it is called method overriding.
Example :
Class pattern {
// method without parameter
public void display() {
System.out.print(“*”);
}
//method with parameter
public void display(String S) {
System.out.print(“S”) ;
}
}
Method Overriding :
In inheritance, When both parent class and child class both contain, 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 Parent class and child class.
Example :
class Animal {
public void name() {
System.out.println("I am Animal");
}
}
class dog extends Animal {
public void name() {
System.out.println("I am Dog");
}
public static void main(String[] args) {
Animal A = new Animal();
A.name();
dog D = new dog();
D.name();
}
}
Output :
I am Animal
I am Dog
Comparison :
In Inheritance a new class is created using the properties of other classes and polymorphism is basically a common thing for multiple forms.
Polymorphism works with functions or methods, while inheritance works with classes.
Polymorphism lets the object determine which form of function to implement at compile time in overloading and at runtime in overriding, reducing code length and allowing for reuse.
Inheritance can be single, multiple, hybrid, multilevel, hierarchical, and so on, and polymorphism can be compile time (method overloading) or runtime (method overloading) (method overriding).
Let's see the 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 the other method of sound 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 object so it will be decided at compile time and runtime.
Good information
ReplyDeleteNice work guys
ReplyDeletePerfect
ReplyDeleteInformative
ReplyDeleteGood Job
ReplyDeleteGreat Information
ReplyDeleteGreat
ReplyDeleteWoahh!!
ReplyDelete