Polymorphism in JAVA

/*
 * Polymorphism: It can be understood as a variety of manifestations of the existence of things
 *
 * People: men, women
 * Animals: cats, dogs
 * cat x = new cat();
 * animal x = new cat(); this is polymorphism
 *
 * 1. The basic embodiment of polymorphism
 * The reference of the parent class points to its own subclass object
 * The reference of the parent class can also receive its own subclass object
 *
 * 2, the premise of polymorphism
 * There must be a relationship between classes (inheritance||implementation)
 * Usually there is also a premise: there is an override
 *
 * 3, the benefits of polymorphism
 * The emergence of polymorphism greatly improves the scalability of the program
 *
 * 4, the disadvantages of polymorphism
 * Improves extensibility, but members of the parent class can only be accessed using the reference of the parent class
 *
 * */

//Animal class, animals all need to eat, extract the eat() method, but each animal eats different things, so abstract
abstract class Animal {
	abstract void eat();
}

//The cat class, inherits the animal class, overwrites the eat() method in the parent class, and has its own unique method to catch mice
class Cat extends Animal {
	void eat() {
		System.out.println("eat fish");
	}

	public void CatchMouse() {
		System.out.println("Catch the mouse");
	}
}

//Dog class, inherits animal class, overwrites the eat() method in the parent class, and has its own unique method for housekeeping
class Dog extends Animal {
	void eat() {
		System.out.println("eat bones");
	}

	void WatchHome() {
		System.out.println("Housekeeping");
	}
}

public class DuoTaiDemo {
	public static void main(String args[]) {
		/*
		 * Cat c=new Cat(); c.eat(); Dog d=new Dog(); d.eat();
		 */
		Animal c = new Cat();
		function(c);
		// If you want to call a cat-specific method, how to do it
		// Force the parent class reference to the child class type
		// Cat a = (Cat) c;
		// a.CatchMouse();
		Animal d = new Dog();
		function(d);

	}

	public static void function(Animal c) {
		c.eat();
		/*if (c instanceof Cat) {
			Cat a = (Cat) c;
			a.CatchMouse();
		}
		if (c instanceof Dog) {
			Dog a = (Dog) c;
			a.WatchHome();
		}*/
		
	}
}

The improvement of scalability lies in: for example, the above code is from half a year ago. I now have a pig class and want to add it, then I only need to add a pig class to inherit the animal class, and there is no need to change the source code. If I don't need polymorphism, I have to add the usage of the pig class to the main() function. It seems acceptable in this example, but in actual development, this is extremely time-consuming. So, using polymorphism, we improve the scalability of the program and reduce the coupling.

for example:

/*
 * Basic students:
 * Study, sleep.
 * Advanced students:
 * Study, sleep.
 * These two types of things can be extracted
 *
 * */

abstract class Student3 {
	public abstract void study();

	public void sleep() {
		System.out.println("Lie down and sleep");
	}
}

class BaseStudent2 extends Student3 {
	public void study() {
		System.out.println("base study");
	}

	public void sleep() {
		System.out.println("Sit and sleep");
	}
}

class AdvStudent2 extends Student3 {
	public void study() {
		System.out.println("adv student");
	}
}

class DoStudent{
	public void doSome(Student3 a) {
		a.study();
		a.sleep();
	}
}

public class DuoTaiDemo2 {
	public static void main(String args[]) {
		
		DoStudent ds = new DoStudent ();
		
		BaseStudent2 bs = new BaseStudent2();
		ds.doSome(bs);
		AdvStudent2 as = new AdvStudent2();
		ds.doSome(as);

	}
//	public static void doSome(Student a) {
//		a.study();
//		a.sleep();
//	}
}

In the above example, because all students (basic students, advanced students) are calling the learning and sleeping methods, we can also encapsulate this call into a function, or encapsulate it into a class, which further reduces the degree of coupling. Fewer programs are exposed.


                                                                                        ----------------------By chick

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325563179&siteId=291194637