Results 1 to 5 of 5
  1. #1
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    157
    Rep Power
    5

    Default 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

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by javastuden View Post
    i want to know why java dosn't support mutliple inheritence.
    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.

  3. #3
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    157
    Rep Power
    5

    Default thanks for reply

    hi thanks for reply
    but i didnt get my query solves means the programe

    Why not multiple inheritance? - JavaWorld

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    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.

    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();
    }
    P.S. All of this, of course, has nothing to do with your title, ipso facto, nothing to do with multiple inheritance.

    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.

  5. #5
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    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

  1. what is required for EJB
    By mmc18 in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 06:16 PM
  2. Blx.tld required
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 01-24-2008, 04:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •