Comparison inheritance and polymorphism

What is OOP in JAVA?

OOP stands for Object-Oriented Programming, OOP is a paradigm that provides many concepts, such as inheritance, data binding, polymorphism, etc.

Procedural writing computer programs is tied in with composing systems or strategies that perform procedure on the information, while object-oriented programming is about creating objects which contain both data and methods.

The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

Object-oriented programming has some advantages over procedural programming:

     OOP is faster and easier to execute

     OOP provides a clear structure for the programs

     OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug

     OOP makes it possible to create full reusable applications with less code and shorter development time



Classes and Objects in JAVA?

Classes and objects are the two main aspects of object-oriented programming.

Look at the following illustration to see the difference between class and objects:

For instance


Thus, a class is a layout for objects, and an item is an example of a class.

When the individual objects are created, they inherit all the variables and methods from the class.

Object -- an embodiment of information along with functions that follow up on that information.

An object comprises of:

·       Name = the variable name we give it

·       Member data = the data that describes the object

·       Member functions = behavior aspects of the object (functions related to the object itself)

Class -- a blueprint for objects. A class is a user-defined type that describes what is a particular kind of object will seem to be.

A class description consists of a declaration and a definition. Normally these pieces are parted into separate files.

An object is a single instance of a class. You can make many items from a similar class type.

Polymorphism:

Polymorphism is a widely used technique in Object Oriented Programming across many programming languages. Polymorphism should be visible in Python, Java, C, C++ and some more. The word ‘Poly’ means many and the word ‘morphs’ means many forms. Polymorphism can be translated as “many design”.

A simple anecdote to understand polymorphism is like a man with many roles. A man acts as a son, a brother, a husband, a father, an uncle, as a teacher and many more depending on what is needed of him at that time. In simple words polymorphism allows one to perform a single action in many different ways. That is we can define a single interface but have many implementations of the same.

In Java, Polymorphism is divided into two types:


1)Compile Time Polymorphism(Method Overloading):

2)Runtime Polymorphism(Method Overriding):

1.  Compile time polymorphism:

This type of polymorphism is also called Static Polymorphism. It very well may be executed by Method Overloading of Operator Overloading. Here, Java Does not support Operator overloading. Operator Overloading is in itself a very revolutionary concept in the world of programming.

Method overloading:

When you have to perform a single operation, having methods with the same name increases readability of the program. Consequently when we have various functions(methods) with similar name, the techniques are supposed to be overloaded. Functions can be overloaded by two methods:

1. By Changing the number of Arguments.

2. By Changing the Data Type. 

For eg:-  When we write a program for two numbers we can define the function as add_two(int a, int b). Whereas if we write a function for adding 3 numbers we can define it as add_three(int a, int b, int c). This can sometimes confuse the readers if the code is large and complex. Here is where strategy over-burdening proves to be useful, we can name the capacity as "add" itself and change the quantity of Arguments.

1. add(int a, int b)

2. add(int a, int b, int c).


Output:


2. Runtime Polymorphism

This type of Polymorphism is also known as Dynamic Dispatch Method. The function call to the overridden method is decided at runtime. This is resolved by Method Overriding. Method overriding occurs when a function of the derived class has the same definition as the member method of the base class. Here the base method is said to be overridden by the method of derived class.




                            Inheritance

The Process where one class acquires the properties (methods & fields) of another class is called inheritance.

The new class that’s created is called a subclass (child or derived class) and also the existing class from which the child class is derived is known as superclass (parent or base class). In Inheritance, the properties of the base class are gained by the determined classes.

The 'extends’ keyword is used to perform inheritance in Java.

General format for Inheritance


Example of Java Inheritance


Output:



Types of Inheritance:




Following are the different types of inheritance in Java:

1] Single Inheritance:

In this Inheritance one class extends to another class (one class only).

In below figure, Class B extends only Class A. Class A is a super class and Class B is a Sub-class.



2] Multiple Inheritance:

It is one of the inheritance in Java types where one class broadens more than one class. Java doesn't uphold multiple inheritance.

According to below diagram, Class C extends Class A and Class B both.



 

3] Multilevel Inheritance:

In this Inheritance, one class can inherit from a derived class. Hence, the derived class becomes the base class for the new class.

As per shown in diagram Class C is a subclass of B and B is a subclass of Class A.

 


4] Hierarchical Inheritance:

In this Inheritance, one class is inherited by many sub classes.

As per following diagram, Class B, Class C, and Class D inherit the same Class A.

 


5] Hybrid Inheritance:

It is one of the inheritance types in Java which is a Mix of Single and Multiple inheritance.

As per below example, all the public and protected members of Class A are inherited into Class D, first via Class B and secondly via Class C.

 



Comparison Between Inheritance & Polymorphism :


Blog by —  
Suraj Upadhye, Vaibhav Shahabade, Vaibhavi Jorvekar, Manasi Yadav, Yash Wavare.
Vishwakarma Institute of Technology, Pune
Department of Multi-Disciplinary Engineering 




 

 


Comments

Post a Comment