Inheritance

4 0 0
                                        

Inheritance  - this is a concept where the child class (sub class) inherits the fields and methods of the mother class (super class) by using the extend keyword. Every class can only extend one mother class. In case that you did not declare a mother class explicitly, the java compiler will extend your class implicitly from the super class Object. An Object class is considered the mother of all classes. Note that this Object class is not the same object that you created from your class. This Object class is a blueprint class where you can create an object too. 

A class you extended does extends a super class of its own. Thus, this concept of extending a class one after another where in the long run creates a chain of classes will help developers to lessen the burdens of creating a program from scratch.

Members of a class are fields, methods and nested types. A constructor is a not a member of a class. Check the example below.

Example:

======================

class Mother {//below italicized items are fields
protected String shoes;
private String phone;
protected String clothes;
public String phoneNumber;

//below bold item is a constructor

Mother(String shoes, String phone, String clothes){
this.shoes = shoes;
this.phone = phone;
this.clothes = clothes;
this.phoneNumber = "1234-5678-90";
}

//below items are methods

protected void setPhone(String phone){
this.phone = phone;
}
protected String getPhone(){
return phone;
}
}

======================

Note: Inherit a member means you can use, replace, hide or supply them with new members.

Rules: 

1. A subclass cannot inherit a constructor since a constructor is not part of the member of a class.

2. A subclass can inherit the public and protected members of a class no matter where the package that super class is in.

3. If a subclass is in the same package with the super class, the subclass can inherit the package-private member of that super class.

4. Private members of the super class cannot be inherited by the subclass.

5. A private field of the super class can be access through a public or protected set and get methods of the super class.

6. If you happen to extend a nested class as a super class, then you have an indirect access to all members including the private of that enclosing class of your super class.

7. You can use a super keyword to call the constructor of the super class.

===================

class Child extends Mother {
Child(){}
Child(String d, String n){
super(d, n);
}
class ChildDream extends Mother.DreamTeam{
void displayDreamTeam(){
System.out.println("My name is " + yourName());
System.out.println("My dream is " + yourDream());
}
}
void display(){
ChildDream childDream = new ChildDream();
childDream.displayDreamTeam();
}
public static void main(String[] args){
Child child1 = new Child();
child1.display();
Child child2 = new Child("Hokage", "Naruto Uzumaki");
child2.display();
}
}
class Mother {
private final String myDream;
private final String myName;
Mother(String dream, String name){
myDream = dream;
myName = name;
}
Mother(){
myDream = "Argonaut";
myName = "Bell Cranel";
}
public class DreamTeam{
protected String yourName(){
return myDream;
}
protected String yourDream(){
return myName;
}
}
}

===================

Results:

My name is ArgonautMy dream is Bell CranelMy name is HokageMy dream is Naruto Uzumaki

Java ProgrammingWhere stories live. Discover now