This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Super ref = new Sub(); Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Super ref = new Sub();" Watch "Super ref = new Sub();" New topic
Author

Super ref = new Sub();

pitambari parekh
Greenhorn

Joined: Jul 06, 2007
Posts: 15
class Employee implements Printable
{
public void print()
{
System.out.println("inside print");
}

public Employee()
{

}

public static void main(String[] args)
{
Printable p1 =new Employee();
Printable p2 =new Employee();
p1.print();
if(p1.equals(p2))
System.out.println("Equal");
System.out.println("Not Equal");
}
}
how is it possible to call "equals" method on Printable reference??? Because with super class/interface reference we can only call methods of super class/interface .. And "Object" class is not a super class for Printable.
Waiting for a reply...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
Welcome to the Ranch.

Not sure. There is another problem. The print() method in java.awt.print.Printable takes (Graphics, PageFormat) as parameters, and your print() method doesn't take any parameters, so you are overloading the method, not implementing the interface. You can't call print() from a Printable object. Unless you can get a Graphics object and a PageFormat object from somewhere (or dummy objects) you will never get that class to compile.

But when I tried to compile your class, it didn't say anything about equals().
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

There is explicit language in the Java Language Spec which states that the methods of Object are available on references of interface types.
[ July 06, 2007: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
Thank you, Mr Friedmann-Hill. That is why my compiler overlooked "equals". I made a mistake; I failed to notice that print needs an int parameter too.
pitambari parekh
Greenhorn

Joined: Jul 06, 2007
Posts: 15
Thank you, Mr Friedmann-Hill..
pitambari parekh
Greenhorn

Joined: Jul 06, 2007
Posts: 15
Thank You Mr. Campbell Ritchie .. But one thing i would like to clarify is that by using Printable I meant simply any interface and not specifically the interface from java.awt.print.Printable..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Super ref = new Sub();
 
Similar Threads
protected keyword
Marcus Green - Exam 2 - Question 9
Static method problem
nesting of if.. weird....
doubts from allover..... help me out