Interface
(1) When the methods in the abstract class are abstract, java provides a new form of expression: interface, which is a collection interface of functions. Unable to create object
(2)Format
Parent interface: public interface Tnter{
}
Subclass: public class interImp implements Itner{
}
Use of interface
1. Interface cannot create objects
2. Define implementation class to implement interface
Implementation keyword implements
3. Override the abstract method
4. Create an implementation class object and call the method
Features of the interface
1. Does not need to be modified by abstract
2 .A class implements an interface, which can be implemented singly or in multiple ways
3. An interface can inherit from an interface, either singly or in multiple ways
4. The functions of the interface and the parent class can be repeated, which means they must have a certain function
Member characteristics of interface
Member variables static constants
Fixed modifier public static final
Whether you write it or not, this modifier remains unchanged
Member method
Fixed modifier public abstract
Whether it is written or not, this modifier remains unchanged
Abstract class and Differences in interfaces
1. Differences in members
Abstract class:
Member variable: It can be a variable or a constant
Constructor method: There is a constructor method for instantiation of subclasses Use
Member method: It can be abstract or non-abstract
Interface:
Member variable: It can only be a constant
Default modifier: public static final
Member method : Can only be abstract
Default modifier: public abstract
Recommendation: Please always give the default modifier manually
2. The difference between classes and interfaces
Classes and classes:
Inheritance relationship, only single inheritance, multi-level inheritance is possible
Classes and interfaces:
Implementation relationship, single implementation or multiple implementations are possible
Class also You can inherit a class and implement multiple interfaces at the same time
Interface and interface:
Inheritance relationship, you can have single inheritance or multiple inheritance
3. The concepts reflected are different
What is defined in the abstract class is the common content in an inheritance system
The interface is a collection of functions, an additional function of a system, and the exposed rules
Everything is used Wherever the parent class/interface refers, its subclass/implementation class object can be passed in
Polymorphism
The same object can be reflected at different times Different states
Example: Water (water, ice, water vapor)
Cat (cat, animal)
Prerequisite:
A: There is inheritance or implementation relationship
B: There is method Rewrite
C: A reference from the parent class points to the subclass object
Characteristics of using members in polymorphism
Fu fz=new Zi();
When polymorphic, all expressions are the parent class Expression form
Only when a method is called, the method rewritten by the subclass is run
1. Member variables
Compile and see on the left. Run and run.
2. Member methods
Compile and run. Look at the right
1 class Fu{ 2 int num=4; 3 void show(){ 4 system.out.println("showFu") 5 } 6 7 } 8 class Zi extends Fu{ 9 10 int num=5;11 void show(){12 system.out.println("showZi");13 }14 }15 class T{16 public static void main(String args[]){17 Fu f=new Zi();18 system.out.println(f.num);19 f.show();20 }21 }
Transformation in polymorphism
1. Upward transformation
Assign the subclass object to the parent class (Interface) reference automatic type promotion
int a=0; double b=a;
Fu fz=new Zi();
2. Downcast
Change the parent class (interface) Reference is cast to a subclass object
double b=10.0;
int a=(int)b;
Fu fz=new Zi();
Zi zi=(Zi)fz ;
Note: Fu fu=new Fu() cannot be downcast and will report a ClassCastException type conversion exception
The benefits and drawbacks of polymorphism
1. Benefits
Improved program maintainability and scalability
2. Disadvantages
Unable to apply subclass-specific content
If you want to use it, you must either downcast or re-create the subclass object
Three forms of polymorphism
1. Concrete class polymorphism parent class variable name=new subclass()
2. Abstract class polymorphism parent abstract class variable name=new subclass()
3. Interface polymorphism Interface variable name = new implementation class ()
instanceof keyword
Format: object name instanceof class name
Return value: true false
Function: Determine whether the specified object is an object created by a given class
Animal a1=new Cat();
Animal a2=new Dog();
method(a1)
public static void method(Animal a){
if(a instanceof Cat){
Downward transformation
Call the cat-specific method
}
}
The above is the detailed content of Object-oriented (interface, polymorphism). For more information, please follow other related articles on the PHP Chinese website!

