Results 1 to 12 of 12
Thread: Problem with Inheritance
- 07-29-2013, 10:31 AM #1
Member
- Join Date
- Jul 2013
- Posts
- 14
- Rep Power
- 0
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
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
- 07-29-2013, 10:42 AM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 07-29-2013, 12:27 PM #3
Member
- Join Date
- Jul 2013
- Posts
- 14
- Rep Power
- 0
Re: Problem with Inheritance
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);
}
}
- 07-29-2013, 12:33 PM #4
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.Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-29-2013, 12:37 PM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Problem with Inheritance
Java Code:// constructor used when no dimensions specified Box() {
edit: ninja'd!
That's the difference between a good Java book and a Schildt book ;)"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 07-29-2013, 12:48 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 07-29-2013, 12:57 PM #7
Member
- Join Date
- Jul 2013
- Posts
- 14
- Rep Power
- 0
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 ?
- 07-29-2013, 01:16 PM #8
Member
- Join Date
- Jul 2013
- Posts
- 14
- Rep Power
- 0
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
}
- 07-29-2013, 01:21 PM #9
Re: Problem with Inheritance
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-29-2013, 01:38 PM #10
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Problem with Inheritance
Noted. Boy the list of no-no books keeps growing.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 07-29-2013, 01:57 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 07-29-2013, 03:02 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Inheritance problem
By paquizzle in forum New To JavaReplies: 1Last Post: 08-18-2012, 10:12 PM -
Inheritance Problem
By kazumahits in forum New To JavaReplies: 5Last Post: 01-11-2011, 04:46 PM -
Inheritance Problem
By g2beastie in forum New To JavaReplies: 4Last Post: 03-25-2010, 09:23 PM -
inheritance problem
By er1c550n20 in forum New To JavaReplies: 2Last Post: 03-10-2010, 07:01 PM -
Problem with Inheritance
By KronikAlkoholik in forum New To JavaReplies: 4Last Post: 08-25-2009, 01:13 AM
Bookmarks