*
The moose likes Java in General and the fly likes Abstract class problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Abstract class problem" Watch "Abstract class problem" New topic
Author

Abstract class problem

Gavi Raaghav
Ranch Hand

Joined: Apr 28, 2005
Posts: 82
I have an abstract class Base.java. Child.java extends Base.java.
The constructor of Child.java is private.Base.java has an abstract method getInstance().Child.java should implement this method and should return its instance. What should be the method signature in Child.java?

public Base getInstance()
{
return new Child();
}

If i put static in the signature it gives me an error "static method cannot hide instance method from Base".Also what shud be the method signature in Base?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Static methods cannot be abstract for the simple reason that static methods cannot be overridden. Instead, they are hidden. Static methods are always executed through/on a class -- never an instance.

If you call a static method using an object reference, the declared type of the reference is used to choose which class receives the method call -- not the class of the object to which the reference points.

To be clear, the following two methods are totally unrelated as far as Java is concerned. They may as well be named "one" and "two":These methods are fine as they are, but since they aren't related, and you have to call the second method using an explicit reference to the Child class, you may as well change its return type to Child. You can still assign it to a Base reference.You might use them like this:
Gavi Raaghav
Ranch Hand

Joined: Apr 28, 2005
Posts: 82
Thanks for the info but i am still confused.As i said my base class is abstract.
Base class:
public abstract Base getInstance();

Child class:
private Child()
{}

public Base getInstance()
{
return new Child();
}

If i put ststic in the getInstance method of Child i get the error mentioned in my first post.If i dont then how do i access the getinstance method of child since the constructor of it is private?
Anil Sharma
Greenhorn

Joined: Jan 29, 2002
Posts: 1
If still need to live with exisitng code, then write another static method in Child class, which calls the private Child constructor first annd then calls getInstance method.

e.g.

public static Base accessInstance()
{
return new Child().getInstance();
}
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Gavi, I have the strong feeling that what you are trying to do is not possible in Java using static methods. To be sure, and to propose alternatives, we had to know what you are actually trying to obtain, and why. How do you imagine the static methods to be used? What results would you expect from calling them?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Abstract class problem
 
Similar Threads
Understanding singleton pattern
what is the type of Base?
does static methods inherit ? what about private ?
Need help for Marcus exam No1 #58
Template Method/Singleton with Factory