当前位置:网站首页>Abstract classes and abstract methods
Abstract classes and abstract methods
2022-07-06 18:55:00 【Youth should strive】
Abstract classes and abstract methods
stay java in , Abstract classes cannot produce object instances . When defining abstractions , Need to use abstract keyword , grammar :
[ Permission modifier ] abstract class Class name {
The class body
}
Use abstract The defined classes are called abstract classes , Use abstract The defined methods are called abstract methods , grammar :
[ Permission modifier ] abstract Method return value type Method name ( parameter list );
example : Use abstract classes to simulate “ Go to the mall to buy clothes ” scene
Definition Market class
public abstract class Market {
public String name; // The name of the mall
public String goods; // Name of commodity
public abstract void shop(); // Abstract method , Used to output information
}
Definition TaobaoMarket class
public class TaobaoMarket extends Market {
public void shop() {
System.out.println(name + " online shopping " + goods);
}
}
Definition WallMarket class
public class WallMarket extends Market {
public void shop() {
System.out.println(name + " Physical store purchase " + goods);
}
}
Definition GoShopping class
public class GoShopping {
public static void main(String[] args) {
Market market = new WallMarket(); // Use derived class objects to create abstract class objects
market.name = " Wal-Mart ";
market.goods = " Seven wolves suit ";
market.shop();
market = new TaobaoMarket(); // Use derived class objects to create abstract class objects
market.name = " TaoBao ";
market.goods = " Han Du Yi she flower skirt ";
market.shop();
}
}
The operation results are as follows :
Wal Mart stores buy seven wolves suits
Taobao online shopping Han Du Yi she flower skirt
Process finished with exit code 0
Parent object new Subclass object
Animal b = new Dog();
Above , object b Call the parent class attribute when calling the attribute , Put the object b Call methods of subclasses when calling methods .
class Animal{
}
class Dog extends Animal{
}
public class Test4 {
public static void main(String[] args) {
Animal a = new Animal();
Animal b = new Dog();
System.out.println(a instanceof Animal);
System.out.println(a.getClass());
System.out.println(b.getClass());
// The method of the subclass is used first, so it calls the subclass getClass
System.out.println(a.getClass() .equals(b.getClass()));
}
}

Dog b = new Dog();
Animal b = new Dog();
The results are the same , But there is a difference , have a look Use abstract classes to simulate “ Go to the mall to buy clothes ” scene , If you will Market market = new WallMarket(); Change to WallMarket market = new WallMarket();, The compiler will report an error , remember : This parent class has two subclasses .
Another example
Define a demo1 class :
package Demo;
public class demo1 {
public static void main(String[] args) {
Teacher Li = new English();
Teacher Yang = new Math();
Li.teaching();
Yang.teaching();
}
}
Define a Teacher class
package Demo;
public abstract class Teacher{
// abstract class
abstract public void teaching(); // Abstract method
}
class Math extends Teacher{
@Override
public void teaching() {
System.out.println(" Let's talk about trigonometric functions !");
}
}
class English extends Teacher{
@Override
public void teaching() {
System.out.println("Good morning class!");
}
}
The operation results are as follows :
Good morning class!
Let's talk about trigonometric functions !
Process finished with exit code 0
Be careful : Abstract classes cannot be instantiated !
Teacher Li = new Teacher(); // The wrong way to write !

The result of error reporting is as follows
java: Demo.Teacher It is abstract. ; Cannot instantiate
What's the use of knowing abstract classes !
边栏推荐
- JDBC驱动器、C3P0、Druid和JDBCTemplate相关依赖jar包
- 徐翔妻子应莹回应“股评”:自己写的!
- 关于静态类型、动态类型、id、instancetype
- Huawei 0 foundation - image sorting
- QPushButton绑定快捷键的注意事项
- Implementation of AVL tree
- Use cpolar to build a business website (1)
- Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
- 渲大师携手向日葵,远控赋能云渲染及GPU算力服务
- 使用cpolar建立一个商业网站(1)
猜你喜欢

openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度

徐翔妻子应莹回应“股评”:自己写的!

Handwritten online chat system (principle part 1)

基于ppg和fft神经网络的光学血压估计【翻译】

爬虫玩得好,牢饭吃到饱?这3条底线千万不能碰!
![[depth first search] Ji suanke: a joke of replacement](/img/f9/10dbbc2f6fed2095d2b155ecf87b75.jpg)
[depth first search] Ji suanke: a joke of replacement

AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?

Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services

视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
![Deep circulation network long-term blood pressure prediction [translation]](/img/9c/c1ed28242a4536c1e8fde3414f82a8.png)
Deep circulation network long-term blood pressure prediction [translation]
随机推荐
Penetration test information collection - CDN bypass
R语言使用dt函数生成t分布密度函数数据、使用plot函数可视化t分布密度函数数据(t Distribution)
Cocos2d Lua smaller and smaller sample memory game
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
Online notes
AcWing 3537. Tree lookup complete binary tree
Echart simple component packaging
Markdown syntax for document editing (typera)
Collection of penetration test information -- use with nmap and other tools
MySQL查询请求的执行过程——底层原理
【论文笔记】TransUNet: Transformers Make StrongEncoders for Medical Image Segmentation
Penetration test information collection - WAF identification
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
基于ppg和fft神经网络的光学血压估计【翻译】
Understanding disentangling in β- VAE paper reading notes
Picture zoom Center
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
Jushan database was among the first batch of financial information innovation solutions!
SQL injection Foundation
二叉搜索树