Java SE - (f): interfaces and abstract classes

A, should not be instantiated package com.zhangguo.chapter5.s1;

Package Penalty for com.zhangguo.chapter5.s1;

/ ** Zoo * / 
public  class Zoo {
     public  static  void main (String [] args) {
        Animal animal=new Animal();
        animal.eat();
        
        / ** new new tone who is who * / 
        / ** the LSP * / 
        Animal Dog = new new Dog ();
        dog.eat();
    }
}

/ ** Animals * / 
class Animal {
     / ** eat * / 
    public  void EAT () {
        System.out.println ( "Animals eat" );
    }
}

class Cat the extends Animal {
     / ** rewrite eat * / 
    public  void EAT () {
        System.out.println ( "cat fish" );
    }
}

class Dog the extends Animal {
     / ** rewrite eat * / 
    public  void EAT () {
        System.out.println ( "dog eat bones" );
    }
}

result:

problem:

As can be seen from the above example Animal abstract parent class, in fact, in reality there is no actual object called the animal, and the animal is just an abstract concept to be.

In this case, Animal should not be instantiated, only as a parent, the object-oriented (OOP) act as the role of this type are: abstract class interface.

Abstract classes and interfaces are more abstract than a type of class.

First, the type can not be instantiated

From the above concepts can be learned in some type should not be instantiated, it does not make sense.

java abstract class is more conducive to the maintenance and code reuse.

1. Because the abstract class can not be instantiated object, so there must be a subclass to achieve before you can use it. So that you can put some of the components have the same properties and methods of abstraction, so that is more conducive to the maintenance of code and procedures.

2. When there is generated a similar assembly, need only implement the abstract class can be obtained to those properties and methods of the abstract class.

In object-oriented approach, the abstract class type is mainly used for hidden. Abstract description of a configuration of a fixed set of behaviors, but this group is able to conduct any possible specific implementations. This abstract description is an abstract class, and this group of any number of possible concrete realization of the performance for all possible derived classes. Module may operate in an abstract. Because the module relies on a fixed abstraction, so it can not be modified; the same time, by deriving from the abstract of this module can also be extended for this behavior function. In order to achieve one of the most core principles OCP (Open-Closed Principle) object-oriented design, abstract class is one of the key.

(1), the interface

(2), an abstract class

(3), access to the constructor is private

Package Penalty for com.zhangguo.chapter5.s1;

/ ** eating interfaces * / 
interface Ieatable {
     void EAT ();
}

/ ** Animals abstract class * / 
abstract  class Animal {
     / ** eating abstract methods * / 
    public  abstract  void EAT ();
}

/ ** Student general classes * / 
class Student {
     / ** private constructor * / 
    Private Student () {
    }
}

public class NoInstance {

    public static void main(String[] args) {
        OBJ1 Ieatable = new new Ieatable (); // Error interfaces can not be instantiated 
        Animal obj2 = new new Animal (); // error can not instantiate abstract class 
        Student OBJ3 = new new Student (); // error can not be instantiated private constructor for class 
    }
}

 

Some of the language in a static class can not be instantiated, such as C #

Meaning: the more abstract, more stable. Abstract superstructure can be defined, standardized top-level design. Abstract will not and should be freely changed.

Second, the abstract class

2.1 syntax definitions

Abstract class definition, before the use of abstract classes abstract keyword modification, the class is an abstract class.

2.2 Use

a, in some cases, a parent knows just how it should contain a subclass method, but you can not know exactly how to implement these methods of these subclasses
(subclass must be abstract class constraints in what ways, but it does not concern child class how to implement these methods.)

B, from a plurality of classes having the same characteristic abstracting an abstract class, an abstract class as a template with this subclass, the subclass of the design so as to avoid randomness.

2.3, meaning

Restrictions subclass must implement certain methods, but does not care about the implementation details.

2.4 Features

1, certain abstract method in an abstract class

2, abstract methods and abstract classes must be modified keywords

3, abstract class can not create an object with new. Because the abstract method called meaningless 4, abstract class abstract method to be used, must be overwritten by a subclass from all the abstract methods, create sub-class object calls.

If a subclass only covers a part of the abstract method, then this is an abstract class subclass.

5, there is no method body abstract method, end with a semicolon

Example:

package com.zhangguo.chapter5.s2;

import java.util.Scanner;

/** 动物 */
public abstract class Animal {
    /** 名称 */
    public String name;

    /** 抽象方法,无方法体,必须被子类实现(重写) */
    public abstract void eat();
    
    /**测试*/
    public static void main(String[] args) {
        //LSP 里氏替换原则
        Animal dog=new Dog();
        dog.name="博美";
        //int i=1;
        //Scanner input=new Scanner(System.in);
        dog.eat();
    }
    
    /**抽象类中可以有非抽象方法,可以有静态方法*/
    public void show(){};
}

