inheritance中文Java语言,Java语言程序设计基础篇课后题答案-Chapter 9 Inheritance and Polymorphism...

这篇博客探讨了Java中继承和多态的概念。首先,解释了当子类调用默认构造器时,如何引发父类构造器的调用。接着,指出所有关于继承的四种陈述都是错误的,包括子类扩展超类、显式调用构造器、重写方法的条件等。然后,展示了错误的代码示例,说明了构造器、成员变量初始化和方法重写时的常见问题。最后,讨论了覆盖`toString()`和`equals()`方法的重要性,它们在子类中通常被重写以提供特定对象的详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chapter 9 Inheritance and Polymorphism

1. (a) The printout is

A’s no-arg constructor is invoked

(b) The default constructor of B attempts to invoke the default of constructor of

A, but class A's default constructor is not defined.

2. All false.

(1) A subclass is an extension of a superclass and normally contains more details

information than its superclass.

(2) If a subclass’s constructor explicitly invoke a superclass’s constructor, the

superclass’s no-arg constructor is not invoked.

(3) You can only override accessible instance methods.

(4) You can only override accessible instance methods.

3. The following lines are erroneous:

{

radius = radius; // Must use this.radius = radius

}

class B extends Circle (missing extends)

{

Circle(radius); // Must use super(radius)

length = length; // Must use this.length = length

}

public double getArea()

{

return getArea()*length; // super.getArea()

}

4. Method overloading defines methods of the same name in a class. Method

overriding modifies the methods that are defined in the superclasses.

5. Yes, because these two methods are defined in the Object class; therefore, they

are available to all Java classes. The subclasses usually override these methods to

provide specific information for these methods.

The toString() method returns a string representation of the object; the equals()

method compares the contents of two objects to determine whether they are the

same.

6. B’s constructor is invoked

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值