Inheritance in OOPS | C++ | JAVA | C++ v/s JAVA
INTRODUCTION
What is Inheritance?
One can say general meaning of inheritance is the common term for getting some heritage or habits from your parents of fore parents.
E.g. When a person receive property or blood group from his parents it is called as inheritance.
Where as in Object Oriented Programming, Inheritance is a condition when an object or class upon another object or class, retaining the similar implementation. Also defined as deriving new class from existing one such as base class and then forming them into hierarchy of classes.
What is Base-Class and Sub-Class?
The class whose properties are inherited is called as Super-Class (Base-Class or Parent-Class). And the class who inherits the properties from different classes is called as Sub-Class(Derived-Class or Child-Class).
Why do we use Inheritance?
With Inheritance, we can reuse the fields and methods of the existing class in different classes with different parameters. Hence inheritance gives facility of Reusability which is an important concept of OOPs.
Lets see it with diagram
Let's See and Example of Inheritance:
Here You can see Car and Bus are Child of Automobiles and Cart and Rickshaw are Child of Pulled Vehicle's. Where as Automobiles and Pulled Vehicles are Child of Vehicles.
Lets see how one can write code for it:
class Vehicles {
// Tiers() function
// Speed() function
};
class Automobiles : public Vehicles {
// CC() function
};
class Pulled_Vehicles : public Vehicles {
//Handel() function
};
So like this we can expand this example in code. Where you can see Automobiles and Pulled_Vehicles as the child class of Vehicles.
Different Types of Access Specifier's:
There are 3 types of access specifier:-
1. Public
2. Protected
3. Private
We hardly use Protected and Private in Inheritance but Public is used most of the time.
Type of Inheritance
Inheritance in C++ :
1. Single Inheritance
2. Multi Level Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Inheritance in JAVA :
1. Single Inheritance
2. Multi Level Inheritance
3. Hierarchical Inheritance
Inheritance in OOPS
1. Single Inheritance
Single inheritance enables a derived class to inherit properties and functions from single super class only. Single inheritance is the simplest of the inheritance models to used. So this can be used when you have a class that has basic characteristics and you need to create more classes that have all the basic characteristics and some other specific characteristics.
Programming Syntax:
C++:
class Parent // base class
{
..........();
};
class Child : (access specifier) A // derived class
{
...........();
} ;JAVA:
public class Parent_A // Base Class
{
.........();
}
public class B extends A // Child Class
{
..........();
} 2. Multi Level Inheritance
Multilevel inheritance is a type of extending the features of one derived class to another new class. Since the parent class features are extended up to multiple levels thus this type of inheritance is known as multilevel inheritance. When a child class extends a parent class, it can use all the features parent class that it have from other parent class.
Programming Syntax:
C++:
class Parent_A // base class
{
...........();
};
class Parent_B : (Access Specifier) Parent_A // derived class of Parent_A and Base Class for Child C
{
...........();
} ;
class Child_C : (Access Specifier) Parent_B // Child Class of Parent_B
{
...........();
} ;public class Parent_A // Base Class
{
.........();
}
public class Child_B extends Parent_A
{
..........();
}
public class Child_C extends Child_B
{
...........();
}3. Multiple Inheritance
In this Inheritance an object or class can inherit characteristics and features from more than one parent object or parent class. And it differ from signal inheritance as it can only inherit from one parent class only. This inheritance is only used in C++ not in JAVA.
Programming Syntax:
C++:
class Parent_A
{
..........();
};
class Parent_B
{
...........();
} ;
class Child_C : (Acess Specifier)Parent_B, (Access Specifier) Parent_A // derived class from A and B
{
...........();
} ;JAVA:
Java Doesn't support Multiple Inheritance because it creates problems during various operations like casting, constructor chaining etc.
4. Hierarchical Inheritance
This Inheritance occur when several classes are derived from common base class it is called hierarchical inheritance.
Programming Syntax:
C++:
class Parent_A // base class
{
..............()
};
class Child_B : (Access Specifier) Parent_A // Child class from Parent_A
{
...........()
} ;
class Child_C : (Access Specifier) Parent_A // Child class from Parent_A
{
...........()
} ;
class Child_D : (Access Specifier) Parent_A // Child class from Parent_A
{
...........()
} ;JAVA:
public class Parent_A // base class
{
..............()
};
public class B extends Parent_A // derived class from A
{
...........()
} ;
public class C extends Parent_A // derived class from A
{
...........()
} 5. Hybrid Inheritance
This inheritance is different from all inheritance as it don't have its own properties as its and combination of different type of inheritance. Or one can say a Hybrid Inheritance is a combination of quite more than one types of Inheritance.
Programming Syntax:
C++:
class Parent_A // Base Class
{
.........()
};
class Child_B : (Access Specifier) Parent_A //Child of Parent_A
{
..........
} ;
class Parent_C // New Base Class
{
...........
};
class Child_D : (Access_Specifier) Parent_B, (Access_Specifier) Parent_C //Child of both Parent class A and C
{
...........
};As Java does not support multiple inheritance, hybrid inheritance is also not supported in Java.
Why java doesn't support multiple inheritance and hybrid?
The reason after this is often to prevent/avoid ambiguity. Consider a case where class B extends class A and sophistication C and both class A and C have the identical method display(). Now java compiler cannot decide, which display method it should inherit. to stop such situation, multiple inheritances is not allowed in java.
Sometimes there may well be two or more possible matches for an invocation of a way, but the. compiler cannot determine the foremost specific match. this might be referd to as ambiguous invocation.
Advantages of using inheritance
The main advantages of inheritance are code reusability and readability. When derived class inherits the properties and functionality of base class, we skip the part to write the identical code again in child class. This makes it easier to reuse the code, makes us reduce the complexity of code and also the code becomes rather more untestable.
As the existing code is reused, it finally ends up to less development and maintenance cost. It also help subclass to follow standard interface.
Thus we are ready to say subclass code are reliable because the base_class code will already be tested and debugged.
Inheritance can also make application code more flexible to change/alter because classes that inherit from a standard superclass are often used interchangeably. If the return style of a technique is superclass
Reusability -- facility to use public methods of base class without rewriting the identical Extensibility -- extending the bottom class logic as per business logic of the derived class Data hiding -- base class can arrange to keep some data private in order that so as that it can't be altered by the derived class
Overriding--With inheritance, we'll be able to override the methods of the bottom class so meaningful implementation of the bottom class method will be designed within the derived class.
Disadvantages of using inheritance
One of the most disadvantages of inheritance is that the increased time/effort it takes the program to leap through all the amount of overloaded classes. If a given class has ten levels of abstraction above it, then it'll essentially intermit jumps to run through a function defined in each of those classes
If a way is deleted within the "super class" or aggregate, then we are going to must re-factor just in case of using that method. Here things can get a small amount complicated just in case of inheritance because our programs will still compile, but the methods of the subclass will now not be overriding superclass methods. These methods will become independent methods in their claim.
Also with time, during maintenance adding new features both base also as derived classes are required to be changed. If a technique signature is modified then we’ll be affected in both cases (inheritance & composition)
Class inheritance and Interface inheritance are two major types (subtyping) As far as inheritance is concerned. Both are appropriate for particular patterns but the latter has inherit favor since GoF was published. If you were learning OOP back within the early 90s, you’d comprehended about Booch Method and Rumbaugh Method, but nowadays you hear little of these.
Comparison of Inheritance in C++ and Java
The purpose of inheritance is same in C++ and Java. Inheritance is employed in both languages for reusing code and/or creating an ‘is-a’ relationship.
In Java, all classes inherit from the Object class directly or indirectly. Therefore, there's always one inheritance tree of classes in Java, and Object class is root of the tree. In Java, when create a category it automatically inherits from the Object class. In C++ however, there's a forest of classes; once we create a category that doesn’t inherit from another, we create a brand new tree in forest.
The meaning of protected member access specifier is somewhat different in Java. In Java, protected members of a category “A” are accessible in other class “B” of same package, whether or not B doesn’t inherit from A (they both need to be within the same package).
Java uses extends keyword for inheritance. Unlike C++, Java doesn’t provide an inheritance specifier like public, protected or private. Therefore, we cannot change the protection level of members of base class in Java, if some data member is public or protected in base class then it remains public or protected in derived class. Like C++, private members of base class don't seem to be accessible in derived class.
Unlike C++, in Java, we don’t should remember those rules of inheritance which are combination of base class access specifier and inheritance specifier.
In Java, methods are virtual by default. In C++, we explicitly use virtual keyword.
Default virtual behavior of methods is opposite in C++ and Java:
In C++, class member methods are non-virtual by default. they will be made virtual by using virtual keyword
In Java, methods are virtual by default and might be made non-virtual by using final keyword.
Java uses a separate keyword interface for interfaces, and abstract keyword for abstract classes and abstract functions.
Unlike C++, Java doesn’t support multiple inheritance; a category cannot inherit from over one class. However, a category can implement multiple interfaces.
In C++, the default constructor of the parent class is automatically called, but if we would like to call parameterized constructor of a parent class, we must use Initializer list. Like C++, default constructor of the parent class is automatically called in Java, but if we wish to call parameterized constructor then we must use super to call the parent constructor.
CONCLUSION
The idea behind inheritance in Java is that you simply can create new classes that are built upon existing classes. When you inherit from an existing class, you'll reuse methods and fields of the parent class.
We saw diff varieties of inheritance in C++ and java like and their world examples too..Then we saw why multiple n hybrid inheritance aren't supported by java. which is because
Then we saw about my inheritance is so important and popular. Its advantages.
Followed by advantages, we saw a number of its disadvantages..
Finally, we saw, what are key differences in inheritance in C++ and java
This was all about inheritance in C++ and java and their differences in inheritance in java and C++
Work Done By:
Sumeet Tambe : sumeet.tambe19@vit.edu (Diagram and Information about Multiple inheritance )
Aditya Tangadi : aditya.tangadi19@vit.edu (All the Definitions and basic information)
Tejas Toshniwal : tejas.toshniwal19@vit.edu (Syntax Codes and Comparison Between JAVA and C++)
Manasi Vathkar : manasi.vathkar19@vit.edu (Advantages and Disadvantages of Inheritance )








Very informative
ReplyDeleteAmazing blog! Was quickly able to grasp the concept of Inheritance. Quite easy to understand!
ReplyDeleteVery well written!
ReplyDeleteExcellent work...easy to understand
ReplyDeleteExcellent information👍🏻
ReplyDeleteVery knowledgeable and easy to understand.
ReplyDeleteInformative!!
ReplyDeleteVery informative
ReplyDeleteInformative 👍
ReplyDeleteGreat work 👍
ReplyDeleteInformative blog
ReplyDeleteBrilliant
ReplyDeleteBrilliant!!!
ReplyDeleteBrilliant!!!
ReplyDeleteInformative blog
ReplyDeleteGood info👍
ReplyDeleteFlow was good.....nice work!!
ReplyDeleteInformative!
ReplyDeleteNice Blog
ReplyDeleteGreat
ReplyDeleteGreat work 👏
ReplyDeleteNice confined info🙌
ReplyDelete