aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract classes, constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract classes, constructors" Watch "abstract classes, constructors" New topic
Author

abstract classes, constructors

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
hi, i have the following question that i don't get...
an abstact class can have a constructor, but how that constructor is called if we can't instantiate the abstract class !!?


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Vicky Nag
Ranch Hand

Joined: Jul 02, 2003
Posts: 40
Abstract classes can never be instantiated. But if you create a subclass of an abstract class and implement the abstract functions, you can then create an instance of the subclass.
When you create an instance of the subclass, if the abstract class has a constructor that will be called as a part of initialsation process..
Hope this helps.
V
abstract class Base
{
Base()
{
System.out.println("Base");
}
public abstract void Test();
}
class Child extends Base
{
Child()
{
System.out.println("Child");
}
public void Test()
{
System.out.println("Child Test");
}
public static void main(String args[])
{
new Child();
}
}
Output will be:
Base
Child
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Vicky Nag
Ranch Hand

Joined: Jul 02, 2003
Posts: 40
Abstract classes can never be instantiated. But if you create a subclass of an abstract class and implement the abstract functions, you can then create an instance of the subclass.
When you create an instance of the subclass, if the abstract class has a constructor that will be called as a part of initialsation process..
Hope this helps.
V
abstract class Base
{
Base()
{
System.out.println("Base");
}
public abstract void Test();
}
class Child extends Base
{
Child()
{
System.out.println("Child");
}
public void Test()
{
System.out.println("Child Test");
}
public static void main(String args[])
{
new Child();
}
}
Output will be:
Base
Child
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
thanks guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract classes, constructors