Results 1 to 4 of 4
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Problem when Overloading Classes

    Okay, so I tried to overload my class, and it is fairly simple. I made the methods, called the variables etc.

    But when I run it, it gives me 0 for the product (or area) of the length * width.

    Java Code:
    public class AreaRect {
    	int length;
    	int width;
    	
    	AreaRect(){
    		int length = 7;
    		int width = 5;
    	}
    	AreaRect(int l, int w){
    		int length = l;
    		int width = w;
    	}
    	
    	int getArea(){
    		return length * width;
    	}
    	
    	
    	
    	public static void main(String [] args){
    		AreaRect RectObj1 , RectObj2;
    		RectObj1 = new AreaRect();
    		RectObj2 = new AreaRect(10, 20);
    		
    		System.out.println("Area of rectangle 1 is:" + RectObj1.getArea());
    		System.out.println("Area of rectangle 2 is:" + RectObj2.getArea());
    	}
    }
    In the first AreaRect I initialize the variables and I thought it should work. Second I just call them, but initialize them later in my object creation.

    Can someone help me get the correct answers (35 and 200)?

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

    Default Re: Problem when Overloading Classes

    You're re-declaring variables in your constructors which will "shadow" the class variables. Don't do this, but instead use the class variables in the constructors.

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Problem when Overloading Classes

    Okay, do I really need to even declare my variables in AreaRect(1)?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem when Overloading Classes

    Quote Originally Posted by Army View Post
    Okay, so I tried to overload my class, and it is fairly simple.
    You mean extend a class? You can't overload a class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Function Overloading
    By ankiit in forum New To Java
    Replies: 7
    Last Post: 08-20-2012, 08:43 PM
  2. Overloading or Overriding...
    By chathura87 in forum New To Java
    Replies: 5
    Last Post: 02-22-2011, 01:46 AM
  3. overloading
    By Bhuvan in forum Advanced Java
    Replies: 6
    Last Post: 01-02-2011, 09:15 AM
  4. Overloading qn
    By diskhub in forum New To Java
    Replies: 18
    Last Post: 06-07-2010, 05:19 PM
  5. Overloading?
    By padutch2 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:26 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
  •