1. Here is the first line of a class declaration. a. What is the name of the sup
ID: 3568898 • Letter: 1
Question
1. Here is the first line of a class declaration.
a. What is the name of the superclass?
b. What is the name of the subclass?
public class Truck extends Vehicle
2. Look at the following class declarations and answer the questions that follow.
public class Shape {
private double area;
public void setArea(double a) {
area = a;
}
public double getArea() {
return area;
}
public class Circle extends Shape {
private double radius;
public void setRadius(double r) {
radius = r;
setArea(Math.PI * r * r);
}
public double getRadius() {
return radius;
}
}
a) Which class is the superclass, and which class is the subclass?
b) When a Circle object is created, what are its public members?
c) What members of the Shape class are not accessible to the Circle class?
d) Given these definitions:
Shape s = new Shape();
Circle c = new Circle();
Indicate whether the following statements are legal or illegal:
c.setRadius(10.0);
s.setRadius(10.0);
System.out.println(c.getArea());
System.out.println(s.getArea());
3. When you create a class, it automatically has a toString method and an equals method. Why?
Explanation / Answer
=====================================================================================
1. Here is the first line of a class declaration.
a. What is the name of the superclass?
b. What is the name of the subclass?
public class Truck extends Vehicle
Ans:
a. Vehicle is Super Class
b. Truck is Sub Class
=====================================================================================
2. Look at the following class declarations and answer the questions that follow.
public class Shape {
private double area;
public void setArea(double a) {
area = a;
}
public double getArea() {
return area;
}
public class Circle extends Shape {
private double radius;
public void setRadius(double r) {
radius = r;
setArea(Math.PI * r * r);
}
public double getRadius() {
return radius;
}
}
a) Which class is the superclass, and which class is the subclass?
b) When a Circle object is created, what are its public members?
c) What members of the Shape class are not accessible to the Circle class?
d) Given these definitions:
Shape s = new Shape();
Circle c = new Circle();
Indicate whether the following statements are legal or illegal:
c.setRadius(10.0);
s.setRadius(10.0);
System.out.println(c.getArea());
System.out.println(s.getArea());
Answer:
a) Shape is Super class
Circle is Sub class
b) setArea(double a);
getArea();
setRadius(double r);
getRadius(); are the public members of the Cricle class
c) Private member area is not accessible via Circle class
d) c.setRadius(10.0); is legal
s.setRadius(10.0); is illegal
System.out.println(c.getArea()); is legal
System.out.println(s.getArea()); is legal
=====================================================================================
3. When you create a class, it automatically has a toString method and an equals method. Why?
Answer:
All the objects in java extends by default Object class
hence the definations for toString() and equals() already defined in Object class
======================================================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.