How is this Possible?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks for the info. It really helped.
Regards,
Jothi Shankar Kumar. S
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
~Sanjeev Singh<br />SCJP 1.5
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Intellect distinguishes between the possible and the impossible; Reason distinguishes between the sensible and the senseless. Even the possible can be senseless.......
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
~Sanjeev Singh<br />SCJP 1.5
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
| I'm doing laundry! Look how clean this tiny ad is: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |










