Java object oriented programming such as inheritance details

  • 2021-01-14 05:59:51
  • OfStack

This article shows examples of inheritance such as Java object-oriented programming. To share with you for your reference, as follows:

Inheritance: 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 it have the characteristics of polymorphism. Inheritance is the relationship between a class and the class before it.

Precautions:

1.java only supports single inheritance, not multiple inheritance. Because multiple inheritance is a security hazard: when multiple superclasses define the same function but have different functions, the subclass does not know which one to run.

2. When a subclass inherits its parent class, it inherits all the methods and attributes of the parent class, which can be used directly.

3, java supports multi-level inheritance, that is, grand-child - father relationship

Grammar:


[ Class modifier ] class  A subclass of  extends  The parent class name {
   statements ;
}

Such as:


class Pserson
{
  int age;
  String name;
  public void speak()
  {
    System.out.println("Hello World!");
  }
}
// inheritance Person Class that inherits all the methods and properties of the parent class 
class Student extends Pserson
{
  public void study()
  {
    System.out.println("Good Study!");
  }
}
// inheritance Person Class that inherits all the methods and properties of the parent class 
class Worker extends Pserson
{
  public void work()
  {
    System.out.println("Good work!");
  }
}

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

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

These three scenarios are frequently encountered during inheritance:

1) Variable with the same name

1. If there is a non-private member variable of the same name in a subclass, then the subclass accesses the variable of the same class, using this; A subclass accesses a variable of the same name in the parent class, using super.
2.this represents a reference to an object of this class
3.super represents a reference to a parent object (the same usage as this)

2) function of the same name

1. If a subclass has a function identical to that of its parent class (with the same name and arguments), then when the subclass object calls the function, the content of the subclass function will be run. , the functions of the parent class are overridden (also known as overrides).

2. Overwrite the definition: When a subclass inherits from its parent class, it inherits the function of the parent class into the subclass. In this case, there is no need to define a new function. Instead, the override feature is used to preserve the function definition of the parent class, and the function content is overwritten.

3. Notes for rewriting (covering) :

< 1 > Subclass overrides the parent class, must ensure that the permissions of the child class is greater than or equal to the permissions of the parent class, to inherit, otherwise the compilation failed. (public > Don't write rhetorical keywords > private)
< 2 > Static can only override static
< 3 > Override: see only the argument list of the function with the same name overwrite: child and parent methods are modulo 1 (function name and argument list)


class Fu
{
  //public void show()   When the parent class show() , and subclass functions 1 die 1 Like, the parent class show The function will be rewritten 
  public void show(String name) // Of the parent class show Functions and subclasses don't 1 Sample (parameter list no 1 Kind), therefore the parent class show Functions are not overwritten 
  {
    System.out.println(name);
  }
}
class Zi extends Fu
{
  public void show()
  {
    System.out.println("zi");
  }
}
class Jicheng
{
  public static void main(String[] args)
  {
    Zi z1=new Zi();
    z1.show("nihao");// The superclass will be called show function 
  }
}

3) Constructors

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

2. ES77en () accesses the void constructor in the parent class, and all constructors in the subclass default to super() on line 1

3. The reason why subclass 1 must access the constructor of the parent class

< 1 > Since the data in the parent class can be obtained directly, the subclass is created by looking at how the parent class initializes the data, so the subclass by default accesses the constructor of the parent class first when the object is initialized.
< 2 > To access a constructor specified by a parent class, or a constructor that has no null arguments, you can do so by manually defining an super statement.
< 3 > Of course, the subclass constructor line 1 can also manually specify the this statement to access the constructor of the class, but at least one of the subclass constructors will access the constructor of the parent class


class Fu
{
  String name;
  int age;
  Fu(){System.out.println("Hello Fu");}
  Fu(String name)
  {
    System.out.println(name);
  }
  Fu(String name,int age)
  {
    this.name=name;
    this.age=age;
    System.out.println("name: "+name+",age: "+age);
  }
}
class Zi extends Fu
{
  //Zi(){System.out.println("Hello Zi");}   By default, the parent class's no-argument constructor is called first 
  Zi()
  {
    super("zhangsan",20);// Use hand super Statement to obtain non-private information about the parent class by specifying the constructor of the parent class 
    System.out.println(name+"::"+age);
  }
}
class Test
{
  public static void main(String[] args)
  {
    Zi z1=new Zi();
  }
}

Examples of constructor exceptions:

Write out the results of the program


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");
    System.out.println(d.i);
  }
}
// Compilation failed because the parent class lacked a constructor for an empty argument. 
// Or subclasses should pass super Statement specifying the constructor in the parent class to call. 

Overriding and overloading examples:


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

The following functions can exist in subclasses of Demo.

A.public int show(int a,int b){return 0; }// OK, overwrite.
B.private int show(int a,int b){return 0; }// No, not enough permissions.
C.private int show(int a,long b){return 0; }// OK, and the parent class is not a function. No overrides, 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 a child or parent class.
E.static int show(int a,int b){return 0; }// No, static can only override static. < br > < br > So subclasses allow overriding and overloading.

More java related content interested readers can see the site: Java object-oriented programming introduction and advanced tutorial, Java data structure and algorithm tutorial, Java operation DOM node skills summary, Java file and directory operation skills summary and Java caching skills summary

I hope this article is helpful to java program design.


Related articles: