Results 1 to 3 of 3
  1. #1
    socboy6579 is offline Member
    Join Date
    Oct 2010
    Posts
    81
    Rep Power
    0

    Default Inheritance in Java

    When i try to build and run this file on JCreator, I get

    class rectangle is public, should be declared in a file named Rectangle.java

    this is the code
    Java Code:
    public class Dimensions 
    {
    	int length;
    	int width;
    	int height;
    }
    {
    	int a;
    	int volume;
    	int area()
    	{
    		a = length * width;
    	}
    	
    	{
    		volume = length * width * height;
    	}
    
        
    
    	public static class Area
    	{
    		public static void main(String[] args)
    		{
    			Rectangle Rect = new Rectangle();
    			Rect.length = 7;
    			Rect.width = 11;
    			Rect.height = 5;
    			Rect.area();
    			System.out.println("The area of a rectangle with a length of " +Rect.length + "and width " +Rect.width + "is " + Rect.a);
    			System.out.println("The volume of a rectangle with a length of " +Rect.length + "and width " +Rect.width + "and height of " +Rect.height + "is " + Rect.volume);
    		}
    	}
    }
    Im trying to use inheritance to run this program, and print the volume and the area of the certain dimensions for a rectangle. I dont know how to fix this error.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    You need create class Rectangle, ant it need extends Dimensions. Well The compiler don't know what is object Rectangle.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    in eclipse i got an error like "Rectangle cannot be resolved to a type" because the class Rectangle is not found. as petr already said you need to create a class Rectangle with the needed members and methods or changing the statement to Dimensions Rect = new Dimensions(); would also work.
    Last edited by j2me64; 02-08-2011 at 04:43 PM.

Similar Threads

  1. Help with java gui and inheritance
    By sssss in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2011, 04:38 AM
  2. Replies: 4
    Last Post: 07-22-2010, 11:22 AM
  3. Inheritance example
    By kris4u4ever in forum New To Java
    Replies: 3
    Last Post: 03-21-2009, 02:53 PM
  4. multiple inheritance in java
    By pawanspace in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 04:08 AM
  5. Multiple Inheritance in java
    By paty in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 02:25 PM

Posting Permissions

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