Inheritance in Java Language - ProgrammerTech
Language:

Search

Inheritance in Java Language

  • Share this:
Inheritance in Java Language

The concept of inheritance

Inheritance, as is the customary case in reality, for a son to inherit some traits from his father or from his mother some traits. The same thing we have in object oriented programming in the concept of inheritance is that a class inherits from another class so that the inherited class is called the son and the inherited class is called the father. So that the son can access all the functions and variables in the parent class and reuse them again, except for the protected variables of the parent class.

The benefit of inheritance is to shorten the code instead of rebuilding the function again if the function is in the parent class. We can reuse it again instead of building it and consuming memory and lengthening the code so that the program is organized and beautiful. So we said that the class that inherits its name is the son's class, and the class that inherits its name is the father's class. The child can access all the variables and functions of the parent class except for the protected variables. Now we will see how to implement inheritance in Java.

Suppose we have a class named c and a class named A, and to make the class A inherit from class c we use the reserved word extends, meaning that A inherits from c, meaning that A becomes the son and the c is the father. Now class A can access all the variables and functions in its father, and be as follows.

package a;
class c
{
#
}
public class A extends c {
     public static void main(String []args){
        System.out.println("Hello World");
     }
}

In this case, class A inherited from class c.

Example 1

package a;
class c
{
    private int x1=6; 
    protected int x2=7;
    public int x3=10;
}
public class A extends c {
     public static void main(String []args){
         A ob1=new A();
        System.out.println(ob1.x2);
     }
}

In the beginning, we created class C, which is class C, and this is considered the father. And we created three variables with different access degrees, the variable x1 whose access degree was private. The second variable with a slightly protected low access degree is x2., and the third is a global variable x3. Then we inherited son A from father c, and inside the main program we asked the program to print the value of x2. When printed, the result will be:

7

We notice when executing this program that we have accessed the value of the variable x2 as we requested and printed it with the ob object. We can also access the value of x3 with the same command, but the value of the variable x1 cannot be accessed because it is protected.

Example 2

Suppose we have a car factory and it has two types, model A and model B.

package model_a;
class Model_B
{
    int speed;
    int size;
    String Model;
}
public class Model_A {
    int speed;
    int size;
    String Model;
    public static void main(String []args){
     }
}

We also note that this example has two classes, and each class has three variables, meaning there were 6 variables in the program and it will reserve a memory space. These variables will take up a lot of memory. We can shorten this code by inheritance to write the common properties between all classes and put them in. One class and then we make the rest of the classes inherit from this class so that we stay away from defining variables as much as possible.

Example 3

package model_a;
class car
{
    int speed;
    int size;
    String Model;
}
class Model_B extends car
{
}
public class Model_A extends car{
    public static void main(String []args){
        Model_A A=new Model_A();
        Model_B B=new Model_B();
        A.speed=200;
        B.speed=250;
        System.out.println(A.speed);
        System.out.println(B.speed);
     }
}

We created a class App named car and it had three variables, two numerical variables, and the last one is a text variable, which is for the specifications of the cars in this gallery. I mean, we defined the common properties in the classes, which are the functions and similar variables that are repeated in each class, and we put them in Class AB. Then we made the rest of the classes inherit from the father class, which is the Class Model_B, inherited from the father class, and then. The Model_A class is also inherited from the father who is a car, and this class has access to all variables and functions except for the protected ones. And here the code was shortened instead of what it was in the previous example, and we shortened the code and freed up a lot of memory.

Then we entered the main program, we will create an object from each class so that it can access the variables and functions in the parent and reuse them when needed. The classes are Model_A A and the class Model_B B, and now we can access the variables and functions in the father, then we print them and the result is.

200
250

 

Hierarchical inheritance in the Java language

We talked at the beginning of the lesson that the inherited class is called the son class and the inherited class is called the father class. The same concept here, but we will branch out a little, we will have hierarchical inheritance so that we will have a grandfather class, a son class, and a grandson class. The heart of the topic in this paragraph is whether the grandson class can access the variables and functions of the grandfather class..

Example 4

package model_a;
class A    /the father
{
    #
}
class B extends A   {    /Son
}
B This class is considered the son of the class
A And he is considered a grandson of the class
public class C extends B{ 
     public static void main(String []args){
        System.out.println("Hello World");
     }
}

We also note from the previous code that it contains three classes, which are Class A, Class B, and Class C. Class C was the father and Class B was the son of Class A and Class C is the son of Class B and he is considered a grandson of Class A, meaning his grandfather is A. And here our question will be whether Class C can access the variables and functions of its grandfather, Class A, or not.

Example 5

package C;
class A
{
    int x= 10;
    void print()
    {
        System.out.println(x);
    }
}
class B extends A
{
}
public class C extends B {
     public static void main(String []args){
         C ob=new C();
         ob.x=20;
         ob.print();
     }
}

The program contains three classes, which are Class A, Class B, and Class C, and Class C was the father. And class B is the son of class A, and class C is a son of class B and is a grandson of class A, meaning his grandfather is A. Then we entered the main program and derived an object from class C, which is ob. Then we asked to access the variable x in his grandfather. And we gave it a value of 20, then we called the print function in Class A to print the value of x, and the work of this function is to print the value of x.

 

keyword super in java

The word super is a reserved word in Java, in this lesson we will talk about the reserved word super and this word means that we can access through it the functions and variables of the parent class, as well as the constructor functions.

Example 6

package a1;
class B
{
    String name="My name is Saja";
}
public class A1 extends B{
    String name="My name is Muhammad";
    A1()
    {
        System.out.println(name);
        System.out.println(super.name);
    }
     public static void main(String []args){
        new A1 ();
    }
}

At first, we created a class AB, which is class B, and it has a text variable named name. And then we inherited class A1 from father B and also has the same variable as in the father. Here we apply the Overrid principle. Then we built A1 build function and then we asked to print the value of the variable name of the child class. Then we asked to access the value of the variable name of the parent class by using super, and in the main program we called the child class A1.

When you run the code, the result will be:

My name is Muhammad
My name is Saja

Example 7

package a1;
class B
{
    String name="My name is Saja";
    void show()
    {
        System.out.println(name);
    }
}
public class A1 extends B{
    String name="My name is Muhammad";
    A1()
    {
        System.out.println(name);
        super.show();
    }
     public static void main(String []args){
        new A1 ();
    }
}

We have created a class AB which is class B and it has a text variable named name and it has a void function called show and this function prints the variable name and it will automatically print the name of the parent class because we did the operation inside the parent class. And then we inherited class A1 from father B and also has the same variable as in the father. Here we apply the Overrid principle. Then we built A1 build function and then we asked to print the value of the variable name of the child class. Then we called the show function of the father via the word super, and this function we put in it the value of the father.

When we run this code, we will get the result

My name is Muhammad
My name is Saja

We conclude that the grandson class can access all variables and functions. The grandfather class except those functions and protected variables are private because they are private.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com