MySQL中如何实现数据的多态存储和多维查询?在实际应用开发中,数据的多态存储和多维查询是一个非常常见的需求。MySQL作为常用的关系型数据库管理系统,提供了多种实现多态存储和多维查询的方式。本文将介绍使用MySQL实现数据的多态存储和多维查询的方法,并提供相应的代码示例,帮助读者快速了解和使用。一、多态存储多态存储是指将不同类型的数据存储在同一个字段中的技

如何在PHP中使用多态和继承来处理数据类型引言:在PHP中,多态和继承是两个重要的面向对象编程(OOP)概念。通过使用多态和继承,我们可以更加灵活地处理不同的数据类型。本文将介绍如何在PHP中使用多态和继承来处理数据类型,并通过代码示例展示它们的实际应用。一、继承的基本概念继承是面向对象编程中的一种重要概念,它允许我们创建一个类,该类可以继承父类的属性和方法

PHP是一种服务器端编程语言,自PHP5之后开始支持面向对象编程(OOP)。OOP的核心思想是将数据和行为封装在对象中,以提高程序的可维护性和可扩展性。在PHP中,面向对象编程具有三大特性:继承、多态与接口。一、继承继承是指一个类可以从另一个类中继承属性和方法。被继承的类称为父类或基类,继承的类称为子类或派生类。子类可以通过继承获得父类中的属性和方法,并且可

什么是面向对象编程?面向对象编程(OOP)是一种编程范式,它将现实世界中的实体抽象为类,并使用对象来表示这些实体。类定义了对象的属性和行为,而对象则实例化了类。OOP的主要优点在于它可以使代码更易于理解、维护和重用。OOP的基本概念OOP的主要概念包括类、对象、属性和方法。类是对象的蓝图,它定义了对象的属性和行为。对象是类的实例,它具有类的所有属性和行为。属性是对象的特征,它可以存储数据。方法是对象的函数,它可以对对象的数据进行操作。OOP的优点OOP的主要优点包括:可重用性:OOP可以使代码更

Go语言中不支持函数重载,因为它采用鸭子类型,根据实际类型确定值类型。而多态则通过接口类型和方法调用实现,不同类别的对象可以以相同方式响应。具体来说,Go语言中通过定义接口并实现这些方法,可以使不同类型的对象拥有相似行为,从而支持多态。

PHP面向对象编程中的多态与接口关系在PHP面向对象编程中,多态(Polymorphism)是一种重要的概念,它使得不同类的对象可以以一种统一的方式被使用。多态通过接口(Interface)的实现来实现,本文将通过代码示例来分析PHP面向对象编程中的多态与接口关系。在PHP中,接口是一种定义了一组方法的抽象结构,类通过实现接口来表达自己具有某些行为能力。接口

PHP中的多态与派发机制的关系在面向对象编程中,多态是一种强大的概念,它允许不同的对象对同一消息做出不同的响应。PHP作为一门强大的开发语言,也支持多态性,并且与之紧密相关的是派发机制。本文将通过代码示例来探讨PHP中的多态与派发机制的关系。首先,我们来了解一下什么是多态。多态是指对象能够根据自己的实际类型来调用相应的方法。通过使用多态,程序可以根据具体对象

在python中,继承和多态是面向对象编程(OOP)中强大的概念,它们使代码更具可扩展性、可重用性和可维护性。本文将深入探讨Python中的继承和多态,揭开它们的神秘面纱并展示它们的强大功能。继承继承允许一个类(子类)从另一个类(父类)继承属性和方法。通过继承,子类可以重用父类中已经定义的代码,从而减少重复和提高代码可维护性。语法:classSubclass(Superclass):#子类独有的属性和方法演示代码:classAnimal:def__init__(self,name):self.n


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
