This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Abstract class problem" Watch "Abstract class problem" New topic

Abstract class problem

Gavi Raaghav
Ranch Hand

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

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

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.


public static Base accessInstance()
return new Child().getInstance();
Ilja Preuss

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
I agree. Here's the link:
subject: Abstract class problem
Similar Threads
Template Method/Singleton with Factory
Understanding singleton pattern
what is the type of Base?
does static methods inherit ? what about private ?
Need help for Marcus exam No1 #58