Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract classes

 
Claire Gout Thee
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm preparing for OCJA exam. I have a few question about abstract classes.

I see this explanation :

"By putting nonabstract methods in an abstract class, you give all concrete subclasses inherited method implementations."

How I understand above statement is that an abstract cannot be instantiated but we can instantiated abstract class by putting nonabstract methods in it. Please correct me if i'm wrong.

I created abstract class and put nonabstract methods in it. However, i can't create instance of that abstract class. I guess my understanding is a bit off.

any pointer would be appreciate.

Thanks
Gout Thee
 
Mala Gupta
Author
Ranch Hand
Posts: 363
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gout -

An abstract class can't be instantiated, whether it includes abstract methods or not.

In the following example, abstract class Animal, with an abstract method (move) can't be instantiated in class Forest:



Let's implement the method move:



Even after implementing method move, class Animal can't be instantiated.

So what benefits does it offer?

With abstract method move, any concrete class that inherits class Animal, must implement move():



But with non-abstract method move, you might not implement move in classes Hare, Horse or Bird:



With much respect,
Mala
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gout!
No,abstract class can never be instantiated.they are just written as a base class for extension.even you provide non-abstract methods in it,how will you instantiate it,if you are talking about static factories their still you need to return a object which is impossible to create.

do you know why constructors are declared then in abstract classes???

 
Claire Gout Thee
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen,

Constructors are needed to be implemented for each class?
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so why it is not possible to instantiate abstract class??
 
Claire Gout Thee
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because abstract class are incomplete ones..even if java make it to be instantiated, it needs to be implemented. Thus, it's impossible to instantiate.
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claire Gout Thee wrote:Because abstract class are incomplete ones..even if java make it to be instantiated, it needs to be implemented. Thus, it's impossible to instantiate.

they can be complete,i mean you can provide all non-abstract methods in it.
actually constructor is not the one who actually creates an object,it is one of the requirement for object creation.
how actually we instantiate an object of any class is,for example:-
--------------------------
Gout g=new Gout();
--------------------------
for creating an object we have to invoke "new" operator on the constructor,so it is a "new" operator who actually allocates a memory for an object on heap and then create it via invoking constructor.in case of abstract class compiler don't allow us to invoke new operator on constructor and thus will not be instantiated.also if abstract class don't have a constructor(not possible) then it is impossible to subclass it(as it's constructor will be called in subclass constructor).


Thankyou,

kind regards,
Praveen.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic