Java Reference
In-Depth Information
11.11
Identify the problems in the following code:
Check
Point
1 public class Circle {
2
private double radius;
3
4 public Circle( double radius) {
5 radius = radius;
6 }
7
8
public double getRadius() {
9
return radius;
10 }
11
12
public double getArea() {
13
return radius * radius * Math.PI;
14 }
15 }
16
17 class B extends Circle {
18
private double length;
19
20 B( double radius, double length) {
21 Circle(radius);
22 length = length;
23 }
24
25 @Override
26
public double getArea() {
27
return getArea() * length;
28 }
29 }
11.12 Explain the difference between method overloading and method overriding.
11.13 If a method in a subclass has the same signature as a method in its superclass with the
same return type, is the method overridden or overloaded?
11.14 If a method in a subclass has the same signature as a method in its superclass with a
different return type, will this be a problem?
11.15 If a method in a subclass has the same name as a method in its superclass with differ-
ent parameter types, is the method overridden or overloaded?
11.16 What is the benefit of using the @Override annotation?
11.6 The Object Class and Its toString() Method
Every class in Java is descended from the java.lang.Object class.
Key
Point
If no inheritance is specified when a class is defined, the superclass of the class is Object by
default. For example, the following two class definitions are the same:
public class ClassName {
...
public class ClassName extends Object {
...
Equivalent
}
}
Classes such as String , StringBuilder , Loan , and GeometricObject are implicitly
subclasses of Object (as are all the main classes you have seen in this topic so far). It is
 
 
Search WWH ::




Custom Search