• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class problem

 
Gavi Raaghav
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic