Inheritance in Java

Inheritance in Java

A class that is used as a basis for inheritance, such as Employee, is a base class. When you create a class that inherits from a base class (such as EmployeeWithTerritory), it is a derived class. When considering two classes that inherit from each other, you can tell which is the base class and which is the derived class by using the two classes in a sentence with the phrase “is a(n).” A derived class always “is a” case or example of the more general base class. For example, a Tree class can be a base class to an Evergreen class. An Evergreen “is a” Tree, so Tree is the base class; however, it is not true for all Treees that “a Tree is an Evergreen.
Similarly, an EmployeeWithTerritory “is an” Employee-but not the other way around –so Employee is the base class. You can use the terms superclass and subclass as synonyms for base class and derived class, respectively. Also use the terms parent class and child class.

When a parent class contains a method that is not overridden within its child, the child can use the method name with super (because the method is a member of the superclass), with this(because the method is a member of the subclass by virtue of inheritance), or alone(again, because the method is a member of the subclass).

The three types of methods that you cannot override in a subclass are:

Static methods

Final methods

Methods within final classes

Programmers of an abstract class can include two method types:

Nonabstract methods, like those you can create in any class, are implemented in the abstract class and are simply inherited by its children.

Abstract methods have no body and must be implemented in child classes.

When a superclass is abstract, you cannot instantiate objects of the superclass; however, you can indirectly create a reference to a superclass abstract object. A reference is not an object, but it points to a memory address. When you create a reference, you do not us ethe keyword new to create a concrete object; instead, you create a variable name in which you can hold the memory address of a concrete object. So, although a reference to an abstract superclass object is not concrete, you can store a concrete subclass object reference there.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值