[HD Pg 251, Sec. 9.2.5 - inheritance-of-multiple-versions-of-a-variable]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

[HD Pg 251, Sec. 9.2.5 - inheritance-of-multiple-versions-of-a-variable]

Post by flex567 »

Based on example you provided can we say that the static variable of an interface is not inherited ?
Just like static methods.

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 251, Sec. 9.2.5 - inheritance-of-multiple-versions-of-a-variable]

Post 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."
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: [HD Pg 251, Sec. 9.2.5 - inheritance-of-multiple-versions-of-a-variable]

Post 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

	}
}

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 22 guests