• Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract class (constructor issue)  RSS feed

 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Q1 .
Do we write a constructor for an abstract class in the real world ??
Since, we cannot create any object for the abstract class, so at one some point the above statement yields NO.

But again we can call an abstract constructor from the subclass which extends this abstract class.

Eg--
abstract class
(
void fun();
}

class loo
{
loo() { super() };

void fun(){ .......... } //overwrites

void halo(){ System.out.print(" This is for Q.2 ");
}


Q2. We can define any method in class loo which is not there is the abstract class. But do such things happens when we work to develop a big project ??
I mean do we define methods in subclass which are not in abstract class( we can I know) in real world project ??
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive me if I mislead you here but I am sure in a real world project you would us interfaces, hopefully some one with more knowledge can explain this in context to your sample.
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every class, whether it is an abstract class or a concrete class has a constructor and that constructor is always called when you create an object.

If you don't define a constructor, one will be defined for you automatically. If you don't call a constructor, one will be called for you automatically.

Also, if you don't call an inherited constructor, one will be called for you automatically.

These three facts about how constructors are defined and called assure that every object will be initialized because a constructor must be called at each level in the inheritance level of each class.

Although abstract classes are not instantiated, subclasses of abstract classes are instantiated or , otherwise the abstract class would be useless.

Abstract classes can have instance variables so sometimes, you have to explicitly define a constructor if you want to give your instance variables values besides the defaults (which is to zero-fill everything).

If you don't define a constructor, it may look like there is not one there, but the Java compiler will automatically generate a constructor with no arguments that automatically calls the no-argument constructor of the super-class.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure whether a million lines of code qualify as *big* for you, but I'm sure it's a real life project I'm working on.

And I can assure you that we use both constructors with arguments in abstract classes and adding methods to subclasses of abstract classes regularly.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I got it. thank you very much ....

Thanks once again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!