wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "abstract " Watch "abstract " New topic
Author

abstract

sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
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

Joined: Jan 10, 2002
Posts: 61106
    
  66

From a class that extends the abstract class.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
no it doesn't work and it didn't work.
sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
How to make use of the constructor of the abstract class . Can anybody kindly illustrate it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

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

Joined: Dec 24, 2007
Posts: 63
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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();


[My Blog]
All roads lead to JavaRanch
sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Dec 24, 2007
Posts: 63
thanks its very clear...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

As I said in my first reply.
 
 
subject: abstract