Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract

 
sandeep Talari
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even abstract classes can have constructors but someone can explain how to make use of the constructor of the abstract class
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From a class that extends the abstract class.
 
sandeep Talari
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no it doesn't work and it didn't work.
 
sandeep Talari
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to make use of the constructor of the abstract class . Can anybody kindly illustrate it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sandeep Talari:
no it doesn't work and it didn't work.

You certainly give up quickly. Perhaps you should take the time and describe what you did and why you think it "didn't work" before just dismissing it?
 
sandeep Talari
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please explain


abstract class HelloWorld
{
HelloWorld()
{
System.out.println("hello world");

}
public void rin()
{
}
}
class ManageAbstract extends HelloWorld
{
public static void main(String[] ars)
{
HelloWorld hw = new HelloWorld();
}
}
this is not working .

you can illustrate
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that abstract classes have constructors does not remove the fact that you cannot instanciate an abstract class. You wrongly assume that you can call "new" on any class having a constructor. The golden rule is : you can't instanciate abstract classes. Now, you should think of what would happen with the following code : HelloWorld hw = new ManageAbstract();
 
sandeep Talari
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank for the reply.In the above given code at what situations the constructor of the abstract class runs.Here, System.out.println("hello world"); when it is going to run,Explain .
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know that all constructors from the inheritance tree will be invoked. So what will happen if you extend an abstract class ? No exception to the rule. Its constructor will also be invoked,
 
sandeep Talari
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks its very clear...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said in my first reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic