What Is Inheritance?
Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle
now becomes the superclass of MountainBike
, RoadBike
, and TandemBike
. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:
A hierarchy of bicycle classes.
The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends
keyword, followed by the name of the class to inherit from:
class MountainBike extends Bicycle {
// new fields and methods defining a mountain bike would go here
}
This gives MountainBike
all the same fields and methods as Bicycle
, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. However, you must take care to properly document the state and behavior that each superclass defines, since that code will not appear in the source file of each subclass.
分享到:
相关推荐
Packt.Publishing.Learning.jQuery.1.3 英文原版电子书
My first encounter with the Java programming language was during a one-week Java training session in 1997. I did not get a chance to use Java in a project until 1999. I read two Java books and took a ...
Machine Learning in Java 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Discover the concepts of deep learning used for natural language processing (NLP), with full-fledged examples of neural network models such as recurrent neural networks, long short-term memory ...
This course aims to create a smooth learning path that will teach you how to effectively use deep learning with Java with other de facto components to get the most out of it. Through this ...
jQuery [jQuery基础教程.(英文原版)].Packt.Publishing.Learning.jQuery.1.3_Source.Code.rar
My first encounter with the Java programming language was during a one-week Java training session in 1997. I did not get a chance to use Java in a project until 1999. I read two Java books and took a ...
Deep Learning in Natural Language Processing 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 ...
Python is the go-to language for applied machine learning and deep learning, both in terms of demand from employers and employees. This is not least because it could be a renaissance for machine ...
The Psychological Course of Java Language Learning.zip
( Deep Learning for Natural Language Processing(CS224d).ra nlp入门课程
Applying deep learning approaches to various NLP tasks can ... Deep Learning for Natural Language Processing starts by highlighting the basic building blocks of the natural language processing domain.
So, you should first obtain a solid grasp of the Java language and its foundation APIs to improve the chances of succeeding as an Android app developer. This book will show you how to get your ...
Learning Modular Java Programming
easier pathway for learning the language and libraries. Given the size of the language and the scope of the new features introduced in Java SE 8, there’s plenty of material to cover, but it’s ...
In particular, the striking success of deep learning in a wide variety of natural language processing (NLP) applications has served as a benchmark for the advances in one of the most important tasks ...
After finishing this book, you can take your Java knowledge to the next level by learning the Java APIs and modules, which are covered in the final book in the series, Java APIs, Extensions and ...
Machine Learning in Java.PDF文档+代码 If you want to learn how to use Java's machine learning libraries to gain insight from your data, this book is for you. You should be familiar with Java ...