One of the characteristics of an object-oriented java - Polymorphism

An object-oriented four characteristics: abstraction, inheritance, encapsulation, polymorphism,
two conditions to achieve polymorphism: an inherited
     2. rewritable
     3. upcast (parent class subclass object references to)
more than three states. implementation: 1 interface
     2. the override inherited from the parent class the parent class method
     3. a method of rewriting the same
four polymorphic appreciated
parent code


public class Father {

	public Father() {
		System.out.println("我是父类构造");
	}
	
	public void name() {
		System.out.println("我是父类的name方法");
	}
}

Subclass code

public class Son extends Father {

	public Son() {
		System.out.println("我是子类构造");
	}
	
	public void name() {
		System.out.println("我是子类的name方法");
	}
	
	public void age() {
		System.out.println("我是子类的age方法,父类没有");
	}
}

Test code: It is a very simple example of polymorphism, inheritance Father Son, override the parent class name in the test code, references to the parent class subclass object, invoke methods overridden, execution of specific sub class or parent class it is called a subclass-specific way to do that? We look at the following example.

public class Test {

	public static void main(String[] args) {
		Father f=new Son();
		f.name();
		Son s=(Son)f;
		s.age();
	}
}

The result:
 
Summary:
 parent references subclass method can not be called unique.
The strong reference to the parent class into subclasses only subclass-specific methods can be called
in short go superclass constructor Under normal circumstances, walk subclass constructor, walk subclass override method
Method overloading Note:
L overloaded method parameters must be different:
the number of different parameters, such as the method (int x) and method (int x, int y) various
different types of parameters, such as the method (int x) and method (double x) g different
in different order parameters, such as the method (int x, double y) and the method (double x, int y) different
l reload only the method name associated with the parameter and return value irrespective of the type
as void method (int x) and int method (int y) method is not overloaded, not exist
l overloaded with a particular independent variable identifier
V. pen questions                                                                                                                                                                                 

 public class A {
    public String show(D obj) {
        return ("A and D");
    }

    public String show(A obj) {
        return ("A and A");
    } 

}

public class B extends A{
    public String show(B obj){
        return ("B and B");
    }
    
    public String show(A obj){
        return ("B and A");
    } 
}

public class C extends B{

}

public class D extends B{

}

public class Test {
    public static void main(String[] args) {
        A a1 = new A();
        A a2 = new B();
        B b = new B();
        C c = new C();
        D d = new D();
        
        System.out.println("1--" + a1.show(b));
        System.out.println("2--" + a1.show(c));
        System.out.println("3--" + a1.show(d));
        System.out.println("4--" + a2.show(b));
        System.out.println("5--" + a2.show(c));
        System.out.println("6--" + a2.show(d));
        System.out.println("7--" + b.show(b));
        System.out.println("8--" + b.show(c));
        System.out.println("9--" + b.show(d));      
    }
}

The result:
 
In fact, there is a priority inheritance chain in the call object methods: this.show (O), super.show ( O), this.show ((super) O), super.show ((super) O) .
The two key questions: 1 subclass can be used as a parent class
2. subclass object to use as a parent loses a child class characteristics, can retain the properties and methods of the parent class of the same name
1) A category this method does not exist, a state where a plurality of parameter
2) above
3) of the present method, in the case of direct call.
4) references to the parent class subclasses the object, the case where the lost property subclasses, calls the same method, there is a case polymorphic method with the same name parameter.
5) Ibid.
6) subclass of this method does not exist, a case where the parent class exists, the direct call parent class
7) present in the subclass, direct call.
8) subclass parent class does not exist the process, where the presence of polymorphic subclass method parameters. Subject to the inheritance chain object method call priority
 9) the presence of the parent class method, call the parent class method

Guess you like

Origin blog.csdn.net/qq_36897901/article/details/91481445