Basic Applications of Polymorphism and Classification of Internal Classes

I. Two Realizations of Polymorphism

  • 1. Using parent class as method parameter to realize polymorphism:
    Inheritance polymorphism is formed when the parent class as a parameter is a common class or an abstract class, and interface polymorphism is formed when the parent class as a parameter is an interface.
  • 2 Use parent class as method return value to realize polymorphism

(1). Polymorphism as a formal parameter

The formal parameters are introduced types: ordinary classes, Abstract classes, interfaces

1. Common class: When a parameter wants us to pass in a common class, we actually pass in an object/anonymous object of that class.
public class ArgsDemo01 {
	public static void main(String[] args) {
		// Requirements: Access the method method method in StudentDemo
			//StudentDemo sd = new StudentDemo();			
			//Using anonymous objects to simplify code;			
			//new Student() {@Override
			//public void study() {
			//	System.out.println("Student subclass study");
			//}
		//} This is a subclass anonymous object of the student that overrides the method
			new StudentDemo().method(new Student() {		
			public void study() {
				System.out.println("Student Subclass study");
			}
		});
	}
}
class Student {
	public void study() {
		System.out.println("Student.study()");
	}
}
class StudentDemo {
	public void method(Student s) {
		s.study();
	}
}
2. Abstract class: When a parameter wants us to pass in an abstract class, we actually pass in a subclass object/subclass anonymous object of that class
public class practice1 {
	public static void main(String[] args) {
		Person s= new students();
//		Requirements: access tap
		s.tap();
//		Using Anonymous Objects to Simplify
		new students().tap();
//		Rewrite Person's anonymous subclass object. tap
		new Person() {
			public void tap() {
				System.out.println("456");				
		}
		}.tap();
//		Requirements: access methods
		Year sYear = new Year();
//		Method 1: Point to child class object by parent class reference
		sYear.methods(new students());
//		new Person() {
//		public void tap() {
//			System.out.println("789");				
//		}
//	} This is an anonymous subclass object that overrides the parent class
		sYear.methods(new Person() {
			public void tap() {
				System.out.println("789");				
			}
		});
//		Mode 2: Use anonymous objects and add functions
		new Year().methods(new Person() {
			public void tap() {
				System.out.println("789");	
				System.out.println("456");
			}
		});		
	} 
}

class Year{
	public void methods(Person s) {
		s.tap();
	}
}
abstract class Person{
	public abstract void tap();
}
class students extends Person{
	public void tap() {
		System.out.println("China 123");
	}
}
3. Interface: When a parameter wants us to pass in an interface, what we actually pass in is the implementation class object / implementation class anonymous object of that class
public class practice1 {
	public static void main(String[] args) {
		IPerson sIPerson= new studentsImpl();
//		Visit tap
		sIPerson.tap();
		new studentsImpl().tap();
		new IPerson() {
			public void tap() {
				System.out.println("456");				
			}
		}.tap();
//		Access methods
		Year sYear = new Year();
		sYear.methods(new studentsImpl());
		sYear.methods(new IPerson() {
			public void tap() {
				System.out.println("789");				
			}
		});
		new Year().methods(new IPerson() {
			public void tap() {
				System.out.println("789");	
				System.out.println("456");
			}
		});
		
	} 
}
class Year{
	public void methods(IPerson s) {
		s.tap();
	}
}
interface IPerson{
	public abstract void tap();
}

class studentsImpl implements IPerson{
	public void tap() {
		System.out.println("China 123");
	}
}

(2) Polymorphism as a Return Value

Return value types: common class, Abstract class, interface

  • General category
    When the return value of a method is an ordinary class, it actually returns an object of that class, which we can receive with the object of that class.
  • abstract class
    When the return value of a method is an abstract class, it actually returns a subclass object of that abstract class, which we can use to receive.
  • Interface
    When the return value of a method is an interface, it actually returns an implementation class object of the interface, which we can receive using the interface.

II. Internal Classes

  • 1. The concept of internal classes: Define classes inside classes, and then they become internal classes.
    Note: Internal class is a relative concept. If there is a class B in Class A, then Class A is an external class relative to Class B, and Class B is an internal class relative to Class A.

  • 2. Internal Classification
    1. Membership inner class
    Format:
    External class name. Internal class name object name = external class object. Internal class object

*/
public class InnerClassDemo03 {
   public static void main(String[] args) {
      //Naming Internal Classes
   	OuterClass3.InnerClass oi = new OuterClass3().new InnerClass();
   	oi.show();		
   	oi.setNum2(100);
   	System.out.println(oi.getNum2());
   }
}

class OuterClass3{	
   // Member location
   private int num = 10;
   //Inner class
    class InnerClass{		
   	private int num2 = 20;		
    public void show() {
    		System.out.println(num);
   		System.out.println(num2);
   	}		
   	public void setNum2(int num2) {
   		this.num2 = num2;
   	}
   	
   	public int getNum2() {
   		return this.num2;
   	}
   }
   
}

2. Local Internal Classes

package com.sxt.innerclassdemo;
/*
 * Local inner class
 */
public class InnerClassDemo04 {
	public static void main(String[] args) {
		OuterClass4 outerClass4 = new OuterClass4();
		outerClass4.show();
	}
}

class OuterClass4{
	
	class InnerClass{
		
	}
	
	public void show() {
		
		int num = 50;
		//Local inner class
		class InnerClass{
			private int num = 100;
			public void method() {
				System.out.println(num);
			}
		}		
		InnerClass innerClass = new InnerClass();
		innerClass.method();
		System.out.println(innerClass.num);		
	}
}

3. Static internal classes

package com.sxt.innerclassdemo;

