Comparison of Inheritance and Polymorphism in OOP
Polymorphism and Inheritance being the most important and commonly used characteristics of Object-Oriented Programming play a very important role in the learning process of Object-oriented Programming. Thus the question arises that which of these characteristics has more advantages and which character has more disadvantages. Before comparing these two characteristics, let's first get acquainted with them.
What is Polymorphism?
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
Polymorphism basically means having many forms, and when we have many classes that are related to each other by inheritance, it occurs. Inheritance imports or we can say inherits the properties from another class. Polymorphism uses these properties or methods in various tasks.
Types of Polymorphism
Static polymorphism:
Static polymorphism is also called Compile-time Polymorphism. Static polymorphism is the type of polymorphism that may occur when the compiler compiles any given program. Java supports a polymorphism. It supports compile-time polymorphism through method overloading. Early binding is also a name for static polymorphism. Polymorphism occurs in method overloading because method overloading allows access to different methods through the same interface. As long as the parameter declarations are different, two or more methods in a class can use the same name in method overloading. When the compiler encounters a call to an overloaded method, it compares the type and the number of arguments and identifies the correct version of the overloaded method.
Runtime Polymorphism
Run-time Polymorphism is the type of polymorphism that occurs dynamically when a program executes. Java supports run-time polymorphism by dynamically dispatching methods at run time through method overriding. For this type of polymorphism, method invocations are resolved at run time by the JVM and not at the compile time. Since the run-time polymorphism occurs dynamically, it is also called dynamic polymorphism or late binding.
What is Inheritance?
Inheritance is one of the most important characteristics of Object-oriented programming. The new class is called as Derived class or Child class, and the original class is called as Base class or Parent class.
In inheritance, properties of a parent class are inherited by the child class. Sometimes this child class also passes on the properties to the another child class.
Types of Inheritance
Multiple inheritances
In object-oriented programming, a phenomenon where a class can inherit properties from more than one parent class is called multiple inheritances.
Single Inheritance
When properties of a single class are inherited from another single class, it is called single inheritance. This is a very simple type of inheritance. Thus, it’s also called simple inheritance.
Multilevel inheritance
When properties of a class are inherited from a class that already has inherited properties, it is called multilevel inheritance.
Hierarchical inheritance
In this, various Child classes inherit a single Parent class. The example given in the introduction of the inheritance is an example of Hierarchical inheritance since classes BMW and Audi inherit class Car.
Hybrid Inheritance
When there is a combination of more than one form of inheritance, it is known as hybrid inheritance.
Applications of Polymorphism and Inheritance
Polymorphism :
Polymorphism is a skill that takes many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child's class object.
Any Java object that can pass more than one IS-A test is considered polymorphic. In Java, all Java objects are polymorphic as any object will pass the IS-A test in its own type and Class object.
It is important to note that the only possible way to get an object is a reference variable. Flexible reference can only be one type. Once announced, the variable reference type cannot be changed.
Reference variables may be redirected to other items unless otherwise stated. The type of reference variant may determine which methods you can apply to the item.
Variation of reference may refer to any item of its kind mentioned or any minor version of its declared version. Reference variables can be declared as a class or type of visual interface.
Polymorphism is the phenomenon of something that can represent the structure of mathematical and logical operations from different perspectives.
Example:
package polymorphism;
class AnimalSounds {
public void Sound() {
System. out.println("The animals make different sounds when asked to speak. For example");
}
}
class Cow extends AnimalSounds {
public void Sound() {
System.out.println("The cow says: moh moh");
}
}
class cat extends AnimalSounds {
public void Sound() {
System.out.println("The cat says: mew mew");
}
}
class Dog extends AnimalSounds {
public void Sound() {
System.out.println("The dog says: bow wow");
}
}
public class AnimalMain {
public static void main(String[] args) {
AnimalSounds Animal = new AnimalSounds();
AnimalSounds cow = new Cow();
AnimalSounds cat = new cat();
AnimalSounds Dog = new Dog();
Animal.Sound();
cow.Sound();
cow.Sound();
Dog.Sound();
}
}
//Output:
The animals make different sounds when asked to speak. For example:
The cow says: moh moh
The cow says: moh moh
The dog says: bow wow
Inheritance :
Java inheritance is the mechanism by which an object inherits all the properties and behaviors of its parent object. This is an important part of OOP (Object Oriented Programming System).
The idea behind inheritance in Java is that you can create a new class based on an existing class. If you inherit from an existing class, you can reuse the methods and fields of the parent class. In addition, you can add new methods and fields to your current class.
Inheritance represents an ISA relationship, also known as a parent-child relationship.
Reasons to use inheritance in Java
Override the o method (to enable run-time polymorphism).
o Code reuse.
Terms used in inheritance
o Class: A class is a group of objects that share properties. This is the template or blueprint from which the object was created.
o Subclass / Subclass: A subclass is a class that inherits from another class. Also known as a derived class, extension class, or subclass.
o Superclass / Parent class: A superclass is a class whose subclass inherits its characteristics. Also known as a base class or parent class.
Reusability
As the name implies, reusability is a mechanism that makes it easier to reuse the fields and methods of an existing class when creating a new class. You can use the same fields and methods already defined in the previous class. The syntax of Java Inheritance
1. class Subclass-name extends Superclass-name
2. {
3. //methods and fields
4. }
The extend keyword indicates that you are creating a new class that derives from an existing class. The meaning of "extension" is an extension of function.
In Java terminology, an inherited class is called a parent or superclass, and a new class is called a child or subclass
Advantages of polymorphism
It assists the developer to reprocess the program codes. It means that the old codes and classes written once confirmed and executed can be reused as necessary, which saves a programmer’s time. Thereby these codes and related classes may be relevant to several other methods.
Only one variable can be applied for storing multiple data types such as Int, Float, Double, Long, etc. It makes it easier to search and implement these types of variables used by the users.
Simple debugging the code.
It helps to maintain and diminish the coupling between various functionalities.
Offers flexibility to the programmers to write programs that use only one method for many executions based on need. Here, thereby agreeing on the interface type, this method can perform inversely for different inputs.
It offers one of the greatest advantages: it permits the programming code to extend itself to make use of the previous program and save the developers’ time and effort.
This Polymorphism feature allows programming to become faster and more resourceful to develop codes.
It is inherently good that reduces coupling and maximizes reusability to code a readable program. It helps code maintenance and is easy for reading later and stored in a structured way.
Disadvantages of polymorphism
One of the disadvantages of polymorphism is that developers find it difficult to implement polymorphism in codes.
Run time polymorphism can lead to the performance issue as the machine needs to decide which method or variable to invoke so it basically degrades the performances as decisions are taken at run time.
Polymorphism reduces the readability of the program. One needs to identify the runtime behavior of the program to identify the actual execution time.
Advantages of Inheritance
Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of the inherited class.
Reusability enhanced reliability. The base class code will be already tested and debugged.
As the existing code is reused, it leads to less development and maintenance costs.
Inheritance makes the subclasses follow a standard interface.
Inheritance helps to reduce code redundancy and supports code extensibility.
Inheritance facilitates the creation of class libraries.
Disadvantages of Inheritance
Inherited functions work slower than normal functions as there is indirection.
Improper use of inheritance may lead to wrong solutions.
Often, data members in the base class are left unused which may lead to memory wastage.
Inheritance increases the coupling between the base class and the derived class. A change in the base class will affect all the child classes.
Group Information
OOP Home Assignment
Vivek Sanap (07)
Aarya Sangle
Sanket Lavalekar
Arya Sapre
Atharva Shingade
Siddharth Dhakar
This comment has been removed by the author.
ReplyDeleteVery nice work
ReplyDeleteVery informative blog ;)
ReplyDeleteop next level @vivek sanap
ReplyDeletefire
ReplyDeleteVery nice
ReplyDeleteGreat work 👍🏻
ReplyDeleteGud hai 🙌
ReplyDeleteGreat work guys !!! Keep it up !! Waiting for more
ReplyDelete