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 5 of 5

Thread: java - inheritance

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post java - inheritance

    following is the program i wanted to print the output of areas of both triangle and rectangle, but im not getting it.....please help me in getting the areas of both...thank you



    class Shape
    {
    int dim1;
    int dim2;

    Shape(int a,int B)
    {
    dim1=a;
    dim2=b;
    }
    void rotate()
    {
    System.out.println("rotating");
    }
    void erase()
    {
    System.out.println("erasing");
    }
    }

    class Rectangle extends Shape
    {
    Rectangle(int l,int B)
    {
    super(l, B);
    }
    int getArea()
    {
    return dim1*dim2;
    }
    }

    class Triangle extends Shape
    {
    Triangle(int b,int h)
    {
    super(b,h);
    }
    int getArea()
    {
    return(dim1*dim2)/2;
    }
    }

    class Run1
    {
    public static void main(String[] args)
    {
    Rectangle rect=new Rectangle(10,20);
    rect.getArea();
    rect.rotate();
    rect.erase();

    System.out.println("-----------------");
    Triangle t1=new Triangle(20,40);
    t1.getArea();
    t1.rotate();
    t1.erase();
    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: java - inheritance

    "i'm not getting it" provides zero information.

    Do you get errors when you compile or run your code? Then post the exact message here.
    Does the program not behave as expected? Then post what your input was, what incorrect output or behaviour you got and what you expected to happen instead.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: java - inheritance

    Hello,
    You are just not printing the areas. Add print statements.

    Syed.

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java - inheritance

    i wanted to print the areas of both... where to add those print statements?

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: java - inheritance

    Wherever you want the values to be displayed. It is your program you decide. However, after the area has been calculated would be a good place!
    Improving the world one idiot at a time!

Similar Threads

  1. java inheritance
    By farheen_zafar1 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 24th, 2013, 01:58 PM
  2. Java Inheritance problem
    By Grot in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2013, 07:34 PM
  3. [SOLVED] Java inheritance
    By maple1100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 9th, 2013, 10:42 PM
  4. Constructors and Inheritance in Java
    By diav in forum Object Oriented Programming
    Replies: 3
    Last Post: November 10th, 2011, 09:45 PM
  5. Java Inheritance Help
    By danielparry in forum Java Theory & Questions
    Replies: 3
    Last Post: March 17th, 2011, 03:20 PM