• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How is this Possible?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Consider the code below,



Now, in the above code, won't the new SuperSub() call the abstract class constructor?? Abstract classes can't be instantiated??which means we can't call their constructors??am I right?? Then jow does the above code work? Anyone on this?

Regards,
Jothi Shankar Kumar. S
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract classes can not be instantiated, but they have constructors and their constructors are called when one instantiates the concrete subclasses.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Just a qucik thought -

class Base is a concrete class with two methods (baseMethod1 and BaseMethod2)

class Sub is an absract subclass of Base with a non abstract method (baseMethod1)

class SuperSub is a concrete class, subclassing Sub, however Sub does not have any abstract methods that Supersub must implement - All fine there - As none of the parent classes have constructors defined, the compiler inserts the default constructor (which are invisible to the developer).

If you were to insert a basic constructor into each of the classes as such



You will get the following result.



Showing that the construtors for all parent classes are call in order of the object heirarchy.

I hope this info is of use.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Thanks for the info. It really helped.

Regards,
Jothi Shankar Kumar. S
 
Jae Stryker
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No worries - I hope people on here can return the favour, when I get stuck (which I will soon no doubt)
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The disscussion here can solve the Questions like:
1) Although abstract class are not supposed to be instantiated then why to declare a constructor?
2) Does compiler inserts default constructor to the abstract class?

and so on...
Thanks
Sanjeev Singh
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sanjib sanju:
The disscussion here can solve the Questions like:
1) Although abstract class are not supposed to be instantiated then why to declare a constructor?
2) Does compiler inserts default constructor to the abstract class?

and so on...
Thanks
Sanjeev Singh



Hey guys but don't you think the questiion still remains unanswered,
Why do we need a constructor for an abstract class.

Agreeded in the above example we need a constructor in the abstarct class as it extends from other concrete class, but why would we need it when abstarct class is the parent class, since we ain't ever going to create an insatance of that abstratc class.
Or is that the coplier will not insert a default constructor when abstract class is the parent class.
 
Jae Stryker
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need a constructor when the parent class is an abstract class as the compiler will use the default one. However as all classes extend the Object class the ability for a constuctor is provided (by interitance).

Writing an constructor for an abstract class has the advantage that you can determine the behaviour (to some extent i.e. initialization) of all child objects upon creation.

(I know this probably hasn't been explained too great)
 
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole idea of creating a abstract class is that we could then create subclasses and override the abstract methods written in abstract class. Ideally, if your class consists of more than two abstract classes, the class should be declared abstract.

For example, We can have an abstract class called "Vehicle" which other subclasses (which extends the class Vehicle) naming "Cars", "Buses" et al. Further, These subclasses would declare all the necessary methods which could override the abstract methods in the abstract class "Vehicle".

There will be a compilation error if you try to instantiate an abstract class.

abstract class Vehicle
{
int size = 0;
abstract public void details();

}

class Car extends Vehicle
{
public void details()
{
size = 10;
System.out.println("Size is " + size);
}
}

class Bus extends Vehicle
{
public void details()
{
size = 50;
System.out.println("Size is " + size);
}
}

public class Motor
{

public static void main(String[] args)
{

Car maruti = new Car();
Bus Best = new Bus();
maruti.details();
Best.details();
}

}

Kindly let me know if i am missing on something.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hey guys but don't you think the questiion still remains unanswered,
Why do we need a constructor for an abstract class.



Can you imagine a child without a father?Should n't compiler know who is the father of this child at runtime?The parent will be present in the JVM at run time for its data members to be used by its subclasses.Though the abstact class are not to be meant for direct instantiation,the constructors will be called whenever one tries to make a instance of its subclasses. I think there is no direct instantiation of the abstact class at any point of time that is what the specificaion says.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gaurang:


Hey guys but don't you think the questiion still remains unanswered,
Why do we need a constructor for an abstract class.





This is a case when you'll need an abstract class constructor
[ November 05, 2006: Message edited by: Valentin Mone ]
 
Goodbye moon men. Hello tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic