Inheritance in Java Example

Inheritance in java is one of the center ideas of Object Oriented Programming. Java Inheritance is utilized when we have is-a connection between items. Legacy in Java is executed utilizing broadens catchphrase. Inheritance in Java

Inheritance in Java is the technique to make a chain of command between classes by acquiring from other classes.

Java Inheritance is transitive – so in the event that Sedan expands Car and Car broadens Vehicle, at that point Sedan is additionally acquired from Vehicle class. The Vehicle turns into the superclass of both Car and Sedan.

Inheritance is broadly utilized in java applications, for instance stretching out Exception class to make an application explicit Exception class that contains more data like mistake codes. For instance NullPointerException<.

Java Inheritance Example

Every class in java certainly broadens java.lang.Object class. So Object class is at the top dimension of legacy chain of command in java.

Let's perceive how to actualize legacy in java with a basic example.

Superclass: Animal package com.journaldev.inheritance; public class Animal { private boolean vegetarian; private String eats; private int noOfLegs; public Animal(){} public Animal(boolean veg, String food, int legs){ this.vegetarian = veg; this.eats = food; this.noOfLegs = legs; } public boolean isVegetarian() { return vegetarian; } public void setVegetarian(boolean vegetarian) { this.vegetarian = vegetarian; } public String getEats() { return eats; } public void setEats(String eats) { this.eats = eats; } public int getNoOfLegs() { return noOfLegs; } public void setNoOfLegs(int noOfLegs) { this.noOfLegs = noOfLegs; } }

The Animal is the base class here. Let’s create a Cat class that inherits from Animal class.

Subclass: Cat

package com.journaldev.inheritance; public class Cat extends Animal{ private String color; public Cat(boolean veg, String food, int legs) { super(veg, food, legs); this.color="White"; } public Cat(boolean veg, String food, int legs, String color){ super(veg, food, legs); this.color=color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } } Notice that we are utilizing extends catchphrase to execute legacy in java.

Java Inheritance Test Program

Let's compose a basic test class to make Cat article and utilize a portion of its methods.

package com.journaldev.inheritance; public class AnimalInheritanceTest { public static void main(String[] args) { Cat cat = new Cat(false, "milk", 4, "black"); System.out.println("Cat is Vegetarian?" + cat.isVegetarian()); System.out.println("Cat eats " + cat.getEats()); System.out.println("Cat has " + cat.getNoOfLegs() + " legs."); System.out.println("Cat color is " + cat.getColor()); } } Java Inheritance Important Points
  1. Code reuse is the most vital advantage of legacy since subclasses acquires the factors and strategies for superclass.
  2. Superclass individuals with default get to is available to subclass ONLY on the off chance that they are in same package.
  3. In the event that superclass doesn't have default constructor, at that point subclass additionally needs an unequivocal constructor characterized. Else it will toss gather time special case. In the subclass constructor, call to superclass constructor is obligatory for this situation and it ought to be the primary proclamation in the subclass constructor.
  4. When a case of Superclass is relegated to a Subclass variable, at that point it's called downcasting. We have to unequivocally cast this to Subclass. For example;
    Feline c = new Cat(); 
    Creature a = c; 
    Feline c1 = (Cat) a; //explicit throwing, works fine in light of the fact that "c" is really of type Cat 

    Note that Compiler won't whine regardless of whether we are treating it terribly, as a result of express throwing. The following are a portion of the situations where it will toss ClassCastException at runtime.

    Puppy d = new Dog(); 
    Creature a = d; 
    Feline c1 = (Cat) a; //ClassCastException at runtime 
    Creature a1 = new Animal(); 
    Feline c2 = (Cat) a1; //ClassCastException on the grounds that a1 is really of type Animal at runtime 

© Journaldev Python 3 tutorial spring tutorial