• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

interfaces-method overriding

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 23
interface Z {void m1();} // 1
class A implements Z {void m1() {}} // 2
class B implements Z {public void m1() {}} // 3
abstract class C implements Z {public abstract void m1();} // 4

A Compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above
-------------------------------------------------------------------
This is from Dan's mock exams- Interfaces-Q23
the answer is given as 2.
It is explained how all other lines are free of compilation error but i dint understand why line 2 causes a compilation error!!
Please help me understand this..
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 2 it implements a default (or package) visible method. But in the interface it is implicitly declared as publicly visible. You cannot "tighten" the visibility of method when you define it in the implementing class.
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes!! Thanks..I was getting confused as there are many questions where interfaces are extending other interfaces..Here a class is IMPLEMENTING the interface..so it's DIFFERENT. Thanks for the quick help
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In line 2 it implements a default (or package) visible method. But in the interface it is implicitly declared as publicly visible. You cannot "tighten" the visibility of method when you define it in the implementing class.



Please tell me how in the interface it is implicitly declared as public.

Well, I have not read such type of explanation earlier about interfaces. Could you give me some link which define this.

Thanks
Kaps
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kaps, from Mughal and Rasmussen, SCJP 1.4 2nd Edition (6.4 Interfaces, p 261):
From last but one paragraph:

Since interfaces are meant to be implemented by classes, interface members implicitly have
public accessibility and the public modifier is omitted.



See also Java Language Specification Section 6.6.1:

All members of interfaces are implicitly public


[ August 21, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think an under-talked-about utility is javap.

I create an interface. MyInterface.java
I compile it. -> MyInterface.class
then I look at what the java compiler REALLY does to it:

javap MyInterface

It shows me that all methods are abstract and public

(javap (with flags) also helps me to see stuff like what constructors are being called in classes, etc).
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic