Section 9.2: Interacting with the Superclass 4. Explain the difference between the this keyword and the super keyword. When should each be used? 5. For the next three problems, consider the following class: // Represents a university student. 1 2 public class Student ( private String name; 3 private int age; 4 5 6 7 8 9 10 11 12 13 public Student (String name, int age) ( this.name = name; this.age age; ) public void setAge(int age) { this.age age; Also consider the following partial implementation of a subclass of Student to represent undergraduate students at a university: public class Undergraduate Student extends Student ( private int year; } } } Can the code in the Undergraduate Student class access the name and age fields it inherits from Student? Can it call the setAge method? 6. Write a constructor for the Undergraduate Student class that accepts a name as a parameter and initializes the UnderGraduate Student's state with that name, an age value of 18, and a year value of 0. } 7. Write a version of the setAge method in the Undergraduate Student class that not only sets the age but also increments the year field's value by one. 8. Consider the following two automobile classes: public class Car ( public void m1() { } public void m2 () { 649 System.out.println("car 1"); System.out.println("car 2"); public String toString() { return "vroom";
Section 9.2: Interacting with the Superclass 4. Explain the difference between the this keyword and the super keyword. When should each be used? 5. For the next three problems, consider the following class: // Represents a university student. 1 2 public class Student ( private String name; 3 private int age; 4 5 6 7 8 9 10 11 12 13 public Student (String name, int age) ( this.name = name; this.age age; ) public void setAge(int age) { this.age age; Also consider the following partial implementation of a subclass of Student to represent undergraduate students at a university: public class Undergraduate Student extends Student ( private int year; } } } Can the code in the Undergraduate Student class access the name and age fields it inherits from Student? Can it call the setAge method? 6. Write a constructor for the Undergraduate Student class that accepts a name as a parameter and initializes the UnderGraduate Student's state with that name, an age value of 18, and a year value of 0. } 7. Write a version of the setAge method in the Undergraduate Student class that not only sets the age but also increments the year field's value by one. 8. Consider the following two automobile classes: public class Car ( public void m1() { } public void m2 () { 649 System.out.println("car 1"); System.out.println("car 2"); public String toString() { return "vroom";
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
Number 8

Transcribed Image Text:Section 9.2: Interacting with the Superclass
4. Explain the difference between the this keyword and the super keyword. When should each be used?
5. For the next three problems, consider the following class:
1 // Represents a university student.
2 public class Student (
private String name;
3
private int age;
4
5
6
7
8
9
public Student (String name, int age) (
this.name = name;
this.age age;
10
11
12
13
14 )
Also consider the following partial implementation of a subclass of Student to represent undergraduate students at
a university:
}
public class Undergraduate Student extends Student {
private int year;
public void setAge(int age) {
this.age age;
}
}
}
Can the code in the Undergraduate Student class access the name and age fields it inherits from Student? Can
it call the setAge method?
6. Write a constructor for the Undergraduate Student class that accepts a name as a parameter and initializes the
UnderGraduate Student's state with that name, an age value of 18, and a year value of 0.
}
7. Write a version of the setAge method in the Undergraduate Student class that not only sets the age but also
increments the year field's value by one.
8. Consider the following two automobile classes:
public class Car {
public void m1() {
}
public void m2 () {
649
System.out.println("car 1");
System.out.println("car 2");
public String toString() {
return "vroom";

Transcribed Image Text:}
public class Truck extends Car (
public void ml() (
}
hapter 9 Inheritance and Interfaces
System.out.println("truck 1"}}
Given the following declared variables, what is the output from the following statements?
Car mycar = new Car():
Truck mytruck = new Truck();
System.out.println (mycar);
mycar.ml();
mycar.m2 ():
System.out.println(mytruck);
mytruck.ml();
mytruck.m2();
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 5 steps

Recommended textbooks for you

Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON

Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON

Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning

Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education

Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY