Results 1 to 12 of 12
Like Tree3Likes
  • 2 Post By PhHein
  • 1 Post By gimbal2

Thread: Problem with Inheritance

  1. #1
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Problem with Inheritance

    Hi All,

    I am learning Java and was trying to learn Class inheritance.

    Class Heiarchy is as such:

    SuperClassEx
    >> feilds >> length ,breadth
    >> Methods > area

    SubClassEx
    >> feilds >height + superClass feilds
    >>Method >> Volume

    DemoCInheritance class
    Contains main and let me see how inheritance work

    But when I run this program I am getting error:

    C:\Java Tuts\Complete Java Ref\Inheritance>javac DemoInheritance.java
    .\SubClassEx.java:8: error: constructor SuperClassEx in class SuperClassEx cannot be applied to given types;
    {
    ^
    required: int,int
    found: no arguments
    reason: actual and formal argument lists differ in length
    1 error
    My codes are :

    Java Code:
    class SuperClassEx
    {
    //Instance Variable 
    int length; //length
    int breadth; //breadth
    
    //Constructor>>Overloading (Y/N) >> N
    SuperClassEx(int p, int q )
    {
    length=p;
    breadth=q;
    }
    
    //Methods>>
    int area()
    {
    return length*breadth;
    }//Area-method end
    
    }//Class end
    Java Code:
    class SubClassEx extends SuperClassEx
    {
    //feilds
    int height;
    
    //Constructor
    SubClassEx(int p,int q,int r)
    {
    length=p;
    breadth=q;
    height=r;
    }//Constructor end
    
    //
    int volume()
    {
    return length*breadth*height;
    }//volume method end
    
    }
    Java Code:
    class DemoInheritance
    {
    public static void main(String[] args)
    {
    //object decleration
    SuperClassEx supObj= new SuperClassEx(2,4);
    SubClassEx subObj= new SubClassEx(3,5,6);
    
    
    //Business logics
    System.out.println("Area: "+ supObj.area());
    System.out.println("Volume: "+ subObj.volume());
    
    }//main end
    }//Class end


    Thank you everyone for helping

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,894
    Rep Power
    5

    Default Re: Problem with Inheritance

    Yes you get an error, because your code is wrong and the error explains crystal clear what is wrong.

    Hint: you are missing a usage of the 'super' keyword somewhere.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Problem with Inheritance

    Quote Originally Posted by gimbal2 View Post
    Yes you get an error, because your code is wrong and the error explains crystal clear what is wrong.

    Hint: you are missing a usage of the 'super' keyword somewhere.
    Yeah Super key word can be used, but I have followed an example from book >> Complete reference for Java ,Herbert Schidt, which uses same logic as I used , but that works.

    I wanted to know why my logic is not working or what is wrong with my code.

    The code form the book is below:

    // This program uses inheritance to extend Box.
    class Box {
    double width;
    double height;
    double depth;
    // construct clone of an object
    Box(Box ob) { // pass object to constructor
    width = ob.width;
    height = ob.height;
    depth = ob.depth;
    }
    // constructor used when all dimensions specified
    Box(double w, double h, double d) {
    width = w;
    height = h;
    depth = d;
    }
    // constructor used when no dimensions specified
    Box() {
    width = -1; // use -1 to indicate
    height = -1; // an uninitialized
    depth = -1; // box
    }
    // constructor used when cube is created
    Box(double len) {
    width = height = depth = len;
    }
    // compute and return volume
    double volume() {
    return width * height * depth;
    }
    }
    // Here, Box is extended to include weight.
    class BoxWeight extends Box {
    double weight; // weight of box

    // constructor for BoxWeight
    BoxWeight(double w, double h, double d, double m) {
    width = w;
    height = h;
    depth = d;
    weight = m;
    }
    }
    class DemoBoxWeight {
    public static void main(String args[]) {
    BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
    BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
    double vol;
    vol = mybox1.volume();
    System.out.println("Volume of mybox1 is " + vol);
    System.out.println("Weight of mybox1 is " + mybox1.weight);
    System.out.println();
    vol = mybox2.volume();
    System.out.println("Volume of mybox2 is " + vol);
    System.out.println("Weight of mybox2 is " + mybox2.weight);
    }
    }

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Problem with Inheritance

    That's the difference between a good Java book and a Schildt book ;)
    In the book's example the super class has a default constructor which is implicitly called (of course without the book mentioneing it, or you haven't read carefully).

    You should explicitly call super(.....) in your subclass' constructor.
    John2find and JosAH like this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,894
    Rep Power
    5

    Default Re: Problem with Inheritance

    Java Code:
    // constructor used when no dimensions specified
    Box() {
    That's the difference between your code and this example; the example in the book has a 'default constructor' with no arguments.

    edit: ninja'd!

    That's the difference between a good Java book and a Schildt book ;)
    Do I read that as "Schildt books suck by definition"?
    John2find likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem with Inheritance

    That's a dreadful example of inheritance...but in any case Schildt has a no argument constructor for Box, which you do not.

    Dear god...the more I look at that the more my eyes bleed.
    Can I recommend not using that book?

    ETA: And how long exactly did I have this page open before replying?
    Even a tortoise could have ninja'ed me on that delay.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Problem with Inheritance

    Thank you guys for replying.

    SO do it mean that its mandate to use Super or either a no variable constructor in this case ?
    And their is no other reason why I am getting this error ?

  8. #8
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Problem with Inheritance

    Your replies helped me do some research & I came across this statement:

    Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

    Using the Keyword super (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

    Thank you all for your time and effort.

    And my program is working fine with modification in SubClass code:

    class SubClassEx extends SuperClassEx
    {
    //feilds
    int height;

    //Constructor
    SubClassEx(int p,int q,int r)
    {
    super(p,q);
    height=r;
    }//Constructor end

    //
    int volume()
    {
    return length*breadth*height;
    }//volume method end

    }

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Problem with Inheritance

    Quote Originally Posted by gimbal2 View Post
    Do I read that as "Schildt books suck by definition"?
    More or less, yes. I haven't seen one that didn't suck.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,894
    Rep Power
    5

    Default Re: Problem with Inheritance

    Noted. Boy the list of no-no books keeps growing.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem with Inheritance

    This is the classic list of mistakes in Schildt's C++ reference.

    And from what I've heard the Java one is really no better, though there doesn't seem to have been the same point by point listing of them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem with Inheritance

    Quote Originally Posted by Tolls View Post
    This is the classic list of mistakes in Schildt's C++ reference.

    And from what I've heard the Java one is really no better, though there doesn't seem to have been the same point by point listing of them.
    A book by Herbert Schildt is only good in the fire place; those books are a waste of trees.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Inheritance problem
    By paquizzle in forum New To Java
    Replies: 1
    Last Post: 08-18-2012, 09:12 PM
  2. Inheritance Problem
    By kazumahits in forum New To Java
    Replies: 5
    Last Post: 01-11-2011, 03:46 PM
  3. Inheritance Problem
    By g2beastie in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 08:23 PM
  4. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 06:01 PM
  5. Problem with Inheritance
    By KronikAlkoholik in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 12:13 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
  •