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

Thread: Small problem regarding inheritance of classes

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Small problem regarding inheritance of classes

    Hey, just moved onto inheritance in college and was working through our first exercise sheet when I got this problem. For the exercise, we have to write a class encapsulating the notion of an employee, then make a Tradesman class by inheritance from Employee and finally, a class Staff to encapsulate the notion of a group of employees.

    I'm having trouble printing out a Tradesman, for example in the following code, when I print the personnel my output should be:

    Mike, Sales
    Fred, Engineering, Welder

    but instead it only prints:

    Mike, Sales
    Fred, Engineering



                    Staff personnel = new Staff();
    		Employee e1 = new Employee("Mike","Sales");
    		Employee e2 = new Tradesman("Fred","Engineering","Welder");
    		personnel.hire(e1); personnel.hire(e2); 
    		personnel.put();
    Can anyone help? I think the problem could be that I'm not storing a Tradesman correctly into the array in the hire method in class Staff. Any guidance in the right direction will help. Thanks in advance.
    Here's the code for the classes so far:


    class Employee {
    	private String name, department;
     
    	Employee(String n, String d) {
    		name = n; department = d;
    	}
     
    	void putEmp() {
    		System.out.print(name + ", " + department);
    	}
     
    	boolean equals(Employee e) {
    		return name==e.name &&
    			department==e.department;
    	}
    }
     
    class Tradesman extends Employee {
    	private String trade;
     
    	Tradesman(String n, String d, String t) {
    		super(n, d); trade = t;	
    	}
     
    	void putTrade() {
    		putEmp();
    		System.out.print(", " + trade);
    	}
     
    }
     
    class Staff {
    	private final int numStaff = 100;
    	private Employee[] staff = new Employee[numStaff];
    	private int count = 0; // num. of staff
     
    	void hire(Employee e) {
    		staff[count] = e;
    		count++;
    	}
     
    	void put() {
    		for(int i=0; i<count; i++) {
    			staff[i].putEmp();
    			System.out.println();
    		}
    	}
    }
     
    class EmployeeTest {
    	public static void main(String[] args) {
    		Staff personnel = new Staff();
    		Employee e1 = new Employee("Mike","Sales");
    		Employee e2 = new Tradesman(
    						"Fred","Engineering","Welder");
    		personnel.hire(e1); personnel.hire(e2); 
    		personnel.put();
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Small problem regarding inheritance of classes

    You seem to expect putTrade() to be called. But where do you call that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Small problem regarding inheritance of classes

    But how would I use it? For instance in put(), if i use putTrade() instead of putEmp() I will get an error any time I try to print an employee without a trade as they have different parameters
    Last edited by Stockholm Syndrome; October 7th, 2011 at 02:37 PM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Small problem regarding inheritance of classes

    I believe what you want to do is override the putEmp() method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Small problem regarding inheritance of classes

    Quote Originally Posted by KevinWorkman View Post
    I believe what you want to do is override the putEmp() method.
    How do I do that?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Small problem regarding inheritance of classes

    Quote Originally Posted by Stockholm Syndrome View Post
    How do I do that?
    First hit for googling "java overriding": Overriding and Hiding Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7

    Default Re: Small problem regarding inheritance of classes

    Quote Originally Posted by KevinWorkman View Post
    And after you have finished learning about overridding...

    Consider using toString instead of putEmp().

    If you set up the toString() correctly in the superclass, it allows for easy continuation through subclasses. You can do this with any method, of course, but using the toString() method just feels right.

    Example:
    class Manager
    {
         public String name, department;
         ...
         public String toString() // overriding the toString() method of the superclass (which is Object)
         {
              return this.name + ", " + this.department;
         }
    }
     
    class SpecialManager extends Manager
    {
         public String title;
         ...
         public String toString() // overriding the toString() method of the superclass (which is Employee)
         {
              //since SpecialManager is a subclass of Manager and Manager.toString() is not private
              //we can use our superclass's toString() method
              //we also want to add what our special title is
              return super.toString() + ", " + this.title;
         }
    }
     
    public static void main(String[] args)
    {
         Manager fred = new Manager("Fred","Programming");
         SpecialManager jimmy = new SpecialManager("Jimmy","Programming","MBA");
     
         System.out.println(fred.toString());
         System.out.println(jimmy.toString());
     
         /*******************
         Output
         Fred, Programming
         Jimmy, Programming, MBA
         *******************/
    }
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  8. #8
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Small problem regarding inheritance of classes

    Thanks for the help I solved it by making a new print method in class Employee and then overriding it in the subclass Tradesman.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Small problem regarding inheritance of classes

    Quote Originally Posted by Stockholm Syndrome View Post
    Thanks for the help I solved it by making a new print method in class Employee and then overriding it in the subclass Tradesman.
    I'm confused about why you needed a new print method. Wouldn't overriding putEmp() have done the trick?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Small problem regarding inheritance of classes

    Yea sorry I phrased that wrong.. I rewrote my code and my print method that I overrode was no longer called putEmp(). Basically the same though. Thanks again for the help

Similar Threads

  1. Small problem, help please?
    By Jonathansafc in forum What's Wrong With My Code?
    Replies: 33
    Last Post: September 3rd, 2011, 06:46 PM
  2. Problem with inheritance??
    By bczm8703 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2011, 06:13 AM
  3. Inheritance - Testing Point, Square, and Cube Classes
    By natefactor07 in forum Object Oriented Programming
    Replies: 1
    Last Post: January 30th, 2011, 08:56 PM
  4. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM
  5. small problem ... I need help
    By Ashar in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2009, 11:26 AM