/*
* Static inner class
* Membership inner classes can also be modified with static
* And member inner classes can also use private modifications
* 
* Access format:
* 	External class name. Internal class name object name = new external class name. Internal class name ();
* The main members of static internal classes are static members, which can be easily invoked while improving the security of classes.
* Static inner classes can have static and non-static members
*/
public class InnerClassDemo05 {
   public static void main(String[] args) {
//		OuterClass5.InnerClass oi = new OuterClass5.InnerClass();
//		System.out.println(oi.num);
//		oi.show();
//		oi.show2();
   	System.out.println(OuterClass5.InnerClass.num2);
   	OuterClass5.InnerClass.show2();
   }
}

class OuterClass5{
   private int num = 5;
   public static int num2 = 10;
   
   public static class InnerClass{
   	public int num = 10;
   	public static int num2 = 20;
   	
   	public void show() {
   		System.out.println("InnerClass.show()");
   	}
   	
   	public static void show2() {
   		System.out.println("InnerClass.show2()");
   	}
   }	
   public static void method() {
   	InnerClass innerClass = new InnerClass();
//		System.out.println(num);
   }
}

4. Anonymous inner classes

package com.sxt.innerclassdemo;
/*
* Anonymous Inner Class
* Concept: Essentially, it's a subclass anonymous object
* Characteristic:
* 	1.Is a subclass
* 			General category
* 			abstract class
* 			Interface
* 	2.Unnamed subclasses
* 	3.Is an object
* 
* Format:
* 	new Class or parent/abstract class/interface (){
* 		//Rewriting method;
* 	}
* 
* The essence of an anonymous inner class is an inheritance (parent class) or implementation (interface) of a subclass anonymous object
*/
public class InnerClassDemo07 {
   public static void main(String[] args) {
   	new Student();
   	// If the subclass is a generic class
   	// Anonymous object
   	new Student().show();
   	// Anonymous subclass object
   	new Student() {}.show();
   	// Overrides the anonymous subclass object of the show method
   	new Student(){
   		
   		public void show() {
   			System.out.println("Zi Student.show()");
   		}
   	}.show();
   	
   	// Polymorphism
   	AbsTeacher teacher = new PrimaryTeacher();
   	teacher.method();
   	
   	// Anonymous inner class + polymorphism
   	// This is a subclass anonymous object of AbsTeacher that inherits and overrides method methods
   	new AbsTeacher() {

   		@Override
   		public void method() {
   			// TODO Auto-generated method stub
   			System.out.println("Zi PrimaryTeacher.method()");
   		}
   		
   	}.method();
   	
   	// Anonymous inner class + polymorphism
   	// This is a subclass anonymous object that implements the Inter interface and overrides the method method method
   	new Inter() {
   		public void method() {
   			System.out.println("Inter impl");
   		};
   	}.method();
   	
   	new IDemo() {
   		
   		@Override
   		public void method() {
   			System.out.println("method impl");
   		}
   		
   		@Override
   		public void method2() {
   			System.out.println("method2 impl");
   		}
   		
   	}.method();
   	
   	/*new IDemo() {
   		
   		@Override
   		public void method() {
   			System.out.println("method impl");
   		}
   		
   		@Override
   		public void method2() {
   			System.out.println("method2 impl");
   		}
   		
   	}.method2();*/
   	System.out.println("------------------------------");
   	// Polymorphism
   	IDemo iDemo = new IDemo() {
   		
   		@Override
   		public void method() {
   			System.out.println("method impl");
   		}
   		
   		@Override
   		public void method2() {
   			System.out.println("method2 impl");
   		}
   		
   	};
   	iDemo.method();
   	iDemo.method2();
   	
   	// Test method Method
//		ICar car = new CarImpl();
   	new CarDemo().method(new ICar() {
   		
   		@Override
   		public void run() {
   			System.out.println("impl ICar2");
   		}
   	}).run();
   }
}

class CarDemo{
   // ICar c = new ICar() {
   
   //	@Override
   //	public void run() {
   //		System.out.println("impl ICar2");
   //	}
//	}
   public ICar method(ICar c) {
   	c.run();
//		ICar car = new CarImpl();
   	return new ICar() {
   		
   		@Override
   		public void run() {
   			System.out.println("impl Icar");
   		}
   	};
   }
}

class CarImpl implements ICar{

   @Override
   public void run() {
   	System.out.println("CarImpl.run()");
   }
   
}

interface ICar{
   public void run();
}

interface IDemo{
   void method();
   void method2();
}

interface Inter{
   void method();
}

abstract class AbsTeacher{
   public abstract void method();
}

class PrimaryTeacher extends AbsTeacher{

   @Override
   public void method() {
   	System.out.println("Fu PrimaryTeacher.method()");
   }
   
}

class Student{
   public void show() {
   	System.out.println("Fu Student.show()");
   }
   
}

//Classic questions
/*

public class InnerClassDemo08 {
   public static void main(String[] args) {
   	OuterClass8.Inner oi = new OuterClass8().new Inner();
   	oi.show();
   }
}

class Fu{
   
   public int num = 40;
   
   public void show() {
   	int num = 50;
   	System.out.println(num);
   }
}

class OuterClass8{
   public int num = 10;
   
   class Inner extends Fu{
   	public int num = 20;
   	
   	public void show() {
   		int num = 30;
   		// Output 30
   		System.out.println(num);
   		// Output 20
   		System.out.println(this.num);
   		// Output 40
   		System.out.println(super.num);
   		// Output 50
   		super.show();
   		// Output 10
   		System.out.println(OuterClass8.this.num);
   	}
   }
}

Posted by moiseszaragoza on Mon, 22 Apr 2019 14:45:34 -0700