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

Thread: Java Inheritance Help

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java Inheritance Help

    Got to create a small presentation on java inheritance, got some basic information about it but need to include example programs and talk about how they work. Iv got a small program below which is supposed to output an oblong but it doesnt for some reason.

    public class oblong 
    {
    	private double length;
    	private double height;
     
    	public oblong(double lengthIn, double heightIn)
    	{
    		length = lengthIn;
    		height = heightIn;
    	}
     
    	public double getlength()
    	{
    		return length;
    	}
     
    	public double getheight()
    	{
    		return height;
    	}
     
    	public void setlength(double lengthIn)
    	{
    		length = lengthIn;
    	}
     
    	public void setheight(double heightIn)
    	{
    		height = heightIn;
    	}
     
    	public double calculateArea()
    	{
    		return length * height;
    	}
     
    	public double calculatePerimeter()
    	{
    		return 2 * (length + height);
    	}

    public class ExtendedOblong extends oblong
    {
    	private char symbol;
    	public ExtendedOblong(double lengthIn, double heightIn, char symbolIn)
    	{
    		super(lengthIn, heightIn);
    		symbol = symbolIn;
    	}
     
    	public void setSymbol(char symbolIn)
    	{
    		symbol = symbolIn;
    	}
     
    	public String draw()
    	{
    		String s = new String();
    		int l, h;
    			l = (int) getlength();
    			h = (int) getheight();
    			for (int i = l; i <= h; i++)
    			{
    				for (int j = l; j <= l; j++)
    				{
    					s = s + symbol;
    				}
    				s = s + '\n';
    			}
    			return s;
    	}
    	public static void main(String[] args) 
    	{
    		ExtendedOblong extOblong = new ExtendedOblong(10,5,'*');
    		System.out.println(extOblong.draw());
    		extOblong.setSymbol('+');
    		System.out.println(extOblong.draw());
     
     
    	}
    }

    Does anyone know why this code will not output an oblong, and also if anyone has any example programs of java inheritance or any information about it can you please post it.

    Many Thanks.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java Inheritance Help

    for (int i = l; i <= h; i++)

    For the above loop, what will happen if l > h?

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Inheritance Help

    nothing, for some reason it wont run. Soon as I click the run button the program just terminates and nothing comes up.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java Inheritance Help

    nothing
    Indeed...Step 1, re-read my last post - it uses an example that points out the problem. Second step if step 1 doesn't help, look at your code and step through - write it out on paper if you have to - with the values you use to instantiate ExtendedOblong. Third step if the previous step doesn't work: add some println's in there to see how your loops are behaving, backtrack through the code with debug statements (see my blog for debugging with println's)

Similar Threads

  1. Inheritance and Overriding help!
    By Knserbrave in forum Object Oriented Programming
    Replies: 4
    Last Post: February 24th, 2011, 01:46 PM
  2. Inheritance questions....
    By smellyhole85 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 06:11 PM
  3. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  4. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM
  5. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM