• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract Class and Constructors

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Do abstract class contains constructors or not. What I read and my understanding is constructors cannot be declared in abstract class. Because abstract class cannot be instantiated. But now am confused after reading Java specification.
In that it says that, " A subclass of an abstract class that is not itself abstract may be instatiated, resulting in the execution of a constructor for the abstract class and there fore extecution of the field initializers for instrance vars of that class".
Now my question is if we define constructor in abstract class what is the effect in subclasses. Will anybody clear my doubt. Or my idea on abstract class is wrong. If they ask can construcors be defined in abstract class. What is the answer
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ofcource an abstract class can have a constructor. U can't instantiate an object of an abstract class. But when u create an object of a subclass of the abstract class, the base class's constructor (i.e abstract class's constructor in this case)
is called. It can be used to initialize members of abstract class. have a look at following code......
abstract class Ab
{
int i;

abstract void m1();
Ab(int n)
{
i = n;
System.out.println("Ab's Constroctor");
}
}
public class testAb extends Ab
{
int testCounter;
testAb(int i , int j)
{
super(i);
testCounter = j;
System.out.println("testAb's constructor");
}
void m1()
{
System.out.println("i am in m1()");
}
public static void main(String args[])
{
testAb ob = new testAb(1,2);
}
}
by the way, where did u read that an abstract class can't have a constructor??? could u give a link??? just curious
Rashmi
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
abstract class can have constructors
But they cant be called directly
They can be instantiated in its subclass which is a concerete
class.
OR. you can define it as an anonymous class
ragu
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstact class can have constructors but they can not be instantiated. the constructors will be used by the subclasses of that abstract.
If u got the Question regarding Abstract Class's constructor u can say the above. may I know where do u read that abstract classes can not have contructors?
~Kumar

------------------
Sun Certified Programmer for the Java�2 Platform
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, Rashmi,Raju & Kumar has explained.The only thing which i want to add is that, Try to write code first at UR own & then clear UR doubts.If it is not cleared then Javaranch is the superb place to share UR quries.For sure u will get answer
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for all. Thanks Rashmi, Your explanation is good. Actually while taking some mock exam, I came across such question. I answered No. But it gave as wrong answer. I wrote some code to clarify my doubts. To make sure I posted the question. Thanks for your time.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic