Skip to main content

Posts

Showing posts with the label inheritance

Hierarchical inheritance

Hierarchical Inheritance You may be having the below question regarding hierarchical inheritance in java. Does Java supports hierarchical inheritance? Yes, It is quite possible to have hierarchical inheritance in java. What is hierarchical inheritance? As you can see in the above diagram that when a class has more than one child classes (sub classes) or in other words more than one child classes have the same parent class then such kind of inheritance is known as hierarchical. Let’s have a look at the below example program to understand it better – I’m using the above figure for implementing hierarchical inheritance in the below example- Class A { public void methodA () { System . out . println ( "method of Class A" ); } } Class B extends A { public void methodB () { System . out . println ( "method of Class B" ); } } Class C extends A { public void methodC () { System . out . println ( "...

Hybrid inheritance

  H ybrid inheritance   in Java Programming Before we discuss what is hybrid inheritance, let me answer few of the questions regarding this. Does java supports hybrid inheritance? Yes and No. If you are using only classes then this is not allowed in java, however using interfaces it’s possible to have  hybrid inheritance in java . We will see this in below example programs. Hierarchical inheritance and Hybrid inheritance are different? Yes, Hierarchical inheritance is different than hybrid inheritance. Hierarchical inheritance is possible to have in java even using the classes alone itself as in this type of inheritance two or more classes have the same parent class or in other words a single parent class has two or more child classes, which is quite possible to have in java. Hybrid Inheritance in Java As you can see in the above diagram that it’s a combine form of single and multiple inheritance. Since java doesn’t support m...

Multiple inheritance

Does Java support Multiple inheritance? Earlier we discussed  types of inheritance . In this post we will discuss why java doesn’t support  multiple inheritance . We would also see how to achieve this using  interfaces in Java . Why Java doesn’t support multiple inheritance? C++ , Common lisp and few other languages supports multiple inheritance while java doesn’t support it. It is just to  remove ambiguity , because  multiple inheritance  can cause ambiguity in few scenarios. One of the most common scenario is  Diamond problem. What is diamond problem? Consider the below diagram which shows multiple inheritance as Class D extends both Class B & C. Now lets assume we have a method in  class A  and  class B  &  C  overrides that method in their own way.  Wait!! here the problem comes  – Because D is extending both B & C so if D wants to use the same method which method would be cal...

Multilevel inheritance

Multilevel inheritance We discussed a bit about  Multilevel inheritance  in types of inheritance in java. In this tutorial we will explain multilevel inheritance with the help of   diagram  and  example program . It’s pretty clear with the diagram that in Multilevel inheritance there is a concept of grand parent class. If we take the example of above diagram then class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B. So in this case class C is implicitly inheriting the properties and method of class A along with B that’s what is called multilevel inheritance. Example : In this example we have three classes –  Car, Maruti and Maruti800. We have done a setup – class Maruti extends Car and class Maurit800 extends Maurti. With the help of this Multilevel hierarchy setup our Maurti800 class is able to use the methods of both the classes (Car and Maruti). class Car { ...

EXAMPLE : Inheritance in Java

What is Inheritance in Java Programming? Inheritance  is one of the feature of Object-Oriented Programming (OOPs). Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as super-class. The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class. Inheritance is a  compile-time  mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance. The superclass and subclass have  “is-a”  relationship between them. Let’s have a look at the example below. Inheritance Example Let’s consider a superclass  Vehicle . Different vehicles have different features and properti...