Home >Java >javaTutorial >JavaSE object-oriented classes, inheritance and polymorphism
1 In Java, a class file is a code file with the suffix .java. Only one public class is allowed to appear in each class file. When there is a public class, The name of the class file must be the same as the name of the public class. If public does not exist, the name of the class file can be any name.
2 Within the class, for member variables, if there is no explicit assignment initialization when defining, Java will ensure that each member variable of the class is properly initialized
1) For variables of basic data types such as char, short, byte, int, long, float, double, etc., they will be initialized to 0 by default (boolean variables will be initialized to false by default);
2) For reference types Variables will be initialized to null by default.
3 If the constructor is not explicitly defined, the compiler will automatically create a no-argument constructor; if the constructor is explicitly defined, the compiler will not automatically add the constructor. . Note that all constructors are static by default.
4 The order in which classes instantiate objects
1) When the program is executed, an object of a certain class needs to be generated. The Java execution engine will first check whether the class is loaded. If it is not loaded, the class will be loaded first and then the object will be generated. If it has been loaded, the object will be generated directly.
2) During the loading process of the class, the static member variables of the class will be initialized. In addition, if there is a static statement block in the class, the static statement block will be executed. The execution order of static member variables and static statement blocks is consistent with the order in the code.
3) In Java, classes are loaded on demand. This class will only be loaded when it is needed, and it will only be loaded once.
Look at the following example to understand:
public class Bread { static { System.out.println("Bread is loaded"); } public Bread() { System.out.println("bread"); } public static void main(String[] args) throws ClassNotFoundException { Bread bread1 = new Bread(); Bread bread2 = new Bread(); } }
When you run this code, you will find that "Bread is loaded" will only be printed once.
4) In the process of generating an object, the member variables of the object will be initialized first, and then the constructor will be executed. This means that the variables in the class will be initialized before any method (including the constructor) is called, even if the variables are interspersed between method definitions.
public class Test { public static void main(String[] args) { new Meal(); } } class Meal { public Meal() { System.out.println("meal"); } Bread bread = new Bread(); } class Bread { public Bread() { System.out.println("bread"); } }
The output result is:
bread meal
1 Inheritance
1) Inheritance is an indispensable part of all OOP languages. The extends keyword is used in java to express the inheritance relationship. When a class is created, it is always inherited. If the class to be inherited is not explicitly stated, it is always implicitly inherited from the root class Object. For example, the following code:
class Person { public Person() { } } class Man extends Person { public Man() { } }
2) The class Man inherits from the Person class. In this case, the Person class is called the parent class (base class), and the Man class is called the subclass (derived class). If there is an inheritance relationship between two classes, the subclass will automatically inherit the methods and variables of the parent class, and the methods and variables of the parent class can be called in the subclass.
3) In Java, only single inheritance is allowed, which means that a class can only explicitly inherit from one parent class at most. But a class can be inherited by multiple classes, which means that a class can have multiple subclasses.
2 Subclass inherits the attributes of the parent class
When the subclass inherits a certain class, it can use the member variables in the parent class, but it does not completely inherit the parent class. all member variables. The specific principles are as follows:
1) The public and protected member variables of the parent class can be inherited; the private member variables of the parent class cannot be inherited;
2) For the package access permission member variables of the parent class, if the subclass and If the parent class is in the same package, the subclass can inherit; otherwise, the subclass cannot inherit;
3) For the parent class member variables that the subclass can inherit, if a member variable with the same name appears in the subclass , the hiding phenomenon will occur, that is, the member variables of the subclass will block the member variables of the same name of the parent class. If you want to access a member variable with the same name in the parent class in a subclass, you need to use the super keyword for reference.
3 Subclass inherits the methods of the parent class
Similarly, the subclass does not completely inherit all the methods of the parent class.
1) Can inherit the public and protected member methods of the parent class; cannot inherit the private member methods of the parent class;
2) For the package access member methods of the parent class, if the subclass and the parent class are in the same package, the subclass can inherit; otherwise, the subclass cannot inherit;
3) For the parent class member methods that the subclass can inherit, if a member method with the same name appears in the subclass, it is calledOverride, that is, the member methods of the subclass will override the member methods of the parent class with the same name. If you want to access a member method with the same name in the parent class in a subclass, you need to use the super keyword for reference.
4 Note
Hide and cover are different.
Hiding is for member variables and static methods,
and overwriting is for ordinary methods.
5 构造器
1)子类是不能够继承父类的构造器,但是要注意的是,如果父类的构造器都是带有参数的,则必须在子类的构造器中显示地通过super关键字调用父类的构造器并配以适当的参数列表。
2)如果父类有无参构造器,则在子类的构造器中用super关键字调用父类构造器不是必须的,如果没有使用super关键字,系统会自动调用父类的无参构造器。
6 super主要有两种用法
1)super.成员变量/super.成员方法;
2)super(parameter1,parameter2....)
第一种用法主要用来在子类中调用父类的同名成员变量或者方法;
第二种主要用在子类的构造器中显示地调用父类的构造器
要注意的是,如果是用在子类构造器中,则必须是子类构造器的第一个语句。
1 子类必是父类,所以父类型的引用可以指向子类型的对象。
2 动态绑定方法的多种不同版本(即非编译时绑定,而是晚绑定或者动态绑定)
Parent p = new Child(); p.eat();
此时Parent和Child中必须都有eat()方法;
即多态进行动态绑定的前提条件是继承,子类必须继承父类的方法,才能使用父类的引用进行方法调用。
3 父类引用能向下转换成子类引用的前提是父类引用指向子类的对象。
4 向下转换后的引用,就可以调用子类特有的方法了。
5 多态的好处,定义方法时,出入参可以申明为父类的类型,传参时,可以传递子类的对象。这样在方法不改变的前提下,就可以扩展子类的种类,添加新的逻辑。
6 覆盖方法才会进行动态绑定,而隐藏是不会发生动态绑定的。
1)举例如下
public class Test { public static void main(String[] args) { Shape shape = new Circle(); System.out.println(shape.name); // 成员变量(隐藏) shape.printName(); // 静态方法(隐藏) shape.printType(); // 非静态方法(覆盖) } } class Shape { public String name = "shape"; public Shape(){ System.out.println("shape constructor"); } public void printType() { System.out.println("this is shape"); } public static void printName() { System.out.println("shape"); } } class Circle extends Shape { public String name = "circle"; public Circle() { System.out.println("circle constructor"); } public void printType() { System.out.println("this is circle"); } public static void printName() { System.out.println("circle"); } }
2)输出结果:
shape constructor circle constructor shape shape this is circle
3)原因分析
覆盖受RTTI(Runtime type identification)约束的,而隐藏却不受该约束。也就是说只有覆盖方法才会进行动态绑定,而隐藏是不会发生动态绑定的。在Java中,除了static方法和final方法,其他所有的方法都是动态绑定。因此,就会出现上面的输出结果。
The above is the detailed content of JavaSE object-oriented classes, inheritance and polymorphism. For more information, please follow other related articles on the PHP Chinese website!