Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract classes, constructors

 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!?
 
Vicky Nag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Vicky Nag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic