Why does the following code compile and run successfully?
I have 2 classes in JAVA:
Parent class:
public class Parent {
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge(){
return this.age;
}
}
Child class:
public class Child extends Parent{
public static void main(String[] args){
Parent p = new Parent();
p.setAge(35);
System.out.println("Parent "+p.getAge());
Child c = new Child();
System.out.println("Child " + c.getAge());
}
}
Output:
Parent 35
Child 0
Private members are not inherited in JAVA When calling a method getAge()
on an instance of a child class, why does it work successfully and even give the result how 0
?
source to share
the private member is not inherited, but the public methods that get it, yes.
When you create an object of a type, Child
all superclass variables are created as well, unless they are used directly.
If the superclass has public methods, you can access them from the subclass. And if these methods access the values ββof the private variable of the superclass, you can indirectly access them through public methods.
source to share
Access Modifiers , since the suggested name can only affect the accessibility of variables / methods and not inheritance, you cannot access the variable age
directly in your Child class, as that is private
, but it doesn't mean it isn't there for the child object.
So, in your case, two public methods are accessible from both classes, but inside your Child class, you cannot use age
directly unless you change its access modifier to protected
orpublic
For a better understanding, see Controlling Access to Class Members
Also you can check Can access modifiers prevent inheritance?
source to share
Inheritance . You get value from method
, not from variable
.
Default value :
- Class type (not primitive, object):
null
- Primitive type (int, float, etc.): 0
Please refer to the Java docs about inheritance: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
source to share
There is no constructor for the parent class, so JAVA creates a defualt constructor.
When a child expands on parental mode, it first goes to its parent contactor.
You can build this constructor in the parent class:
public Parent(){
this.age = 15;
}
and you will see the child is 15 when you print it out.
source to share
One of the main aspects of a programming language is how it initializes data. For Java, this is explicitly defined in the language specification. For fields and array components, when elements are created, they are automatically set by the system by default:
numbers: 0 or 0.0
booleans: false
object references: null
This means that explicitly setting fields to 0, false, or null (as appropriate) is unnecessary and redundant. Since this language feature was included in order to partially cut down on repetitive coding, it would be nice to make full use of it. Insisting that fields must be explicitly initialized to 0, false, or null is an idiom that is probably not acceptable in the Java programming language.
Also, setting a field explicitly to 0, false, or null can even cause the same operation to be performed twice (depending on your compiler).
source to share
public class Child extends Parent{
public static void main(String[] args){
Parent p = new Parent();
p.setAge(35);
System.out.println("Parent "+p.getAge());
Child c = new Child();
System.out.println("Child " + c.age); //can't access private
System.out.println("Child " + c.name); //can access public variable
System.out.println("Child " + c.getAge()); //can access public method
System.out.println("Child " + c.getName());//can,t access private method
}
}
class Parent {
private int age;
public String name;
public void setAge(int age){
this.age = age;
}
private String getName(){
return name;
}
public int getAge(){
return this.age;
}
}
- A private variable cannot be accessed outside the class. But we can make it available through
getter
andsetter
. TheOOP
concept of Encapsulation comes into play here . - public
methods
,variables
defined in the Parent class, can be accessed in a child class. TheOOP
concept of Inheritance is present here .
source to share
Adding a few cents. The Child class cannot access the age variable private
, which is part of the Parent class. But the age state is still part of the Child object. You can access it using the get and set method. To illustrate this further, you can call setAge()
on the child to set the correct age for the child.
public class Child extends Parent {
public static void main(String[] args) {
Parent p = new Parent();
p.setAge(35);
System.out.println("Parent "+p.getAge());
Child c = new Child();
c.setAge(20);
System.out.println("Child " + c.getAge());
}
}
Output:
Parent 35
Child 20
source to share
The private member has not inherited rights, but we can access it publicly. So, in your case, the private member is accessed using a public method. And the reason for the value is zero because the value of the variable age by the parent class object is not an object of the child class. so when you access the value of the age variable by the child class object, it prints the default value for the age variable.
source to share