blog posts

Inheritance Training In Java

Inheritance is one of the main features of OOP (object-oriented programming) that allows us to define a new class from an existing class.

For example,

  1. class Animal
  2. {
  3. // eat () method
  4. // sleep () method
  5. }
  6. class Dog extends Animal
  7. {
  8. // bark () method
  9. }

In Java, we use the extends keyword to inherit a class. Here, the Dog class inherits from the Animal class.

Animal is a superclass (parent or base class), and Dog is a Dog subclass (child or derivative class). The subclass inherits features and methods from the superclass.

C: \ Users \ Mr \ Desktop \ java-inheritance-introduction.png

It is important to note that the subclass inherited only visible (available) members.

Is-a relationship

Inheritance is an is-a relationship. We use inheritance only if there is an is-a relationship between the two classes.

Here are some examples:

  • A car is a vehicle.
  • An orange is a fruit.
  • The surgeon is a doctor.
  • The dog is a Dogl.

Example 1: Inheritance in Java

  1. class Animal {
  2. public void eat () {
  3. System.out.println (“I can eat”);
  4. }
  5. public void sleep () {
  6. System.out.println (“I can sleep”);
  7. }
  8. }
  9. class Dog extends Animal {
  10. public void bark () {
  11. System.out.println (“I can bark”);
  12. }
  13. }
  14. class Main {
  15. public static void main (String [] args) {
  16. Dog dog1 = new Dog ();
  17. dog1.eat ();
  18. dog1.sleep ();
  19. dog1.bark ();
  20. }
  21. }

Output

I can eat

I can sleep

I can bark

Here, we inherited the Dog class from the Animal superclass. The Dog class inherits the eat () and sleep methods from the Animal class.

Hence, Dog class objects can invoke Dog class and Animal class methods.

C: \ Users \ Mr \ Desktop \ java-working-inheritance.png

Example 2: protected keyword

  1. class Animal {
  2. protected String type;
  3. private String color;
  4. public void eat () {
  5. System.out.println (“I can eat”);
  6. }
  7. public void sleep () {
  8. System.out.println (“I can sleep”);
  9. }
  10. public String getColor () {
  11. return color;
  12. }
  13. public void setColor (String col) {
  14. color = col;
  15. }
  16. }
  17. class Dog extends Animal {
  18. public void displayInfo (String c) {
  19. System.out.println (“I am a” + type);
  20. System.out.println (“My color is” + c);
  21. }
  22. public void bark () {
  23. System.out.println (“I can bark”);
  24. }
  25. }
  26. class Main {
  27. public static void main (String [] args) {
  28. Dog dog1 = new Dog ();
  29. dog1.eat ();
  30. dog1.sleep ();
  31. dog1.bark ();
  32. dog1.type = “mammal”;
  33. dog1.setColor (“black”);
  34. dog1.displayInfo (dog1.getColor ());
  35. }
  36. }

Output

I can eat

I can sleep

I can bark

I am a mammal

My color is black

In the example above, the Animal class has a private color property. Hence, it is only visible in animals. Therefore, the Dog subclass cannot inherit it.

We used the general getColor () and setColor () methods inside the Animal superclass to access the color attribute.

Also, pay attention to the protected keyword. Protected is an access-level regulator, just like public and private.

Protected members are also visible in a package and subclass. In the example above, the Animal superclass has a type attribute that is declared protected. Hence, the Dog subclass can access it.

Overriding Method

The above examples show that objects in a subclass can access superclass methods and define their strategies.

What happens if the same method is defined in the super and subclass?

In this case, the methods in the subclass reject the methods in the superclass. For example,

Example 3: Example of the overriding method

  1. class Animal {
  2. protected String type = “animal”;
  3. public void eat () {
  4. System.out.println (“I can eat”);
  5. }
  6. public void sleep () {
  7. System.out.println (“I can sleep”);
  8. }
  9. }
  10. class Dog extends Animal {
  11.   @Override
  12. public void eat () {
  13. System.out.println (“I eat dog food”);
  14. }
  15. public void bark () {
  16. System.out.println (“I can bark”);
  17. }
  18. }
  19. class Main {
  20. public static void main (String [] args) {
  21. Dog dog1 = new Dog ();
  22. dog1.eat ();
  23. dog1.sleep ();
  24. dog1.bark ();
  25. }
  26. }

Output

I eat dog food

I can sleep

I can bark

Here, the eat () method exists in the Animal and Dog subclasses. We created the dog1 object from the Dog subclass.

When we call the eat () method using the dog1 object, the method inside the Dog class is called, and the superclass method is not. This is called the overriding method.

In the above program, we used the overriding method to tell the compiler we were overriding the technique. However, it is not mandatory.

We use the super keyword if we need to call the eat () method of Animal from the Dog subclass.

Example 4: super keyword

  1. class Animal {
  2. public Animal () {
  3. System.out.println (“I am an Animal”);
  4. }
  5. public void eat () {
  6. System.out.println (“I can eat”);
  7. }
  8. }
  9. class Dog extends Animal {
  10. public Dog () {
  11. super ();
  12. System.out.println (“I am a dog”);
  13. }
  14. @Override
  15. public void eat () {
  16. super.eat ();
  17. System.out.println (“I eat dog food”);
  18. }
  19. public void bark () {
  20. System.out.println (“I can bark”);
  21. }
  22. }
  23. class Main {
  24. public static void main (String [] args) {
  25. Dog dog1 = new Dog ();
  26. dog1.eat ();
  27. dog1.bark ();
  28. }
  29. }

Output

I am an Animal

I am a dog

I can eat

I eat dog food

I can bark

We have used the super keyword to call the constructor using super (). Also, using the super. Eat () method, we used the Animal superclass eat () method.

When using the constructor and method, consider the difference between using super.

Inheritance types

There are five types of inheritance:

  • Single heirs – Class B inherits only from Class A.
  • Multilevel Inheritance – Class B inherits from Class A, and Class C inherits from Class B.
  • Hierarchical Inheritance – Class A is a superclass for classes B, C, and D.
  • Multiple Inheritance – Class C inherits from interfaces A and B.
  • Combined Inheritance – A combination of two or more types of inheritance.

Java does not support various and hybrid class inheritance. But it can be used with the help of interfaces.

Why do we use inheritance?

The most important application is the ability to reuse code. The code in the parent class does not need to be rewritten in the child class.

Another application is to use the overriding method to achieve runtime polymorphisms.