Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Little help with "extends" please

  1. #1
    Junior Member uzisuicide's Avatar
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Little help with "extends" please

    I have to extend a class from squareDemo in this assignment. All i need to do is add depth to the compute surface area arithmetic. It wont allow me to print in the extended class. I also cant grab height, width from the parent class which I "thought" was the purpose of inheritance. Please help with code and explain how my logic is flawed. I'm a new programmer so please go easy on me heh...

    package square;
     
    /**
     *
     * @author Michael
     * Chapter 9 exercise 2
     */
    public class SquareDemo
    {
        public static void main(String[] args)
        {
            int height = 6;
            int width = 6;
            int surfaceAreaSQ = height * width;
     
            System.out.println("The surface area of the square is: " + surfaceAreaSQ);
        }
     
        public class CubeDemo extends SquareDemo
        {
            int height = 6;
            int width = 6;
            int depth = 6;
            int surfaceAreaCU = height * width * depth;
     
            System.out.println("The surface area of the cube is: " + surfaceAreaCU);
     
     
        }
     
    }
    Last edited by Json; April 27th, 2010 at 08:43 AM. Reason: Please use code tags.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Little help with "extends" please

    Hello there,

    The problem you have is that you've not actually declared any instance members in the SquareDemo class, therefore your CubeDemo will not be able to see any.

    You also need to take into account the access modifiers, if you mark members as private thats what they will be. If you want an inheriting class to have direct access to the superclass members you need to make the members protected.

    If you had protected instance members in the SquareDemo class your CubeDemo class would then be able to see them.

    I will see if I can dig up some sort of inheritance snippet, we should have something available.

    // Json

  3. #3
    Junior Member uzisuicide's Avatar
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Little help with "extends" please

    Okay scratch the last code. This code actually looks like it's going to work. I'm getting one error in the main. non-static variable cannot be used in static context. It's odd because the few lines above the error is basically doing the same thing without error. Check this out please.
    /**
     *
     * @author Michael Bowers
     * Chapter 9 - Assignment 2
     */
    public class Square
    {
        int height;
        int width;
        int surfaceArea;
     
    //********** CONSTRUCTOR Square **********
        public Square(int h, int w)
        {
            height = h;
            width = w;
        }//___________ END Constructor ________
     
        public void DisplaySquare()
        {
            System.out.println("The surface area of the square is: " + surfaceArea);
        }
     
        public void computeSurfaceArea()
        {
           surfaceArea = height*width;
        }
     
    //********** Extends Square **********
        public class Cube extends Square
        {
            int depth;
     
     //********** CONSTRUCTOR Cube **********       
            public Cube( int h, int w, int d)
            {
              super(h, w);
              depth = d;
            }//_______ END Constructor _______
     
            public void computeSurfaceArea()
            {
                super.computeSurfaceArea();
                surfaceArea = surfaceArea * depth;
            }
     
        }//_____ END Class Cube ______
     
        public void DisplayCube()
        {
            System.out.println("The surface area of the cube is: " + surfaceArea);
        }
     
     
        public static void main(String[] args)
        {
            Square square1 = new Square(6, 6);
            square1.computeSurfaceArea();
            square1.DisplaySquare();
     
            Cube cube1 = new Cube(6, 6, 6);
            cube1.computeSurfaceArea();
            cube1.DisplayCube();
        }
     
    }
    Last edited by helloworld922; April 28th, 2010 at 08:32 PM.

  4. #4
    Junior Member uzisuicide's Avatar
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Little help with "extends" please

    Fixed - disregard

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Little help with "extends" please

    please surround your code with [code] tags please

    Also, if your problem is solved please mark the thread as [solved]

  6. #6
    Junior Member uzisuicide's Avatar
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Little help with "extends" please

    if (problem == solved)
         select Solved;
    else
         don't;
     
    // I don't know where the solved button resides.
    Fear is the mind killer.

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Little help with "extends" please

    Quote Originally Posted by uzisuicide View Post
    if (problem == solved)
         select Solved;
    else
         don't;
     
    // I don't know where the solved button resides.
    The 'Mark Thread As Solved' button is in Thread Tools at the top of the forums.

    I have done it for you this time
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. The Following User Says Thank You to JavaPF For This Useful Post:

    uzisuicide (April 29th, 2010)

Similar Threads

  1. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  2. Replies: 1
    Last Post: March 15th, 2010, 10:03 PM
  3. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  4. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM