Panos
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.
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.
Please post the code you've tried
ASKER
Hi.
I have tried the code using public.
Can i use protected for the Horse constructor to create an object?
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);
}
}
Can i use protected for the Horse constructor to create an object?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
This means that the Objekt will be not a Horse but for example a Pony that is a Horse or not?
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
:)