Page 1 of 1
[HD Pg 251, Sec. 9.2.5 - inheritance-of-multiple-versions-of-a-variable]
Posted: Sat Jun 08, 2019 6:45 am
by flex567
Based on example you provided can we say that the static variable of an interface is not inherited ?
Just like static methods.
Re: [HD Pg 251, Sec. 9.2.5 - inheritance-of-multiple-versions-of-a-variable]
Posted: Sat Jun 08, 2019 7:49 am
by admin
First, check out what is meant by inheritance as explained in 9.1.4 "Inheritance of instance members vs static members".
Second, the situation with fields of an interface is explained under section 9.2.5 under paragraph that you mentioned. It says, "Fields of an interface are inherited by a sub class and therefore Process does get two versions of SIZE variable.".
and then again in 9.2.6 under para titled, "Inheriting multiple variables with same name". It says, "Static fields of an interface are inherited by a sub interface and therefore ReadWritable does get two versions of SIZE variable."
Re: [HD Pg 251, Sec. 9.2.5 - inheritance-of-multiple-versions-of-a-variable]
Posted: Sat Jun 08, 2019 8:44 am
by flex567
I think I understood the concept with this program.
You can even delete this question.
Code: Select all
interface Task{
int SIZE = 10;
default void doIt(){
System.out.println("Doing Task");
}
static void wontWork() {};
}
interface Activity{
//long SIZE = 20;
//void doIt();
}
public class TestClass implements Task, Activity{
public static void main(String[] args){
TestClass p = new TestClass();
//p.doIt();
long l = p.SIZE; //inherited
long l1 = SIZE; //inherited
p.wontWork(); //cannot find symbol
Task.wontWork(); //works
}
}