SoFunction
Updated on 2025-03-08

Detailed explanation of inheritance such as object-oriented programming in Java

This article describes the inheritance of Java object-oriented programming. Share it for your reference, as follows:

inherit:A special class has all the properties and behaviors of a general class.

Inheritance benefits:

1. Improved code reusability

2. Let the class have a relationship with the class before, and only with this relationship can there be polymorphic characteristics. Inheritance is the relationship between a class and a class before it.

Notes:

Only single inheritance is supported, but multiple inheritance is not supported. Because multiple inheritance has security risks: when multiple parent classes define the same function, but the functions are different, the subclass does not know which one to run.

2. When a child class inherits the parent class, it inherits all methods and properties of the parent class and can be used directly.

3. Java supports multi-layer inheritance, that is, the relationship between grandson-son-father

grammar:

[Class modifier] class Subclass name extends Parent class name{
  Statement;
}

For example:

class Pserson
{
  int age;
  String name;
  public void speak()
  {
    ("Hello World!");
  }
}
//Inherit Person class, inherit all methods and properties of the parent classclass Student extends Pserson
{
  public void study()
  {
    ("Good Study!");
  }
}
//Inherit Person class, inherit all methods and properties of the parent classclass Worker extends Pserson
{
  public void work()
  {
    ("Good work!");
  }
}

How to use functions in an inheritance system (see API documentation):

Check the functions of the parent class and create subclass objects to use functions

These three scenarios are often encountered during the inheritance process:

1) Variables with the same name

1. If a subclass has a non-private member variable of the same name, the subclass accesses the variable of this class, and uses this; the subclass accesses the variable of the same name in the parent class, and uses super.
References representing objects of this class
References representing parent class object (used the same as this)

2) Functions with the same name

1. If a subclass appears a function exactly the same as the parent class (the function name and parameters are the same), when the subclass object calls the function, the subclass function content will be run. , the functions of the parent class will be overwritten (also called rewriting).

2. Rewrite the definition: When the subclass inherits the parent class, follows the functions of the parent class, and enters the subclass. However, although the subclass has this function, the content of the function is inconsistent with the parent class. At this time, there is no need to define a new function, but use the override feature, retain the function definition of the parent class, and rewrite the function content.

3. Notes on rewriting (overwrite):

<1>Subclasses override parent class. You must ensure that the permissions of the subclass are greater than or equal to the permissions of the parent class before inheritance, otherwise the compilation will fail. (public>Don't write rhetorical keywords>private)
<2> Static can only cover static
<3>Overload: Only look at the parameter list of the function with the same name. Rewrite: The child-parent class methods must be exactly the same (function name and parameter list)

class Fu
{
  //public void show() When the parent class is show(), it will be exactly the same as the child class function, and the parent class's show function will be rewritten  public void show(String name) //The show function of the parent class is different from the child class (the parameter list is different), so the show function of the parent class will not be rewritten  {
    (name);
  }
}
class Zi extends Fu
{
  public void show()
  {
    ("zi");
  }
}
class Jicheng
{
  public static void main(String[] args)
  {
    Zi z1=new Zi();
    ("nihao");//The show function of the parent class will be called  }
}

3) Constructor

1. When initializing the subclass object, the constructor of the parent class will also run, because the first line of the constructor of the subclass has an implicit statement super() by default.

() will access the constructor of the hollow parameter in the parent class, and the first line of all constructors in the subclass is super() by default

3. The reason why subclasses must access the parent class constructor

<1> Because the data subclass in the parent class can be obtained directly, the subclass is to first see how the parent class initializes the data. Therefore, when the subclass is initialized, it first accesses the constructor of the parent class by default.
<2>If you want to access the constructor formulated by the parent class or the constructor whose parent class does not have empty parameters, you can formulate it by manually defining the super statement.
<3> Of course, the first line of the constructor of the subclass can also be manually specified to access the constructor of this class, but at least one of the constructors in the subclass will access the constructor of the parent class.

class Fu
{
  String name;
  int age;
  Fu(){("Hello Fu");}
  Fu(String name)
  {
    (name);
  }
  Fu(String name,int age)
  {
    =name;
    =age;
    ("name: "+name+",age: "+age);
  }
}
class Zi extends Fu
{
  //Zi(){("Hello Zi");} By default, the parameterless constructor of the parent class will be called first.  Zi()
  {
    super("zhangsan",20);//Manually use the super statement to specify the constructor of the parent class to obtain non-private information of the parent class    (name+"::"+age);
  }
}
class Test
{
  public static void main(String[] args)
  {
    Zi z1=new Zi();
  }
}

Example of constructor exception:

Write out the program results

class Super
{
  int i=0;
  public Super(String s)
  {
    i=1;
  }
}
class Demo extends Super
{
  public Demo(String s)
  {
    i=2;
  }
  public static void main(String[] args)
  {
    Demo d=new Demo("yes");
    ();
  }
}
//Compilation failed because the constructor with empty parameters is missing in the parent class.//or the subclass should specify the constructor in the parent class to be called through the super statement.

Rewrite and overload examples:

class Demo
{
   int show(int a,int b){return 0;}
}

The following functions can exist in a subclass of Demo.

A.public int show(int a,int b){return 0;}//Yes, overwrite.
B.private int show(int a,int b){return 0;}//No, permissions are not enough.
C.private int show(int a, long b){return 0;}//Yes, it is not the same function as the parent class. No coverage is equivalent to overloading.
D.public short show(int a,int b){return 0;}//No, because this function cannot appear in the same class as the given function, or in the child parent class.
E.static int show(int a,int b){return 0;}//No, static can only overwrite static. <br><br>So the subclass allows rewriting and overloading.

For more Java-related content, please view the topic of this site:Introduction and Advanced Tutorial on Object-Oriented Programming in Java》、《Java Data Structure and Algorithm Tutorial》、《Summary of Java operating DOM node skills》、《Summary of Java files and directory operation skills"and"Summary of Java cache operation skills

I hope this article will be helpful to everyone's Java programming.