10. Object-oriented (abstract classes, interfaces)

Abstract class

review

package org.westos.demo;

/**
 * @author lwj
 * @date 2020/4/20 16:46
 */
public class MyDemo {
    public static void main(String[] args) {
        Panda panda = new Panda();
        //抽象类AbstractAnimal初始化
        //Panda构造方法执行了
        System.out.println(panda.num);
        //20
    }
}

abstract class AbstractAnimal {

    /** 抽象类不能直接实例化,但是抽象类存在构造方法,目的是子类在创建对象时需要初始化父类(抽象类)的数据 */
    public AbstractAnimal() {
        System.out.println("抽象类AbstractAnimal初始化");
        //1
    }

    public void sleep() {
        System.out.println("睡觉");
    }

    /**
     * 抽象方法eat
     */
    protected abstract void eat();
}

class Panda extends AbstractAnimal {
    {
        num = 20;
    }
    int num;

    public Panda() {
        System.out.println("Panda构造方法执行了");
        //2
    }

    @Override
    protected void eat() {
        System.out.println("大熊猫吃竹子");
    }
}

class Cat extends AbstractAnimal {

    public Cat() {
        System.out.println("Cat构造方法执行了");
    }

    @Override
    protected void eat() {
        System.out.println("猫咪吃小鱼干");
    }
}

Features of abstract class members

  • Member variables: can be either variables or constants
  • Constructor: exists, used to initialize the parent class data when the child class creates an object
  • Member method: It can be either abstract or non-abstract.
    • Abstract methods: Force subclasses to implement abstract methods
    • Non-abstract methods: subclasses can override the methods of the parent class or not
package org.westos.demo;

/**
 * @author lwj
 * @date 2020/4/20 17:14
 */
public class MyDemo2 {
    public static void main(String[] args) {
        Student student = new Student(2);
        System.out.println(student.nation);
        //中国
        student.show();
        //我是父类
        //子类重写父类的show()方法
        student.abstractMethod();
        //我是子类
    }
}

abstract class AbstractPerson {
    public int id = 1;

    /** final类型变量在定义后,1、在定义声明时初始化,2、在构造代码块中初始化,3、在构造方法中初始化 */
    public final String nation;

    {
        nation = "中国";
    }

    public AbstractPerson(int id) {
        this.id = id;
    }
    /** 抽象方法 */
    public abstract void abstractMethod();

    public void show() {
        System.out.println("我是父类");
    }
}

class Student extends AbstractPerson {

    /*由于父类的构造方法只有一个有参构造,而子类默认无参构造,调用父类的无参构造失败,所以报错,
      解决:子类生成一个有参构造,并且显式调用父类有参构造 */

    public Student(int id) {
        super(id);
    }

    @Override
    public void abstractMethod() {
        System.out.println("我是子类");
    }

    @Override
    public void show() {
        super.show();
        System.out.println("子类重写父类的show()方法");
    }
}

Interview questions in the abstract class

1. If there is no abstract method for a class, can it be defined as an abstract class? If so, what is the point?

Yes, you cannot create such objects.

2. Which keywords can abstract coexist with?

  1. Abstract modification method, subclasses must override this method, private modification method, subclass cannot inherit, it cannot be overridden, so conflict;

  2. Abstract modified class, representing abstract class, subclass inherits abstract class, conflict with final (modified class, modified class cannot be inherited);

  3. Abstract decoration methods, representing abstract methods in abstract classes, subclasses inheriting abstract classes, must implement abstract methods in abstract classes, and conflict with final (decoration methods, modified methods cannot be overwritten by subclasses)

  4. Abstract modification method, representing abstract method, without method body, static modification method represents static method, with method body, does not participate in rewriting, cannot coexist, meaningless.

interface

Features of the interface

  • The interface is represented by the keyword interface, the interface interface name {}
  • The class implements the interface using the keyword implements, the class class implements interface name {}
  • Interface cannot be instantiated, instantiated in a polymorphic way
  • Subclass of interface
    • Abstract class, abstract class class name implements interface name {}
    • Concrete class, to override all abstract methods in the interface, class class name implements interface name {}
package org.westos.demo2;

/**
 * @author lwj
 * @date 2020/4/20 17:33
 */
public class MyDemo {
}

interface IExpandMethod {
    /**
     * 打电话
     */
    public abstract void call();
}

abstract class AbstractAnimal implements IExpandMethod {

}

class Animal implements IExpandMethod {

    @Override
    public void call() {
        System.out.println("具体类必须实现接口中所有方法");
    }
}

Member characteristics of the interface

  • Member variables: can only be constants, and public static, the default modifier: public static final
  • Constructor: There is no constructor in the interface
  • Member method: can only be an abstract method, the default modifier: public abstract

Class and Class, Class and Interface, Interface and Interface

  • Class and class: inheritance relationship, only single inheritance, multiple inheritance;
  • Class and interface: implementation relationship, can be implemented single or multiple, multiple interfaces are ,separated, and multiple interfaces can be implemented while inheriting a class;
  • Interface and interface: inheritance relationship, can be single inheritance, or multiple inheritance.
