Java-- inheritance and polymorphism

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_45613931/article/details/101993151

Java-- inheritance and polymorphism


I was a novice, to write blog for self-review, self-summary.
If wrong, please point out heavyweights.
Reference materials: zero-based learning Java


When succession, into the general method of operation of the parent class when the code to avoid repeated inheritance, do not use protected fields.
When you do not want your own class again be extended, can be added in a final statement before the class, so that these can no longer be inherited.

When you create a subclass object, the first implementation of the parent class constructor, initialize the members inherited from the parent class, then subclass constructor executes, member initialization subclasses.

In Java, a class can only inherit from a parent class, that is, single inheritance.

Access rights control character subclass only greater than or equal to the parent, i.e., subclass overrides the superclass method.

Between classes and categories: dependence, polymerization, inheritance
1) dependency: the method may be operated in class instances of another class. In programming, to minimize the number of interdependent categories.
2) Polymerization: A class contains another class.

Polymorphism: have the same form, but different parameters for different functions. Overload on polymorphic. Polymorphism has two forms: overloading and coverage.

Superclass: In Java, its library in all classes inherit from the Object class. Object class is a Java class library that is the parent of all classes, but the writing class, so do not write: class student extends Object; because the system automatically think Object is the parent class student class.

Object class equals method for determining whether a test object and another object are equal. Object, is to determine whether two objects point to the same memory area. In fact, it equals the string processing method covers the equals method of Object class.

Sometimes variable of type Object may be used to represent any type of object:
Object obj = new new Student ();
sometimes can be converted directly into the data type of a variable of type Object:
Object obj = (Object) X;

class inhert{
	//构造函数 系统默认的构造函数不带参数,如果需要带参数,需要自己书写
	inhert(){
		a=10;
		b=20;
	}
	inhert(int a) {
		this.a=a;
		b=10;
    }
	inhert(int a,int b){
		this.a=a;
		this.b=b;
	}
	public int a;
	public int b;
	void print(){
		System.out.println("a:"+a+"  b:"+b);
	}
}

public class ExtendsAndPolymorphic extends inhert{
	private int c;
	ExtendsAndPolymorphic()	{}
	ExtendsAndPolymorphic(int a,int b,int c){
		super(a,b); //调用父类带两个参数的构造函数
		this.c=c;
	}
	void print(){
		System.out.println("a:"+a+"  b:"+b+"  c:"+c);
	}
	public static void main(String args[]){
		//测试构造函数
		inhert ob1=new inhert();
		ExtendsAndPolymorphic ob2=new ExtendsAndPolymorphic(1,2,3);
		ob1.print();
		ob2.print();
		//利用多态性质调用相应方法
		ExtendsAndPolymorphic ob3=new ExtendsAndPolymorphic();
		ob3.print(2);
		ob3.print(2,3);
		ob3.print(2,3,5);
		ob3.print(1.1);
		ob3.print(1.1,2.2);
		ob3.print(1.1,2.2,3.3);
	}
	/*以下为(多态)重载函数*/

	void print(int x)
	{System.out.println(2*x);}
	void print(int x,int y)
	{System.out.println(2*x+y);}
	void print(int x,int y,int z)
	{System.out.println(2*x+y*z);}
	void print(double o)
	{System.out.println(2*o);}
	void print(double o,double p)
	{System.out.println(2*o+p);}
	void print(double o,double p,double q)
	{System.out.println(2*o+p+q);}
}

Guess you like

Origin blog.csdn.net/qq_45613931/article/details/101993151
Recommended