This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface type object type relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interface type object type relationship" Watch "interface type object type relationship" New topic
Author

interface type object type relationship

Abhishek KumarSoni
Ranch Hand

Joined: Nov 05, 2013
Posts: 61

i define an interface say "diskable "as follow .
interface diskable
{
public void show();
public boolean equals(Object());
}
here show method is its own method and in front of equals method it add override notation which mean this method is overided from java.lang.Object. How this can be possible when an interface can not extend any class how it can override its method. Intersting thing is if i define "interface diskable extends Object" it shows expected error(interface expected) then why this is saying equals method is overrided from java.lang.Objecct
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Abhishek,
Welcome to CodeRanch!

You can't write interface diskable extends Object because Object is a class. Interfaces can only extend other interfaces.

That said, it still makes sense for @Override to be there. Think about this from the point of view of a class that implements diskable. That class needs to implement equals() becuase it is in the interface. And for that class, the method IS overridden from Object.

Note that in Java, convention is for class and interface names to begin with uppercase. Consider writing Diskable to develop good habits.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Abhishek KumarSoni
Ranch Hand

Joined: Nov 05, 2013
Posts: 61

Jeanne Boyarsky wrote:Abhishek,
Thank you Boyarsky for your suggestioon i will take care of naming convention from now but i still did not get your point.. when An interface can not extends Object then what is the sense overriding of method from that class.
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 275

Actually You are getting wrong impression that diskable interface is extending Object

There is an abstract in the equals() method declaration , I think you are getting an impression that this is same as Object class`s equals(Object a) method.



Actually you are declaring an abstract method named equals that have nothing to do with Object class it just happens to be having the same name nothing else other than that.

And your second question that following declaration gives error

interface diskable extends Object
{}

This is because an interface can not extends a class. Remember one thing an interface is not an Object but a class is an Object.

class diskable extends Object{} this will will be OK and you know that.

So key things to take from this question are
1. An interface can not extend a class
2. An interface is not an Object but a class is(any class abstract or non abstract)


Oracle Java Programmer , Oracle SQL Expert , Oracle Java Web Component Developer
 
GeeCON Prague 2014
 
subject: interface type object type relationship