Inheritance tutorial Series:
- Inheritance in Java
- Access Modifiers and Inheritance
- Overriding
- Hiding
- Inheritance and constructors
- Super Keyword
- Constructor Chaining
A class can have public, protected, package-private or private members. Lets see which members gets inherited and which do not.
Note: You can learn more about access modifiers here[Link].
A subclass inherits all of the public and protected members of its parent, no matter what package[Link] the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent.
Consider following example
Note: The following example is run using eclipse IDE. If you want to know how to run java program in eclipse see here[Link].
Superclass:
package packageA;
public class Parent{
//public member
public int age;
//protected member
protected String name;
//package-private member
String school;
//public member function
public void print() {
System.out.println("Age is:"+age);
System.out.println("Name is:"+name);
System.out.println("School is:"+school);
}
}
Subclass:
package packageB;
import packageA.Parent;
public class Child extends Parent{
public static void main(String[] args) {
Child childObject= new Child();
childObject.age=10;
childObject.name="James";
childObject.school="SchoolABC"; //this line will give error
childObject.print();
}
}
Here our superclass and subclass are in different packages. Superclass Parent is in package packageA and subclass Child is in package packageB.
If you run the above program it will give error at the mentioned line in code in Child class.
This is because when superclass and subclass are in different packages then only public and protected members[Link] gets inherited. In this case package-private members[Link] do not get inherited. So if you remove the line
System.out.println("School is:"+school);
from print() function in Parent and line
childObject.school="SchoolABC";
from Child class then this program will run fine.
Package-private members[Link] get inherited when superclass and subclass are in same package. For Examples
package packageA;
public class Parent{
//public member
public int age;
//protected member
protected String name;
//package-private member
String school;
//public member function
public void print() {
System.out.println("Age is:"+age);
System.out.println("Name is:"+name);
System.out.println("School is:"+school);
}
}
package packageA;
public class Child extends Parent{
public static void main(String[] args) {
Child childObject= new Child();
childObject.age=10;
childObject.name="James";
childObject.school="SchoolABC"; //Now it will run fine
childObject.print();
}
}
Output:
Age is:10
Name is:James
School is:SchoolABC
Now above code will run fine because both superclass and subclass are in same package packageA and the package private member school will get inherited in class Child also.
Private members and Inheritance
A subclass does not inherit the
private
members[Link] of its parent class. For Example
package packageA;
public class Parent{
//private member
private int age;
//public member function
public void print() {
System.out.println("Age is:"+age);
}
}
package packageA;
public class Child extends Parent{
public static void main(String[] args) {
Child childObject= new Child();
childObject.age=10;
childObject.print();
}
}
The above code will give error and will not compile because private member age does not get inherited in subclass.
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment