Results 1 to 5 of 5
Thread: why interface is most required
- 02-04-2010, 08:29 AM #1
why interface is most required
Hi ,
i want to know why java dosn't support mutliple inheritence.
i will explin by a code
public class Main {
public static void main(String[] args) {
shape circleshape=new circle();
circleshape.Draw();
}}
interface shape
{ public String baseclass="shape";
public void Draw();
public void print();
}
class circle implements shape
{
public void Draw() {
System.out.println("Drawing Circle here");
}
public void print(){}
}
this code got from by googling
in the main class if i wrote
circle circleshape=new circle();
circleshape.Draw();
and
class circle // implements shape (comment the imple)
then i will get the wanted out put ,then why the fu..k interface (because every interviewr will ask about interface)is using we can easy call the same by circle circleshape=new circle();
OR the code change to
public class Main {
public static void main(String[] args) {
shape circleshape=new circle();
circleshape.Draw(); }
}
/*interface shape
{
public String baseclass="shape";
public void Draw();
public void print();
}*/
class circle extends shape//implements shape
{
public void Draw() {
System.out.println("Drawing Circle here);
}
public void print(){}
}
class shape {
public void Draw() {
System.out.println("Drawing");
}
}
and also we don't want to add the print() in the shape class as in interface
Then what is mutiple inhertence means i dont want definition that is every where as a saying that "water water every where but not even a drop to drink".A eg:code which shows the problem in multiple interface and why that lead to interface (keyword) and then how it solved by interface,
another thing is what is abstarct class the same for abstarct class also
Thanks and regards
javastudent
- 02-04-2010, 08:34 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Because the designers decided to leave it out. Nearly every developer has their own idea as to why that was a good or bad decision, but those are opinions. If you want to find out why then Google for James Gosling's blog and ask there. Not that I expect he would answer you.
- 02-04-2010, 09:33 AM #3
thanks for reply
hi thanks for reply
but i didnt get my query solves means the programe
Why not multiple inheritance? - JavaWorld
- 02-04-2010, 09:54 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You use the interface to remain "generic". In the manner in which it was used in the example, however, the call to draw can also be contained within another method of yours, and if you then restrict the method to only accepting a Circle, then you have to overload it multiple times to be able to do the same things with other shapes I.E.
P.S. All of this, of course, has nothing to do with your title, ipso facto, nothing to do with multiple inheritance.Java Code:public void doSomething(Shape s) { // perform some generic action(s) s.draw(); } // instead of having to do public void doSomething(Square s) { // perform some action(s) s.draw(); } public void doSomething(Circle c) { // perform the same action(s) as by square c.draw(); } public void doSomething(Rectangle r) { // perform the same action(s) as by square r.draw(); } public void doSomething(Trapeziod t) { // perform the same action(s) as by square t.draw(); }
And, yes, you can use an abstract class as well, but that only makes sense if all possible shapes also have common implementation features, rather than just interface features.Last edited by masijade; 02-04-2010 at 09:57 AM.
- 02-06-2010, 09:26 AM #5
Why doesn't Java support multiple inheritance?
In multiple inheritance a class has more than one parent class. For example, how do you categorize a musical production that is both a play and a concert? In C++ you can define a class Musical that is derived from both Play and Concert. Multiple inheritance has inherent complexities.
In Java you must do more analysis and decide which characteristics require an abstract data type and which are pure behavior. You may have to add a layer of abstraction. For example, if you consider that plays and concerts are both theatre attractions but have different behaviors, you can create Java interfaces for Play and Concert. Then the class Musical can extend TheatreAttraction and implement both Play and Concert. The declaration could be:
class Musical extends TheatreAttraction implements Play, Concert { /* ... */}
Similar Threads
-
what is required for EJB
By mmc18 in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-15-2008, 06:16 PM -
Blx.tld required
By anki1234 in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 01-24-2008, 04:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks