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.
Related
All classes in java extend the Object class implicitly. But that doesn't concern interfaces. Interfaces can only extend other interfaces, but no classes. However, I can override object class methods inside my interface.
public interface NewInterface {
#Override
boolean equals(Object var1);
#Override
int hashCode();
}
Can you please explain in simple words how is this even possible? Is there any use case for this?
Interface is a just contract. It says that all classes that inherits interface should implement these methods. Interface cannot have implementation. It is possible to override a class that implements this interface.
However, from Java 8 you can define static methods in interfaces in addition to default methods.
UPDATE:
The members of an interface are:
Those members declared in the interface.
Those members inherited from direct superinterfaces.
If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method corresponding to each public instance method declared in Object, . It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.
Now it is clear that all superinterface have abstract member method corresponding to each public instance method declared in Object .
Read more about interface members here
I get the following message when trying to implement a class:
public class MyLocationListener implements BDLocationListener
This sentence is wrong. The hint is:Class 'MyLocationListener' must either be declared abstract or implement abstract method 'onConnectHotSpotMessage(String, int)' in 'BDLocationListener
I use Android Studio.
You are declaring that you're going to "implement BDLocationListener" which is an abstract class.
When you implement an abstract class, you must provide a method for every unimplemented abstract method within the abstract class. In this case, onConnectHotSpotMessage(String, int) is one of the abstract methods in that class.
You must either provide an implementation of this method in your MyLocationListener class or declare your MyLocationListener as an abstract class. If you declare MyLocationListener as an abstract class, you will not be able to instantiate it, so it is more likely that you want to implement the method.
Your BDLocationListener class has an Abstract method that needs to be implemented in your MyLocationlistener, it's like a contract, if a class wants to implement an abstract class it has to implement its abstract methods or to be abstract as well.
You can find more information about Abstract class here
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.
In java, we give body to an abstract method of parent class inside the child class and then call that function via child class object like:
//let the abstract function be fun then,
child c= new child();
c.fun();
then fun executes the body given by child but what's new in this as same being done in function overriding?
The difference is that subclasses that won't also be abstract must implement all inherited abstract methods. Also, the concrete implementation can't call super.method() as there's no implementation in the abstract class.
As to the why, or perhaps more explicitly, why choose the abstract superclass rather than an interface: often it's useful to provide implementations of only some methods of an interface - for instance for code-sharing - and require concrete subclasses to provide specific implementations for other parts of the interface.
The abstraction means that the developer has (explicit) to implement the method. Overriding means that the developer implement a method that's already implement. This is by definition of the languaje.
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.