Inheritance in OOPS | C++ v/s JAVA

TEJAS TOSHNIWAL
9 min readJun 11, 2021

--

Inheritance -Types of Inheritance and difference in inheritance of Java & C++

INTRODUCTION
Inheritance as we all know is one in all the foremost important concepts in object-oriented programming is that of inheritance.

So what’s inheritance?
Inheritance is that method by which object of a specific category acquires properties of object of another category within the same hierarchy. Taking an example, a toddler inherits the traits of his/her parents.

Technically, The potential of a category to derive properties and characteristics from another class is termed to be Inheritance.. Here one class inherits the attributes and methods of another class.

It allows us to make a replacement class (derived class) from an existing class (base class). The derived class inherits the features from the bottom class and might have additional features of its own. With the employment of inheritance the information is formed manageable in a very hierarchical order.

What is base class and derived class?
The class whose properties are inherited is called as superclass (base class, parent class). the category which inherits the properties of other is interpreted as subclass (derived class, child class)

With inheritance, we are able to reuse the fields and methods of the prevailing class. Hence, inheritance facilitates Reusability and is a vital concept of OOPs.

Inheritance allows us to define a category in terms of another class, which makes it easier to make and maintain an application. …. the concept of inheritance implements presence of a relationship.

In inheritance one must note of visibility mode. It specifies weather features of base class are publicly inherited or privately inherited. default mode of visibility is private and one must observe that non-public members of base class can never be inherited.

When deriving a category from a base class, the base class could also be inherited through public, protected or private inheritance. We hardly use protected or private inheritance, but public inheritance is most typically used.

Types of inheritance in C++ and Java

C++ supports five types of inheritance:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

JAVA supports Three types of inheritance:

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance

* In java programming, multiple and hybrid inheritance is supported through interface only. we’ll find out about interfaces later.

* Single Inheritance
Single inheritance enables a derived class to inherit properties and behavior from a only one parent class. It allows a derived class to inherit the properties and behavior of a base class, thus enabling code reusability further more as adding new features to the present code.
Ex-. asexual reproduction where just one parent is involved in production of offspring and every charecteristics from parent are inherited in offspring.

* Mutilpe inheritance
Multiple inheritance is feature of some object-oriented programming languages within which an object or class can inherit characteristics and features from over one parent object or parent class.
Ex- The real life example of inheritance is child and fogeys, all the properties of father are inherited by his son. Child has 2 base class, mom and pa

* Hierarchical
If over one class is inherited from the bottom class, it’s refered to as hierarchical inheritance. In hierarchical inheritance, all features that are common in child classes are included within the bottom class. As an example, Physics, Chemistry, Biology are derived from Science class.

* Multilevel
Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the bottom class for the new class.
Ex- grandfather- father- son

Father springs class of grandfather class. Futher son springs from derived class i.e. father class

* Hybrid
A hybrid inheritance is combination of quite quite one types of inheritance. For example when class A and B extends class C & another class D extends class A then this is often a hybrid inheritance, because it’s a mix of single and hierarchical inheritance.

Why java dosen’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 child class inherits the properties and functionality of parent class, we won’t must write the identical code again in child class. This makes it easier to reuse the code, makes us write the less code and also the code becomes rather rather more readable.

As the code is reused, inheritance helps to chop back code rebundancy and supports code extensibility. it’ll also reduce time complexity.

As the existing code is reused, it finally ends up to less development and maintainence cost. It also help subclass to follow standard interface.
Thus we are ready to say subclass code are reliable because the baseclass 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

Inheritance increases the coupling between base class and derived class so we are ready to say change in base class will cause change all told the bottom classes. So 2 classes can’t be used independent of every other

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 comprehened 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.

Ex-
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++

REFERENCES

https://intellipaat.com/blog/cpp-vs-java-difference/

https://www.geeksforgeeks.org/comparison-of-inheritance-in-c-and-java/

https://www.edureka.co/blog/inheritance-in-java/

https://www.geeksforgeeks.org/inheritance-in-c/#:~:text=The%20capability%20of%20a%20class,Sub%20class%20or%20Derived%20Class.

https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm

https://www.programiz.com/cpp-programming/inheritance

--

--

No responses yet