/**抽象类动物(Animal)的子类,必须实现父类未实现的方法*/
class Dog extends Animal {
    //注解
    @Override
    public void eat() {
        System.out.println(this.name+"狗在吃骨头");
    }
}

 

运行结果:

三、接口

接口是一组没有实例的标准与规范。

没有接口的电脑是怎样的?

3.1、为什么需要接口

继承:描述事物的自然属性和行为的复用。

接口:描述事物的社会属性和行为的复用。

1、重要性:在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力。

2、简单、规范性:如果一个项目比较庞大,那么就需要一个能理清所有业务的架构师来定义一些主要的接口,这些接口不仅告诉开发人员你需要实现那些业务,而且也将命名规范限制住了(防止一些开发人员随便命名导致别的程序员无法看明白)。

3、维护、拓展性:比如你要做一个画板程序,其中里面有一个面板类,主要负责绘画功能,然后你就这样定义了这个类。

4、安全、严密性:接口是实现软件松耦合的重要手段,它描叙了系统对外的所有服务,而不涉及任何具体的实现细节。这样就比较安全、严密一些(一般软件服务商考虑的比较多)。

因为类具有“单根性”,所有的类只能有一个直接父类,通过可以实现一个类有多个父类,可以实现多重继承。

package com.zhangguo.chapter5.s2;

/**usb接口*/
public interface IUSB {
    /**未实现的方法,发送数据*/
    void sendData();
}

/**网线接口*/
interface IRJ45
{
    /**未实现的方法,接收数据*/
    void receiveData();
}

/**设备*/
class Device{
    
}

/**电脑*/
/**一个类只能继承一个类,但可以实现多个接口*/
class Computer extends Device implements IUSB,IRJ45{

    @Override
    public void receiveData() {
        System.out.println("接收数据");
    }

    @Override
    public void sendData() {
        System.out.println("发送数据");
    }
    
    
    interface IA{}
    interface IB{}
    /**接口可以继承其它他口*/
    interface IC extends IA,IB{}
    
    class CC{}
    /**继承需要写在实现接口前*/
    class DD extends CC implements IC {}
}

 

测试:

package com.zhangguo.chapter5.s2;

public class ComputerClient {

    public static void main(String[] args) {
        Computer ln=new Computer();
        ln.sendData();
        ln.receiveData();
        
        /**接口是一种类型*/
        IUSB usb=new Computer();
        
        /**一个对象可以有多个不同的类型*/
        
    }

}

 

3.2、接口的特点

1)、接口中的方法可以有参数列表和返回类型,但不能有任何方法体。

2)、接口中可以包含字段,但是会被隐式的声明为static和final。

3)、接口中的字段只是被存储在该接口的静态存储区域内,而不属于该接口。

4)、接口中的方法可以被声明为public或不声明,但结果都会按照public类型处理。

5)、当实现一个接口时,需要将被定义的方法声明为public类型的,否则为默认访问类型,Java编译器不允许这种情况。

6)、如果没有实现接口中所有方法,那么创建的仍然是一个接口。子类必须实现接口中未实现的方法,除非子类也是接口。

7)、扩展一个接口来生成新的接口应使用关键字extends,实现一个接口使用implements。

8)、接口中的方法是抽象方法(abstract),不能是静态方法(static))、接口的所有方法都是抽象的,而抽象方法是没有static,有static的方法是不能override的,所以这样定义接口才有意义。

接口中的字段是默认为:static final ,通俗说就是常量

四、Final(最终的)

4.1、final修饰类

  final修饰的类不允许被继承。

  一个类不能既是final的,又是abstract的。因为abstract的主要目的是定义一种约定,让子类去实现这种约定,而final表示该类不能被继承,两者矛盾。

4.2、final修饰方法

  final修饰方法,表示该方法不能被子类中的方法覆写Override。不能被重写

4.3、final修饰变量

  final成员变量表示常量,只能被赋值一次,赋值后值不再改变。

  当final修饰一个原生数据类型时,表示该原生数据类型的值不能发生变化;

  如果final修饰一个引用类型时,表示该引用类型不能再指向其他对象了,但该引用所指向的对象的内容是可以发生变化的。

  本质上是一回事,因为引用的值是一个地址,final要求值,即地址的值不发生变化。

  final修饰一个成员变量(属性),必须要显示初始化。

  这里有两种初始化方式,一种是在变量声明的时候初始化;第二种方法是在声明变量的时候不赋初值,但是要在这个变量所在的类的所有的构造函数中对这个变量赋初值。

  当函数的参数类型声明为final时,说明该参数是只读型的。

五、视频与示例下载

上课示例下载

B站视频在线观看

六、面试题

1、Java中有那些不能实例化的类型?

2、抽象类有何特点?

3. What are the characteristics of the interface?

Guess you like

Origin www.cnblogs.com/jiekun/p/6881878.html