Calling static factory method with unknown class - java

Hi I have an abstract class which have many subclasses. Id like to make this abstract class' constrcutor private and create factory method. How should this method look like to work in the same way in every sub-class? If I make:
return new AbstractClass();
I get error saying: Class is abstract, cannot be instances... Should I use reflection?

You can access the constructor of the abstract class from the subclasses using the super keyword.
public SubClass() {
super(); // this will call AbstractClass()
// something else that you want to do for this subclass
}

As already pointed out in the comments, you can't use the new keyword with an abstract class. When you use new, you need to know the real type.
You could either implement the factory method in the abstract base class and make it decide which non-abstract subclass to return based on the parameters passed to the create method and/or some internal logic.
Or you could make the factory method itself abstract and implement it in every non-abstract subclass to return an object of that type.

Related

How can i call an non static method of Abstract class

How can i call an non static method of Abstract class, without using its sub class or extends it because abstract class and sub class is too complex so i do not want to cause any side effect.
for example concrete static methods of an abstract class can be call with class name an dot operator, without creating sub class. Similar is there any way to call a non static method.
i just want to run a method and i do not want run any other code. i tried to use reflection but it requires instance
Abstract classes are abstract, meaning that you cannot create an instance of the class.
Therefore, you cannot call instance methods of an abstract class.
public abstract class Foo {
static void bar();
void foobar();
}
you can call Foo.bar() as it is not an instance method (meaning that it does not require an instance of the class) but you cannot call foobar() since you cannot do new Foo().foobar().
Foo.bar(); // OK, we don't need an instance.
Foo foo = new Foo(); // Not OK - we cannot instantiate an abstract class.
foo.foobar();
For a way to create an instance of an abstract class without having to use derived classes, see ernest_k's answer utilizing anonymous classes.
The short answer is that you can't. You need an instance.
An easy way to create an instance is using an anonymous class:
AbstractClass o = new AbstractClass(){
//implement abstract methods... or just leave stubs
};
o.concreteMethod();

Accessing abstract method implementation from abstract interface

My application is dependent on another applications abstract method implementation. I want help in how to access the implementation of that abstract method as the other application is only exposing us the Abstract interface which has the method definition.
So, my question is how to access the implementation of the abstract method, present in the abstract class. Do we have to use reflection for this ?
You can do that but you'll need to instantiate a class that extends the abstract class.
Method m = AbstractClass.class.getDeclaredMethod("MethodName", Integer.class);
m.setAccessible(true);
m.invoke(new InstanceOfAbstractClass(),"parameter");
This is useful just if you need to access a private method of the abstract class. If this is not the case, you'll need to call the method from the child instance.

Calling a subclass method for a superclass constructor

I have a superclass Class and a subclass SubClass. Our teacher has asked us to put all the class dependent methods in Class and all the independent ones in SubClass. For example, I need to have the search and sort methods in SubClass.
However, in main, the array list that I work with is defined with the Class constructor and it does not let me call the method from the SubClass to search/sort.
How can I fix that?
Keep in mind that search and sort need to be in the subclass. I cannot put them in the super class just so the program would work.
Thanks!
Don't. That's a terrible idea. Because the sub-class hasn't been instantiated when you're in the super constructor. Instead, you have to finish the construction of your instance(s). Then you can call the method with your variable reference.
For just solving problem at hand. You can call the superclass constructor in subclass and instantiate a subclass object. This will let you instantiate the subclass object on which further you can call your sort etc.
public class SuperClass {
//definition
}
public class SubClass extends SuperClass {
SubClass(//arg) {
super(//arg)
}
}

java abstract class inheritance

i have an abstract class,this class is extended in her subclasses:
i implementend one method on this abstract class and i made the other method abstracts
the implemented method is a general method that every subclass object has to access on it.So i decided to implement it on the abstract class,avoid implementing the same method on each subclass.
little example:
public abstract class Foo{
//plus constructor and other stuff.
public abstract void differentTypeOfImplementation();
public void doSomething(Foo foo){
//do something with the generic Foo object passed
}
}
i want your opinion on this type of implementation,
regards.
This question is probably too open ended, but your solution is perfectly fine.
The alternative is that you can make an Interface with differentTypeOfImplementation(), and then a utility class with doSomething. That way, your subclasses can also extend from other classes. However, if subclasses may occasionally override doSomething, or if doSomething require accessing internal states of the object, then what you have is perfectly valid.
Implementing a method in an abstract class is very much valid and acceptable design. If this method implementation is necessary for all its subclasses then this is the way to go. In your example however - the signature of the method makes it little fishy - it looks like you are not using the super class state in any way . That means you could as well declare this method as static.

Java: Make a method abstract for each extending class

Is there any keyword or design pattern for doing this?
Please check the update
public abstract class Root
{
public abstract void foo();
}
public abstract class SubClass extends Root
{
public void foo()
{
// Do something
//---------------- Update -------------------//
// This method contains important code
// that is needed when I'm using a instance
// of SubClass and it is no instance of any
// other class extending SubClass
}
}
public class SubberClass extends SubClass
{
// Here is it not necessary to override foo()
// So is there a way to make this necessary?
// A way to obligate the developer make again the override
}
Thanks
If you are doing this, then you are probably abusing inheritance; inheritance, contrary to popular myth, is not intended for making custom hooks/handlers, but rather to enable alternative implementations.
If you want your user to provide some sort of function/hook/callback, then you should define an interface that provides just those methods that you need your user to define. Then you should require the user to pass in an instance of that interface to your object's constructor or passed into the function that needs it.
Aggregation, delegation, and composition are frequently better and safer design patterns than inheritance; forcing other users to inherit from your class, is incredibly risky, as it provides the user with many opportunities to violate the contract of your class or to invalidate the invariant of your base class.
If every class subclassing SubClass has to override foo() then why provide an implementation at all in SubClass? You can simply remove the method definition from SubClass and then all subclasses will be forced to provide an implementation.
If you really want to, you can re-declare foo as abstract.
public abstract class SubberClass extends SubClass
{
public abstract void foo();
}
Instead of overriding foo() in SubClass, create a new method fooImpl() and leave foo() abstract. This way, all classes must implement foo() but you can simply implement it by calling fooImpl() if that is already enough.
Yeah it is not necessary to override foo() in SubberClass.
You can't have it both ways. You can't provide a method with a default implementation AND require child classes override it. Instead of declaring the method as abstract in Root, you could define an interface (IFoo) with the method declared and then provide an abstract class that implements the interface. That would still require a concrete child class but would not require a method override.
Most of the time you see this type of pattern, an interface is used to define a set of methods and an abstract base class provides some default implementations for some but not all methods from the interface. This requires the concrete child class to provide code for the remaining methods and the option to override the default behaviors.
In any case, you can't provide a default behavior for a single method and require child classes to override that same method.

Categories

Resources