4 Replies - 614 Views - Last Post: 21 November 2008 - 06:53 AM Rate Topic: -----

#1 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Problem with inheritance

Posted 21 November 2008 - 05:00 AM

Hi! I need to write a program with inheritance (homework). So I start my code:

public class Employee
{
  protected String name;
  protected int sallary;

  public Employee(String name, int sallary)
  {
	 this.name = name;
	 this.sallary = sallary;
  }

  // getters and setters here
}



For now, everything's fine.
Now the subclass Boss in other file:

public class Boss extends Employee
{
// some code here
}



And I get a compilation error: "cannot find symbol - constructor Employee" ... Example in book looks identical, but I can't compile this. Ah, and I use BlueJ (required by teacher :/) as IDE. Any one got idea what's wrong?

This post has been edited by k0b13r: 21 November 2008 - 05:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with inheritance

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Problem with inheritance

Posted 21 November 2008 - 05:19 AM

which one doesnt compile, the superclass or the subclass?
Was This Post Helpful? 0
  • +
  • -

#3 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Re: Problem with inheritance

Posted 21 November 2008 - 05:23 AM

Employee (superclass) works perfect, Boss (subclass) got the error

This post has been edited by k0b13r: 21 November 2008 - 05:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5775
  • View blog
  • Posts: 12,587
  • Joined: 16-October 07

Re: Problem with inheritance

Posted 21 November 2008 - 05:55 AM

Your Employee class has only one constructor and it takes two parameters. In order for a child to work, that parent constructor must be called somehow, using super.

So, this will work:
public class Boss extends Employee {
	public Boss(String name, int sallary) {
		super(name, sallary);
	}
}



So will this:
	public Boss() {
		super("Bob", 1000000);
	}



If you don't want the kids to always have to call the constructor, but you don't want the parent to have a default constructor, but you can make one protected. This will work:
class Employee {
	protected String name;
	protected int sallary;

	public Employee(String name, int sallary) {
	 this.name = name;
	 this.sallary = sallary;
	}

	// so the kids can extend
	protected Employee() {} 
}

public class Boss extends Employee {
}


Was This Post Helpful? 1
  • +
  • -

#5 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Re: Problem with inheritance

Posted 21 November 2008 - 06:53 AM

I see. Thanks a lot baavgai !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1