Case 1:
I have an Interface Inter.java:
interface Inter
{
void abcd();
}
A base class:
class Base
{
void abcd()
{
System.out.println("Base abcd");
}
}
A Derived class:
class Use extends Base implements Inter
{
void abcd() //intentionally not using public
{
System.out.println("Use abcd");
}
public static void main(String[] args)
{
Use u = new Use();
u.abcd();
}
}
Now I compile it:
gyan@#ns:~/Desktop$ javac Inter.java Base.java Use.java
Use.java:3: error: abcd() in Use cannot implement abcd() in Inter
void abcd()
^
attempting to assign weaker access privileges; was public
1 error
gyan@#ns:~/Desktop$
That means the overridden method abcd() in the class Use is from the implemented interface "Inter" and not from the base class "Base".
Case 2:
We have a file Test.java containing following codes:
interface Ab
{
void run();
}
class A extends Thread implements Ab
{
public void run()
{
System.out.println("class A");
}
public static void main(String[] args)
{
A a = new A();
Thread t = new Thread(a);
t.start();
System.out.println("main method");
}
}
When we execute it we got:
gyan@#ns:~/Desktop$ java A
main method
class A
gyan@#ns:~/Desktop$
Since t.start() executed the method run(), that means method run() of the class Thread got overridden. But in the case 1 method abcd() of the interface "Inter" got overridden.
In case 1: Whose method abcd() is overridden in class Use? Class Base or interface Inter? Error says we are overriding the abcd() of interface Inter. But in case 2: It seems that we are not overriding the method run of interface Ab. Because we can execute the run() method by calling t.start(). Which is only possible if we are overriding the run() of class Thread.