Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Please tell me the difference between these two code example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please tell me the difference between these two code example" Watch "Please tell me the difference between these two code example" New topic
Author

Please tell me the difference between these two code example

SachinSunny Jain
Greenhorn

Joined: Jun 23, 2011
Posts: 2
Public interface I{

Method1();

Method2();

}



Public abstract class A implements I {

}



Class B extends A implements I{

}




Example 2:



Public interface I{

Method1();

Method2();

}



Public abstract class A implements I {

}



Class B extends A {

}


what will be the difference between the functionality of Class B in
example 1 as compared to that of Class B in Example 2?


Many Thanks in Advance
Sachin
Harnoor Singh
Ranch Hand

Joined: Aug 24, 2010
Posts: 35
Welcome to JavaRanch.

Class B in the first case need to provide the implementation of methods present in I interface. There is no such restriction of the second B class.
SachinSunny Jain
Greenhorn

Joined: Jun 23, 2011
Posts: 2
Thanks!!!

But in example 1 as Class A has already implemented the methods of I then B need not provide an implementation as they will be inherited from A

Correct me if I am wrong!!!

Many Thanks
Sachin
Harnoor Singh
Ranch Hand

Joined: Aug 24, 2010
Posts: 35
I am not able to understand your doubt. Code is also vague.

1) Let us assume that in first example class A has implemented all the methods of the interface I. Now since B is subclass of A, do we really need to implement interface I again ? Regarding your doubt in case class A has implemented all the methods of I, we dont need to implement them in B.
2) In the case when Class A has implemented only one method of interface I , we need to define class A as abstract. If class B extends A and implement I , class B can be concrete only if we define second method of I.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Because class A implements interface I and class B extends class A, class B already implicitly implements interface I. You can leave out the implements clause. In fact, Eclipse can warn you about unnecessary implements clauses.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
SachinSunny Jain wrote:
But in example 1 as Class A has already implemented the methods of I then B need not provide an implementation as they will be inherited from A

Correct me if I am wrong!!!



Yes you are right.
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
Sachin Sunney !

There is no difference As much I understand your code. In both the cases your class B has to implement both the methods

Regards
Shalindra Singh
SCJP, SCWCD
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
Yes in both cases class B has to implement method of interface I.

Maki Jav


Help gets you when you need it!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Please tell me the difference between these two code example
 
Similar Threads
interface and concrete classes
Question on "instanceof"
abstract interface
Does run() method is declard i Thread?
Basic Question about extending/implementing