Results 1 to 4 of 4
Thread: Inheritance example
- 03-21-2009, 10:25 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 1
- Rep Power
- 0
Inheritance example
import java.io.*;
class rectangle
{
int l,b;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
rectangle()
{
try
{
System.out.println("Enter l & b values");
l = Integer.parseInt(br.readLine());
b = Integer.parseInt(br.readLine());
}
catch(Exception e)
{}
}
void area()
{
System.out.println("area of rectangle = "+(l*b));
}
class cuboid extends rectangle
{
int h;
cuboid()
{
h=5;
System.out.println("Volume of cuboid = "+(l*b*h));
}
}
}
class single
{
public static void main(String args[])
{
System.out.println("In");
cuboid cb = new cuboid();
System.out.println("out");
}
}
Hello, can any one help me with this
when i compile the code i am getting this error
single.java:36: cannot find symbol
symbol:class cuboid
location:class single
cuboid cb = new cuboid();
single.java:36: cannot find symbol
symbol:class cuboid
location:class single
cuboid cb = new cuboid();
- 03-21-2009, 10:42 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
cuboid is a method in the Rectangle class. You cannot initiate a method.
You never cannot do this. Do you want to call that method, is it? Since it's not a static member of the class, you have to call with the Rectangle class initialization.Java Code:cuboid cb = new cuboid();
-
cuboid is an inner class and such needs to be called on a rectangle instance. This will work:
but it's ugly and unwieldy. I recommend that you make it a stand-alone class, and pass it the proper parameters for it to work.Java Code:class single { public static void main(String args[]) { System.out.println("In"); rectangle.cuboid cb = new rectangle().new cuboid(); //cuboid cb = new cuboid(); System.out.println("out"); } }
- 03-21-2009, 02:53 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Oops, I've made a mistake there. I didn't see the class definition. Really sorry about that. Unformatted codes mess me up. Sorry again.
Similar Threads
-
inheritance
By itaipee in forum New To JavaReplies: 6Last Post: 01-20-2009, 08:18 PM -
aggrigation and inheritance
By ramakrishna.tata in forum New To JavaReplies: 6Last Post: 07-08-2008, 08:11 AM -
Inheritance
By mew in forum New To JavaReplies: 1Last Post: 12-07-2007, 06:08 PM -
Inheritance in GUI
By Marty in forum SWT / JFaceReplies: 2Last Post: 05-11-2007, 12:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks