Roshin Maharjan
Roshin Maharjan

Reputation: 1

Are all abstract classes parent classes?

I started learning Java few months ago. I am wondering if all abstract classes are parent classes. An abstract method in an abstract class won’t have a body. So the abstract method it must be overridden when another class extends to it.

So from what I understand when an abstract class has an abstract method, the method is written to make abstract to make sure its child classes include this method. And since it has a child class it should be a parent class. Correct?

Upvotes: -1

Views: 446

Answers (3)

Rishank Jindal
Rishank Jindal

Reputation: 1

Abstract classes do not always extend as parent class as abstract classes can have methods that have implementation.

See, we put abstract modifier with the class keyword if there is at least one abstract method.

For example, we have a class named "Area" in which we have methods to find area of different shapes and we know the implementation of square, rectangle, etc. But we don't know the implementation for triangle so we have to leave it abstract and hence there is now one method inside "Area" class so the class should be abstract.

I have shared my all knowledge about this concept. Hope you will understand it.

Upvotes: -2

Basil Bourque
Basil Bourque

Reputation: 339837

“parent class” is not a term defined in Java. I assume you are asking if an abstract class must have one or more concrete classes that extend from it.

The answer is: No, an extending class is not required.

The Java specifications do not require an abstract class to have subclasses. Extending with a subclass is indeed the purpose of an abstract class, but that is not technically required.

Here’s a couple such cases.

  • A library/framework may include abstract classes with the expectation that the calling programmer will write an extending concrete class.
  • During development, you may create an abstract class before you get around to writing any extending concrete class.

So, no, an abstract class does not necessarily have any concrete class extending from it. Ultimately, though, a never-extended abstract class would serve no purpose, and would deserve deletion.

Upvotes: 3

Marce Puente
Marce Puente

Reputation: 503

 public abstract class LivingBeing {
    int age;
    int getAge() {
       return age;
    }
 }
 
 public abstract class Animal extends LivingBeing {
    public abstract void move();
 }
 
 public abstract class Phonator extends Animal {
    public abstract void emitSound();
 }
 
 public class Dog extends Phonator {
    public void emitSound() {
       System.out.print( "wof" );
    }
 
    public void move() {
       System.out.print( "move" );
    }
 } 

It is not necessary for the abstract class:
to have a parent class.
to have an abstract method.
implements an abstract method of the parent class.

LivingBeing is abstract to prevent objects from being created from it.
Animal is abstract because it has a abstract method.
Phonator does not implement the move method, it leaves it for its child classes.

Upvotes: 0

Related Questions