Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please tell me the difference between these two code example

 
SachinSunny Jain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maki Jav
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes in both cases class B has to implement method of interface I.

Maki Jav
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic