简体   繁体   English

接口和继承如何影响Java中类与对象之间的关系?

[英]How do interfaces and inheritance affect the relationship between classes and objects in Java?

As I continue to expand my knowledge of OOP programming, it's becoming a bit more difficult to tie all the concepts back together. 随着我继续扩展OOP编程知识,将所有概念重新结合在一起变得越来越困难。 Separately, I know what an object, class, interface, and inheritance is. 另外,我知道什么是对象,类,接口和继承。 However, I fail to see "the big picture." 但是,我看不到“大局”。

An object embodies a specific concept (class) and stores specific data to that class. 对象体现了特定的概念(类),并将特定的数据存储到该类。 It's an instance of a class, to be more specific. 更具体地说,它是一个类的实例。

A class defines the common set of attributes and behaviors among all objects of its class ("the template") 一个类定义了该类的所有对象(“模板”)之间的公共属性和行为集

Interfaces are essentially a to-do list of method headers. 接口本质上是方法标题的待办事项列表。 It expects its children to implement these abstract methods. 它期望其子级实现这些抽象方法。 It does this in an attempt to create a standard way of doing something in unrelated classes (ex: compareTo() in Comparable) 这样做是为了尝试在不相关的类中创建做某事的标准方式(例如:Comparable中的compareTo())

Inheritance allows subclasses to extend the design space for objects by adding more functionality and behaviors while still retaining that of the superclass. 继承允许子类通过添加更多功能和行为来扩展对象的设计空间,同时仍保留超类的功能和行为。

All these concepts affect the way objects interact with classes, but I fail to see how inheritance and interfaces have anything to do with objects. 所有这些概念都影响对象与类交互的方式,但是我看不到继承和接口与对象有什么关系。 This is a rather conceptual question, but what exactly is this "big picture?" 这是一个相当概念性的问题,但是“全局”到底是什么? How do all these concepts affect the relationship between classes and objects? 所有这些概念如何影响类与对象之间的关系?

Inheritance is the OO concept which has many types like Single, Hybrid etc. Java supports Multiple inheritance in the way of Interface. 继承是OO概念,它具有许多类型,例如Single,Hybrid等。Java以Interface的方式支持Multiple继承。 Interface allows you to define the behaviour or functionality in abstract level. 接口允许您在抽象级别定义行为或功能。 It leaves the children to implement those behaviour or functionality. 它让孩子们去实现那些行为或功能。 For eg(given in official docs): You can define the behaviour of a Bicycle like gearUp, gearDown, pedal, break, etc., and let the brands to implement their style and functionality. 例如(在官方文档中给出):您可以定义Bicycle的行为,例如gearUp,gearDown,踏板,break等,并让品牌实现其样式和功能。 Below is the java doc link. 以下是java doc链接。 https://docs.oracle.com/javase/tutorial/java/concepts/interface.html https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

Inheritance in simple terms is to acquire the properties / behaviours of an other class. 简单来说,继承就是获取其他类的属性/行为。 In order to achieve Inheritance we have to use a keyword extends in JAVA. 为了实现继承,我们必须在JAVA中使用关键字extends。

Class B extends Class A means Class B will act just like class A and all the methods and Variables in class A can be referred using class B also. B类扩展了A类,这意味着B类将像A类一样起作用,并且A类中的所有方法和变量也可以使用B类来引用。 ie. 即。 Whatever class A possesses it is shared by class B too. 无论A类拥有什么,B类也共享。

Whereas Interface is a Type Definition Block (User-Defined). 而接口是类型定义块(用户定义)。 An abstract (incomplete) class can be called Interface. 抽象(不完整)类可以称为Interface。 An Interface allows only Incomplete methods. 接口仅允许使用不完整的方法。 In order to Inherit an Interface we have a keyword implements in JAVA. 为了继承接口,我们在JAVA中有一个关键字实现。 If a Class implements an Interface it is bound to contract. 如果一个类实现一个接口,则它必定会契约。

That is: Either the class implementing the Interface should be declared abstract OR the class should over-ride the method in Interface and Complete it.( ie. provide method body). 也就是说:应该将实现接口的类声明为抽象,或者该类应重写接口中的方法并完成该方法(即提供方法主体)。

Example: 例:

Interface Car

{ public void display(); //Incomplete Method, Method has no Implementation.

}

class B implements Car

{ public void display()

{ System.out.println(“HI”);

} }

Here display method is over-rided and completed in class B. Hence no need to declare class B as abstract. 在这里,显示方法是在B类中重写并完成的。因此,无需将B类声明为抽象。

NOTE: Multiple Inheritance using class is not possible in JAVA. 注意:在JAVA中无法使用类进行多重继承。 ie. 即。 A child class can extend or have only one parent class. 子类可以扩展或只有一个父类。

Multiple Inheritance is possible in JAVA using Interfaces. 使用接口在JAVA中可以进行多重继承。 ie. 即。 An interface can extend more than one interface / implement more than one interface. 一个接口可以扩展多个接口/实现多个接口。

Interface Objects cannot be created, because it is incomplete. 无法创建接口对象,因为它不完整。

Interface methods should be made non-static, because it is incomplete and should be over-rided in order to make it complete. 接口方法应设为非静态的,因为它是不完整的,并且应该被重写以使其完整。 If interface methods are static, it cannot be over-rided, because only one copy of static members are present and that is loaded during class loading. 如果接口方法是静态的,则不能覆盖它,因为仅存在一个静态成员副本,并且该副本在类加载期间加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM