This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt interface, abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt interface, abstract class" Watch "doubt interface, abstract class" New topic
Author

doubt interface, abstract class

Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
hi all,
abstract class A
{
public void print();
}
interface B
{
public void print();
}

class C extends A implements B
{
public void print()
{
System.out.println ("Hello world");
}
}

class c extends A and implements B, what version print() method does it impelement? (I mean, print() method of the class 'A' or interface 'B'?)


Manivannan
Nibin Jacob Panicker
Greenhorn

Joined: Aug 16, 2005
Posts: 22
hope the u forgot the write the abstract keyword for print() method in class A,otherwise it wont compile.

here,ur extending an abstract class A, so ur expected to override and defive print() method.since ur implementing the interface B also,u have to write code for the print() method declared in interface B.its interesting that both class A and interface B have a print() method with same signature.so to satisfy them u need to define only one print() method like

public void print(){
//some code.
}

in the class C.here both interface and abstract class will be taking the same print() method.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

The point u need to remember is ...

if there are methods with the same name and signature spread across the

interfaces or abstract class that u r extending...

the return type should be same across all the interfaces and the abstract class......


We can overload the methods in the normal way..


Regards

But no idea which of both the print methods is considered???
[ August 26, 2005: Message edited by: A Kumar ]
Nibin Jacob Panicker
Greenhorn

Joined: Aug 16, 2005
Posts: 22
hope the following code will give you some idea.

abstract class A{
abstract public void print();
}

interface B {
void print();
}

class C extends A implements B{
public void print(){
System.out.println("Inside Print");
}
}

public class TestClass{
public static void main(String args[]){
A a = new C();
a.print();
B b = new C();
b.print();
}
}

When run, it prints "Inside Print" twice which makes it clear that the print() method in class C satifies both the abstract class and the interface.
 
jQuery in Action, 2nd edition
 
subject: doubt interface, abstract class
 
Similar Threads
A class extending a class and implementing an interface with same methods ?
which method is called?
which one is called abstract or interface.....?
doubt regarding same method in interface and abstract class
a bug with a constructor