Summary of three characteristics of object-oriented

1. Packaging

1. Improve program security and protect data

2. Hide the implementation details of the code and provide external access

3. Unified interface

4. The system is maintainable and enhanced

2. Succession

The essence of inheritance is the abstraction of a group of classes

It improves the reusability of the code

The relationship between classes provides the premise for polymorphism

When to write inheritance?
Things inherit only when there is a ownership relationship between them. xxx is one of yyy.

super note

1.super must call the constructor of the parent class in the first instance of the constructor

2.super must only appear in subclass methods or construction methods!

3.super and this cannot call the constructor at the same time!

This: call this object by itself (it can be used without inheritance)

super: represents the application of the parent object (can only be used under inheritance conditions)

Notes for using inherited overrides:

1. The permission of the subclass to overwrite the parent class must be greater than or equal to the permission of the parent class

. public>protected>default>private

2. In overwrite, static can only overwrite static or be overwritten by static.

Override: inheritance relationship is required. The subclass overrides the method of the parent class. The subclass must be the same as the parent class

Classes are consistent. Subclasses override the methods of the parent class and execute the methods of subclasses

Why rewrite?

The functions of the parent class and the child class are not necessarily required or satisfied

When the subclass object is created, the constructor in the parent class is also run.
Why?

In fact, in the first line of all constructors of subclasses, there is a default super(); Null in calling parent class

Parameter constructor.

Why must subclass instantiation be initialized in the parent class?

Because a subclass inherits from the parent class and only the content in the parent class, before instantiating and using the content in the parent class,
You must first specify how the parent class initializes its own content,
Therefore, the constructor of the subclass will access the constructor of the null parameter of the parent class by default. Via super();

Example:

class Fu extends Object
{
	Fu()
	{
		//super();
		//Display initialization.
		//Construct code block initialization.
		System.out.println("fu run...");
	}
	/**/
	Fu(int x)
	{
		System.out.println("fu ..."+x);
	}
}
class Zi extends Fu
{
	Zi()
	{
//		super();
		
		System.out.println("Zi run...");
	}
	Zi(int x)
	{
		this();
		System.out.println("Zi ..."+x);
	}
}

class ExtendsDemo4 
{
	public static void main(String[] args) 
	{
		new Zi(4);
	}
}

3. Polymorphism

A variety of manifestations of something.
The focus is on the polymorphism of objects.

The embodiment of polymorphism in the program: the reference of the parent class or interface points to its own subclass object.

**Benefits of polymorphism: * * improves the scalability of the code, and the later functions can be executed by the previous program.

Disadvantages of polymorphism: Although you can use later code, you can't use subclass specific functions. Only functions that override the parent class can be used.

The premise of polymorphism: there must be relationships (inheritance, implementation), and there are usually overlay operations.

Animal a = new Dog();//Upward transformation. When? When it is necessary to improve the extensibility of the program and hide specific subtypes.

Cat c = (Cat)a;//Downward transformation. When? When you need to use subtype specific methods.

Animal a = new Dog();// Upward transformation. When? When it is necessary to improve the extensibility of the program and hide specific subtypes.

Cat c = (Cat)a;// Downward transformation. When? When you need to use subtype specific methods.

Methods that cannot be overridden

1.static method belongs to class, not instance

2.final modifier constant

3.private modification method

The special embodiment of polymorphic calling methods in child and parent classes.

1. Member variables.

When a polymorphic call is called, the compilation and operation refer to the member variables in the class to which the reference variable belongs.
Simply put: compile and run to the left of the equal sign.
‚Äč
2. Member function.

During polymorphic call, the compilation refers to the class to which the reference variable belongs, and the runtime refers to the class to which the object belongs.
To put it simply: compile to the left and run to the right. Because functions have an overriding feature.

3. Static function.

When a polymorphic call is made, both compilation and operation refer to the static function in the class to which the reference variable belongs.
Simply put: compile and run to the left of the equal sign.

example

class Fu
{
//	int num = 4;
	void show()
	{
		System.out.println("fu run");
	}

	static void staticMethod()
	{
		System.out.println("fu static method");
	}
}
class Zi extends Fu
{
//	int num = 5;
	void show()
	{
		System.out.println("Zi run");
	}

	static void staticMethod()
	{
		System.out.println("Zi static method");
	}
}
class DuoTaiDemo4 
{
	public static void main(String[] args) 
	{
		/*
		Static function, statically bound to its class.
		*/
		Fu f = new Zi();
		f.staticMethod();
		Zi z = (Zi)f;
		z.staticMethod();
		/*
		//Member function. Dynamically bind to the current object
		Fu f = new Zi();
		f.show();
		*/
		/*
		//Member variable
		Fu f = new Zi();
		Zi z = (Zi)f;
		System.out.println(f.num);//4
		System.out.println(z.num);//5
		*/
	}
}

Tags: Java

Posted on Wed, 08 Sep 2021 00:43:16 -0400 by the-hardy-kid