DED9

Inheritance Training In Java (In Quite Simple Language)

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 class or base class) and Dog is a subclass (child class 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 only those members that are visible (available) are inherited by the subclass.

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:

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.

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 color property that is private. Hence, it is only visible in Animal. Therefore, the Dog subclass cannot inherit it.

To access the color attribute, we used the general getColor () and setColor () methods inside the Animal superclass.

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

From the above examples, we see that objects in a subclass can access superclass methods as well as define their own methods.

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

Well, 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 both the Animal superclass and the Dog subclass. 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 called. This is called the overriding method.

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

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

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

Here, 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, pay attention to the difference between using super.

Inheritance types

There are 5 types of inheritance:

Java does not support multiple 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 so that runtime polymorphisms can be achieved.

Die mobile Version verlassen