Hammad Hassan
Hammad Hassan

Reputation: 1222

Working With Abstract Class

In java if there is an abstract class A, class B inheriting from A and if class B does not provide the implementation of any method of class A. Then could we create the object of class B? CASE 2: What will happen if class B provide implementation of some functions of class A. Then still could we create an object of class B?

Upvotes: 0

Views: 93

Answers (3)

Dharmraj
Dharmraj

Reputation: 174

Its depends upon Class A :

  1. If class A does not have any abstract method then class B can instantiated, No issue.
  2. If class A having abstract method then class B need to give implementation of each abstract method (in this case also class B can be substantiated) otherwise B itself be abstract.

    if class B making itself abstract it can't be instantiated.

Upvotes: 1

Fernando
Fernando

Reputation: 7905

What's inside class A?

If class B is concrete, it needs to implement all abstract methods from class A, otherwise it will not compile.

If class B is abstract you can't instantiate it at all.

The rule is:

If class B inherits from abstract class A, it must implement all abstract methods from A in order to be concrete. Otherwise, B must be marked as abstract too.

PS: I see you are new to SO, please read the best practices here before posting a question.

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201517

1 No. And Class B would also have to be abstract (or you'd get a compiler error).

2 If by some, you mean all of the abstract methods, then yes. If by some, you mean a subset of the abstract methods - then no and see answer 1.

Upvotes: 1

Related Questions