• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding interfaces and abstract classes example  RSS feed

 
JayWinna nguyen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, although this example definitely doesn't really go into interfaces and abstract classes, I just recently started learning about them and am facing this problem:
Code was provided by professor, but likely to be in HeadFirstJava or Effective Java.


the answer is the interface Gadget will not compile.

Can anyone offer an explaination to why this is so? Thanks!
 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first, the example code is missing a closing bracket ('}') at the end. Second the code will not compile, because Tablet implements doStuff() with the default access modifier. However all interface methods are implicitely public and abstract, and implemented or inherited methods cannot reduce visibility. The code would compile if you changed line 8 to
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is almost always given in the error codes: doStuff in Junk cannot implement doStuff in Gadget, attempting to assign weaker access privileges; was public.

In other words your access was not appropriate for the implementation in Junk.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JayWinna nguyen wrote:the answer is the interface Gadget will not compile.

Actually, that's not true.

If you were to take only the Gadget code and put it in its own file, it would compile just fine.

It's the Tablet class that won't compile - for all the reasons mentioned by my esteemed colleagues.

Winston
 
JayWinna nguyen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
JayWinna nguyen wrote:the answer is the interface Gadget will not compile.

Actually, that's not true.

If you were to take only the Gadget code and put it in its own file, it would compile just fine.

It's the Tablet class that won't compile - for all the reasons mentioned by my esteemed colleagues.

Winston



Woops! You are right. It's the Tablet class that won't compile. Thanks!
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!