+ 2

Do I have to declare all of class members as 'protected' if I would like to use them in a derived class?

If I create a subclass of class Animal { private int age; public void setAge(int Age){ age=Age; } public int getAge(){ return age; } } Does it make sense to declare age as private if I know that I will create subclasses? Is getter and setter are necessary? How can I use them in my subclass? Or should I use protected, if I know that I will create a subclass? lesson: "When one class is inherited from another class, it inherits all of the superclass' non-private variables and methods." But why do I want to not inherit anything? I make a subclass because it is the same thing with some extra members/methods. My question is about Java, but I also would like to know about if there is a difference in C++

16th Nov 2016, 6:34 PM
Marcell Juhasz
1 Odpowiedź
+ 2
if u like to change the value for the private age make a public or protected method that do that or read it. private Propertys is good u have controle of it more that way. let say the method prevent u from setting a age higer than maby 100 but public age u can set any valid intvalue very high age for an animal
16th Nov 2016, 9:32 PM
Ola
Ola - avatar