Reputation: 485
For example, I have an interface with 4 methods.
If I implement this interface incomplete in a class, the class must be abstract. Right?
For example, I leave one method out. So now I am writing a subclass which extends this class. Now I implement the last method of the interface.
What happens, if I call this method in the abstract superclass? Nothing! It works. But why?
What will happen, if I write several classes, extending this abstract class and implement the fourth method of the interface? Which one will be called?
Upvotes: 4
Views: 2051
Reputation: 51721
What happens, if I call this method in the abstract superclass? Nothing! It works. But why?
You cannot invoke this method on the abstract
superclass because it can't be instantiated. You would only be invoking it on a sub-class of this abstract super-class which would be forced to provide an implementation (or be declared abstract itself).
So, when you invoke this method, its the implementation provided by the non-abstract sub-class that gets executed.
The funny thing is that a method of the class 'AndroidGame' calls 'getStartScreen()'. But there could be several classes like 'MrNomGame' which extending 'AndroidGame'. Then which method would be executed?
The method code executed would depend on the runtime type of the actual object that you invoke this method on. So, you could have a code like this
AndroidGame game1 = new MrNomGame();
AndroidGame game2 = new SomeOtherGame();
game1.getStartScreen(); // invokes MrNomGame's version
game2.getStartScreen(); // invokes SomeOtherGame's version
and due to dynamic binding the JVM would invoke the getStartScreen()
as implemented by the class type of the actual object that the reference points to.
Upvotes: 0
Reputation: 14413
A interface is a contract you define the signature of your methods, only behaviour and constants, all methods are public and abstract.
In an Abstract Class
you define behaviour and state, you can have some implementation and abstract methods.
For example for your question:
If I implement this interface incomplete in a class, the class must be abstract. Right?
Right
For example, I leave one method out. So now I am writing a subclass which extends this class. Now I implement the last method of the interface.
What happens, if I call this method in the abstract superclass? Nothing! It works. But why?
Will run cause in runtime execution knows what class is. this is polymorphism.
What will happen, if I write several classes, extending this abstract class and implement the fourth method of the interface. Which one will be called?
The one you instanciate in your client code :D
public interface Operation{
void operation1();
void operation2();
}
I don't implement operation2
public abstract class ClaseBase implements Operation{
//with final im saying childs wont override
public final void operation1{
// do something
operation2();
// do something
}
}
//have to implements operation2 cause it's a concrete class
public class Child extends ClaseBase{
void operation2(){
System.out.println("Something");
}
}
You can't instanciate an AbstractClass.
In your client code
ClaseBase base = new Child();
base.operation1(); // and it's gonna to call operation2 in childClass
Abstract class is useful with Template Method
pattern.
Upvotes: 2
Reputation: 34387
Keep in mind that when you instantiate a subclass and refer it as super/abstract class or interface, it's still the instance of the subclass. Any method called on the object bubbles up from subclass to super class if its not available in the sub class.
Thus if you have:
interface GemeInterface
getStartScreen()
getCloseScreen()
abstract class AndroidGame implement GemeInterface
getCloseScreen()
class MrNomGame extends AndroidGame
getStartScreen()
class MrPetNomGame extends AndroidGame
getStartScreen()
and using as
//You can't instantiate AndroidGame hence the it has to be instance of a subclass
AndroidGame androidGame1 = new MrNomGame ();
androidGame1.getStartScreen();
//You can't instantiate AndroidGame hence the it has to be instance of a subclass
AndroidGame androidGame2 = new MrPetNomGame ();
androidGame2.getStartScreen();
Then also it works as androidGame1.getStartScreen() --> calls the method from MrNomGame, while androidGame2.getStartScreen() --> calls the method from MrPetNomGame.
Both the calls i.e. androidGame1.getCloseScreen()
and androidGame2.getCloseScreen()
will end up calling the method from AndroidGame
as it's not available in the sub classes.
Upvotes: 0
Reputation: 1967
It depends on the object that you created. Super class can refer to subclass objects. So the actual object's method would be called.
interface A{
public void test1();
public void test2();
}
public abstract class B implements A{
public void test1(){
}
public abstract public void test2();
}
public class C extends B{
public void test2(){
}
}
B b = new C();
b.test2();//This works because the object that is refered by B is actually an object of C
Infact you can also do:
A a = new C();
a.test1();
a.test2();
Again though A is an interface(super type), but it is actually referring to a concrete implementation C(subtype) object and hence this works.
So at the compile time, compiler checks if A/B has a method called test2(), if yes compile is happy and it compiles successfully. But at the runtime, you invoke test2() method on the object and object actually is of class C which has the complete implementation.
Upvotes: 0