Polymorphism
Polymorphic Overview
The same object, manifested in different forms at different times
Polymorphic premise and reflect
- Inheritance / implementation relationship
- There are ways to rewrite relations
- There are references to parent child class object
public class Animal {
public void eat() {
System.out.println("动物吃东西");
}
}
public class Cat extends Animal {
@override
public void eat() {
System.out.println("猫吃鱼");
}
public class AnimalDemo {
public static void main(String[] args) {
Animal a = new Cat();
}
}
Polymorphic members access features
- Member variable : Compile look left, look left execution
- Member method : Compile look left, look to the right of execution
Why is the member variable and it is not the same access method
- Because members of the methods are overridden and member variables are not the same?
public class Animal {
public int age = 40;
public void eat() {
System.out.println("动物吃东西");
}
}
public class Cat extends Animal {
public int age = 20;
public int weight = 10;
@override
public void eat() {
System.out.println("猫吃鱼");
public void playGame() {
System.out.println("猫捉老鼠");
}
}
public class Dog extends Animal {
@override
public void eat() {
System.out.println("狗吃骨头");
public void lookDoor() {
System.out.println("狗看门");
}
}
public class AnimalOperator {
public void useAnimal(Animal a){
//Animal a = new Cat();
//Animal a = new Dog();
a.eat;
}
}
public class AnimalDemo {
public static void main(String[] args) {
Animal a = new Cat();
System.out.println(a.age));
// System.out.println(a.weight));
a.eat();
// a.playGame();
AnimalOperator ao = new AnimalOperator();
Dog d = new Dog();
ao.useAnimal(d);
}
}
Polymorphic benefits and drawbacks
- Polymorphism benefits: improved scalability program (definition of the method when using the parent as a parameter type, when in use in the future, the use of specific types of operations of the participating sub)
- Polymorphic drawbacks: not available subclass-specific functions
Polymorphism in transition
Upcast:
- From child to parent, the parent class subclass object references to
Downcast:
- From parent to child, parent child class object reference into
public class Animal {
public void eat() {
System.out.println("动物吃东西");
}
}
public class Cat extends Animal {
@override
public void eat() {
System.out.println("猫吃鱼");
public void playGame() {
System.out.println("猫捉老鼠");
}
}
public class AnimalDemo {
public static void main(String[] args) {
//多态
Animal a = new Cat(); //向上转型
a.eat();
/*
//创建Cat类的对象
Cat c = new Cat();
c.eat();
c.playGame();
*/
//向下转型
Cat c = (Cat)a;
c.eat();
c.playGame();
}
}
Abstract class
Abstract class overview
In Java, a no method body method should be defined as abstract methods , and class if there is an abstract method , the class must be defined as an abstract class .
Abstract class Features
- Abstract classes and abstract methods must be abstract keyword modified
public abstract class class name []
public abstract void EAT ();- Abstract classes are not necessarily abstract methods, a method of abstract class is an abstract class must
- An abstract class can not instantiate
abstract class is instantiated how to achieve it? Referring polymorphic manner, by subclass object instantiated abstract class called polymorphism- Subclasses of the abstract class
all abstract methods abstract class is either overwritten
or is an abstract class
Features abstract class members
- Member variable
can be a variable
can also be a constant- The method of construction
has a structure methods, but can not be instantiated
action constructor is used to access the initialization data of the parent class- Method members
can abstract methods: defining a subclass must complete certain actions
may also have non-abstract methods: improved code reusability
Cats and dogs abstract classes Case
//创建动物类
public abstract class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void eat();
}
//创建猫类
public class Cat extends Animal {
public Cat() {
}
public Cat(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
public class Dog extends Animal{
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("狗吃骨头");
}
}
public class AnimalDemo {
public static void main(String[] args) {
//创建对象,按照多态的方式
Animal a = new Cat();
a.setName("布偶");
a.setAge(3);
System.out.println(a.getName()+","+a.getAge());
a.eat();
System.out.println("========");
a = new Cat("布偶",3);
System.out.println(a.getName()+","+a.getAge());
a.eat();
}
}
interface
Interface Overview
Interface is a kind of public normative standards , they meet the standards, we can be common
Java interface in more reflected in the abstract behavior
Features Interface
- Interface with the keyword interfa modified
public interface interface name {}- Class implements the interface implements represented by
public class name of the class implements the interface name} {- Interfaces can not be instantiated
interfaces how to instantiate it? Referring polymorphic manner, by implementing the class of the object instance, the interface is called polymorphism.
Polymorphic forms: polymorphic concrete class, an abstract class of multi-state , the interface polymorphism- Interface implementation class
or all abstract methods interface rewrite
either abstract class
Members of the interface features
- Member variables
can only be a constant
default modifier: public static Final- Constructor
interface has no constructor, because the interface is mainly to conduct the abstract, there is no specific presence
of a parent if a class does not explicitly inherit the default inherit the Object class- Member method
can only abstract method is
the default modifier: public abstract
Cats and dogs Interface Case
//定义接口Jumpping
public interface Jumpping{
public abstract void jump();
}
//创建动物类
public abstract class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void eat();
}
//创建猫类
public class Cat extends Animal implements Jumpping {
public Cat() {
}
public Cat(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("猫吃鱼");
}
@Override
public void jump() {
System.out.println("猫可以跳高了");
}
}
public class Dog extends Animal implements Jumpping {
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println("狗吃骨头");
}
@Override
public void jump() {
System.out.println("狗可以跳高了");
}
}
public class AnimalDemo {
public static void main(String[] args) {
//创建对象,调用方法
Jumpping j = new Cat();
j.jump();
System.out.println("========");
Animal a = new Cat();
a.setName("布偶");
a.setAge(3);
System.out.println(a.getName()+","+a.getAge());
a.eat();
a = new Cat("布偶",3);
System.out.println(a.getName()+","+a.getAge());
a.eat();
System.out.println("========");
Cat c = new Cat();
c.setName("布偶");
c.setAge(3);
System.out.println(a.getName()+","+a.getAge());
c.eat();
c.jump();
}
}
Relationship classes and interfaces
- The relationship between classes and
inheritance, only single inheritance, but multiple layers can inherit- The relationship between classes and interfaces
to achieve relations, can achieve alone, can achieve more than can also implement multiple interfaces inherit a class at the same time- The relationship between the interface and the interface of
inheritance, single inheritance can also be multiple inheritance
The difference between abstract classes and interfaces
- Difference members
abstract class: variables, constants; the constructor; abstract method, non-abstract methods
Interface:; abstract method constants- The relationship between the difference between
class and class: inheritance, single inheritance
classes and interfaces: implementation can achieve alone, can achieve more than
the interface and the interface: inheritance, single inheritance, multiple inheritance- Design difference between
abstract class: abstract class, including properties, behavior
Interface: abstract behavior, mainly behavior