Link to home
Start Free TrialLog in
Avatar of Panos
PanosFlag for Germany

asked on

Create object from subclass with protected constructor

Hello
I m new in Java and i need help with a question i have.
Protected Constructors can be accessed form subclasses from other packages too.
Can i create one object from this subclasses? And if yes is there any sample code ?
thank you in advance.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post the code you've tried
Avatar of Panos

ASKER

Hi.
I have tried the code using public.
package horse;
public class Horse {
	private int weight;
	public Horse(int weight) {
		this.setWeight(weight);
	}
}

import horse.Horse;
public class test {
	public static void main(String[] args) {
		Horse firstHorse = new Horse(145);
	}
}

Open in new window


Can i use protected for the Horse constructor to create an object?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Panos

ASKER

Hi
This means that the Objekt will be not a Horse  but for example a Pony that is a Horse or not?
Yes, that sort of thing. That's a good example actually
:)