Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the importance of abstract class that extends from another abstract class

I just started with java about two months ago and I am trying to understand abstract classes more. I understand the basic idea of abstract and its implementation but what I don't get is why an abstract class extends another abstract class and what functionalities does it add. Also, i saw an abstract class that extends another abstract class, but it implements only one function of the other abstract class.

I did look up for example codes but they only show implementation with no explanation to them:

public abstract class Beverage {

   String description = "Beverage";

   public String getDescription(){
      return description;
   }
     public abstract double cost();

 }

// abstract class CondimentDecorator 
   public abstract class CondimentDecorator extends Beverage {

       @Override
       public abstract String getDescription();
 }

As you see, the abstract class CondimentDecorator does not implement all the functions in Beverage abstract class. It only implements the getDescription() function. But if the CondimentDecorator was concrete then it would be required to implement all the functions inside the Beverage abstract class.

like image 347
HSH Avatar asked Mar 29 '19 06:03

HSH


People also ask

Can abstract class extend another abstract class?

An abstract class can extend another abstract class. And any concrete subclasses must ensure that all abstract methods are implemented. Abstract classes can themselves have concrete implementations of methods. These methods are inherited just like a method in a non-abstract class.

Is it necessary to extend abstract class?

A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

What happens when a class extends an abstract class?

Abstract classes can include abstract methods. Any class that extends a class with an abstract method must implement that method. For example, our Mammal class includes an abstract speak() method. Any class that extends Mammal must implement the speak method, and that implementation must have the same signature.

What is an important use of abstract classes?

Which among the following is an important use of abstract classes? Explanation: The abstract classes can be used to create a generic, extensible class library that can be used by other programmers.


2 Answers

It's totally fine when one abstract class extends another abstract class. It just means that it details the behavior of the new class based on the behavior of the old class. But it's still not a full object to be used, because some of its behavior is still unknown.

In analogy to the real world.
Imagine you have a class Vehicle. It can be any vehicle: car, plane, bus, bicycle, whatever. This is your abstract class. Can you use it now? No, because you do not know whether you have to push the pedals or turn the wheel.
Now you create another class, say, Car extends Vehicle. Can you use it now? Probably, but you still do not know whether it's a truck or a passenger car. But you know that it has a steering wheel.
And finally, when you create one more class, say, MySuperPassengerCar extends Car you know exactly what object this is, how it can be used and what methods it has.

like image 130
Pavel Smirnov Avatar answered Oct 31 '22 17:10

Pavel Smirnov


Abstract class defines abstract methods. Any class extending another class enhances the super class by adding more behavior. If the child class is abstract, it can add some abstract behavior.

Abstract methods are like Contracts. The other code can consume the existing code and can depend on it. The concrete class are bound to follow the contract by providing the implementation.

Lets see it with an example below.

public abstract class SuperAbstract {
      public void nonAbstract(){
            // some code
      }
      public abstract void contract();
}

public abstract class SubAbstract extends SuperAbstract{
       public void additionalNonAbstract()
             // some code
        }
        public abstract void additionalContract();
 }

public class Concrete extends SubAbstract{
       public void contract(){
             // implementation
       }
       public void additionalContract(){
               //implementation
       }
}

// although below is allowed and sometimes when we use an external library then this is one of the way but still this is not a good practice. 
// dependencies should be on abstractions only and not on concrete implementation
public abstract class AnotherAbstract extends Concrete{
       public void someMethod(){
             //some code
       }
       public abstract void oneMoreContract();
}

public class ConcreteA extends AnotherAbstract{
        public void oneMoreContract(){
               //some implementation
        }
}

Now Note that in all we have defined 3 contracts and ConcreteA has all the implementations. Also note that as Concrete provides implementations for methods contract and additionalContract hence those implementations are inherited by ConcreteA

Consumer code can easiely depend upon the abstraction. Lets see it in user code (consumer code)

  public class Consumer{
      public void m1(SuperAbstract c)
             c.contract();
             c.nonAbstract();
     }
     public void m2(AnotherAbstract c){
          c.contract();
          c.nonAbstract();
          c.oneMoreContract();
          c.additionalContract();
    }
 }

Now lets see the wiring code providing the dependencies

 public class Main{
       public static void main(String[] args){
            Consumer c = new Consumer();
             c.m1(new Concrete());
             c.m1(new ConcreteA());
             c.m2(new ConcreteA());
     }
}
like image 23
nits.kk Avatar answered Oct 31 '22 17:10

nits.kk