//类与类:extends,单继承
//类与接口:implements,单实现,多实现
public final class Integer extends Number implements Comparable<Integer> {}
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable{}
//接口与接口:extends,单继承,多继承
public interface List<E> extends Collection<E> {}
public interface SortedMap<K,V> extends Map<K,V> {}

The difference between abstract classes and interfaces

Differences between members:

  • Abstract class:
    • Member variables: variable, constant
    • Constructor: there is a constructor
    • Member methods: Can be abstract, can be non-abstract
  • interface:
    • Member variables: only constants
    • Constructor: There is no constructor
    • Member methods: only abstract

Relationship difference:

  • Class and Class
    • Inheritance
  • Classes and interfaces
    • Realize, single realize, multiple realize
  • Interfaces and interfaces
    • Inheritance, single inheritance, multiple inheritance

Differences in design concepts:

  • Abstract class
    • What is inherited is the relationship of "is a". The abstract class defines the common functions of the inherited class
  • interface
    • What is realized reflects the "like a" relationship, and the extended functions of the inheritance system are defined in the interface

Note: After JDK 1.8, a method decorated with default is provided in the interface , which can give the specific implementation of the function, and the subclass can inherit it.

package org.westos.demo3;

/**
 * @author lwj
 * @date 2020/4/20 20:09
 */
public interface IExpandMethod {
    int NUMBER = 10;

    /**
     * 1、jdk1.7 公共静态常量和public abstract抽象方法
     * 钻火圈
     */
    void fire();

    /**
     * 2、jdk1.8 增加default关键字修饰方法,可以存在方法体,默认权限修饰符为public
     * 展示
     */
    default void show() {
        System.out.println("展示");
    }

    /**
     * 3、jdk1.8 增加static静态方法,默认权限修饰符为public
     * 测试
     */
    static void test() {
        System.out.println("静态方法");
    }
}

class ExpandMethodImpl implements IExpandMethod {

    @Override
    public void fire() {
        System.out.println("钻火圈");
    }
}

class MyDemo {
    public static void main(String[] args) {
        IExpandMethod test = new ExpandMethodImpl();
        test.fire();
        //钻火圈
        test.show();
        //展示
        System.out.println(IExpandMethod.NUMBER);
        //10
        IExpandMethod.test();
        //静态方法
    }
}

Abstract class and interface exercises

Abstract class

package org.westos.demo3;

/**
 * @author lwj
 * @date 2020/4/20 20:36
 */
public abstract class Animal {
    public String name;
    public int age;

    /**
     * 吃饭
     */
    public abstract void eat();

    /**
     * 睡觉
     */
    public abstract void sleep();
}

Implementation class

package org.westos.demo3;

/**
 * @author lwj
 * @date 2020/4/20 20:38
 */
public class Cat extends Animal{
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }

    @Override
    public void sleep() {
        System.out.println("猫睡觉");
    }
}

extensions

package org.westos.demo3;

/**
 * 扩展功能
 * @author lwj
 * @date 2020/4/20 20:42
 */
public interface IHighJump {
    /**
     * 动物跳高
     */
    void highJump();
}

Garfield

package org.westos.demo3;

/**
 * 加菲猫
 * @author lwj
 * @date 2020/4/20 20:40
 */
public class Garfield extends Cat implements IHighJump{
    @Override
    public void eat() {
        System.out.println("加菲猫吃鱼排");
    }

    @Override
    public void sleep() {
        System.out.println("加菲猫睡在沙发上");
    }

    @Override
    public void highJump() {
        System.out.println("加菲猫学会了跳高");
    }
}

Civet cat

package org.westos.demo3;

/**
 * 狸花猫
 * @author lwj
 * @date 2020/4/20 20:44
 */
public class CivetCat extends Cat {
    @Override
    public void eat() {
        System.out.println("狸花猫吃鱼翅");
    }

    @Override
    public void sleep() {
        System.out.println("狸花猫睡在摇篮里");
    }
    
    public void catchMouse() {
        System.out.println("狸花猫抓老鼠");
    }
}

Test Methods

package org.westos.demo3;

/**
 * @author lwj
 * @date 2020/4/20 20:46
 */
public class MyTest {
    public static void main(String[] args) {
        Garfield garfield = new Garfield();
        Cat cat = garfield;
        cat.name = "波斯猫";
        cat.age = 2;
        cat.eat();
        cat.sleep();
        IHighJump jump = garfield;
        jump.highJump();
        System.out.println("===============");
        CivetCat civetCat = new CivetCat();
        cat = civetCat;
        cat.name = "狸花猫";
        cat.age = 3;
        cat.eat();
        cat.sleep();
        CivetCat civetCat1 = (CivetCat) cat;
        //执行CivetCat的特有方法,需要向下转型
        civetCat1.catchMouse();
    }
}

Guess you like

Origin www.cnblogs.com/shawnyue-08/p/12740529.html