inherit
The benefit of inheritance is code reuse. Inherited subclasses automatically have all the properties and methods in the parent class. So inheriting existing classes is the way and domain to reuse them. On this basis, subclasses can also add new methods and domains to meet new requirements.
Direct example:
//Parent Class public class Father { public void doSome1(){ System.out.println("Parent Class doSome1"); } public void doSome2(){ System.out.println("Parent Class doSome2"); } } //Subclass public class Son extends Father{ @Override public void doSome1() { System.out.println("Subclass Override doSome1"); } public void doOther(){ System.out.println("Subclass doOther"); } } //Test Class public class TestExtend { public static void main(String[] args) { Son son = new Son(); son.doSome1();//Subclass Override son.doSome2();//Inherit from parent son.doOther();//Subclass Extension } }
Test class run results:
polymorphic
First of all, there are several concepts, "Transition up", "Reference from parent class to subclass", "Automatic type transformation". They all mean one thing. Like code: Father father = new Son(); Here father is the reference.
Literal understanding of polymorphism: many forms, many states.
The java program is divided into compile and run phases. Polymorphism is actually one form at compile time and another form at run time. The compiler at compile phase considers father object to be Father type. Father can only access methods of Father class. If it accesses subclasses, errors will be made, such as the method doOther() accessing subclass Son, which will become popular:
But it comes from the Son class new, which is essentially the Son class, and what the runtime really does is implement is the method of the Son class.
public class TestExtend { public static void main(String[] args) { Father father = new Son(); father.doSome1(); } }
Test class execution results:
summary
Following the new keyword is the nature of the object, and "reference" restricts the object's functionality only at the compilation stage.
The role of polymorphism and inheritance in development
In order to improve the extensibility of the program and reduce the coupling of the program, we use abstract-oriented, Interface-oriented programming, for example, we have such an action as "feed dog", "feed cat"... We can do this with code:
//Dog public class Dog { public void eat(){ System.out.println("Dogs eating"); } } //cat public class Cat { public void eat(){ System.out.println("Cats eating"); } } //test public class Master { public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); feed(dog); feed(cat); } public static void feed(Dog a){ a.eat(); } public static void feed(Cat a){ a.eat(); } }
It's easy to see the bugs in the above code. Every time I feed an animal, I need to write a feed method, while Interface-oriented programming, optimized code:
//Write an abstract interface, whether it's a dog, a cat, a chicken, a goose, an animal public interface Animal { public void eat(); } //Dog public class Dog implements Animal { public void eat(){ System.out.println("Dogs eating"); } } //cat public class Cat implements Animal{ public void eat(){ System.out.println("Cats eating"); } } //test public class Master { public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); feed(dog); feed(cat); } public static void feed(Animal a){ a.eat(); } }
With interface programming, we only need to write a feed method to feed all the animals.