Results 1 to 4 of 4
  1. #1
    kris4u4ever is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default 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();

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    cuboid is a method in the Rectangle class. You cannot initiate a method.

    Java Code:
    cuboid cb = new cuboid();
    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    cuboid is an inner class and such needs to be called on a rectangle instance. This will 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");
      }
    }
    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.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. inheritance
    By itaipee in forum New To Java
    Replies: 6
    Last Post: 01-20-2009, 08:18 PM
  2. aggrigation and inheritance
    By ramakrishna.tata in forum New To Java
    Replies: 6
    Last Post: 07-08-2008, 08:11 AM
  3. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 06:08 PM
  4. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12:54 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
  •