0% found this document useful (0 votes)
272 views5 pages

Department of Computer Science & Engineering: Jubail University College

This document describes an exercise on inheritance in Java. It defines a Student class and subclasses Undergrad and Graduate. It also defines classes PurchaseItem, WeighedItem, and CountedItem to model purchased items. The exercises ask to modify the code to answer questions about method overriding and hiding between instance and static methods with different access modifiers.

Uploaded by

S alghamdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
272 views5 pages

Department of Computer Science & Engineering: Jubail University College

This document describes an exercise on inheritance in Java. It defines a Student class and subclasses Undergrad and Graduate. It also defines classes PurchaseItem, WeighedItem, and CountedItem to model purchased items. The exercises ask to modify the code to answer questions about method overriding and hiding between instance and static methods with different access modifiers.

Uploaded by

S alghamdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Jubail University College

Department of Computer Science & Engineering


CS 231: Programming 2
Lab1 supplement exercise

Objectives: In this lab, the following topics will be covered

1. Inheritance (in Java),


2. Exercises for practice
3. Shadowing, Overriding, and Hiding

1. Inheritance (in Java):


Inheritance is an important object-oriented concept that allows classes to be reused in
order to define similar, but distinct, classes. In this lab we walk through the development
of a class hierarchy and a program that makes use of several classes in the hierarchy. We
begin by looking at an example of inheritance hierarchy
The class Student is the parent class. Note that all the variables are private and hence the
child classes can only use them through accessor and mutator methods. Also note the use
of overloaded constructors.

public class Student{


private String name;
private int id;
private double gpa;

public Student(int id, String name, double gpa) {


this.id = id;
this.name = name;
this.gpa = gpa;
}

public Student(int id, double gpa){


this(id, "", gpa);
}
public String getName(){
return name;
}
public int getId() {
MyLine.java
return id;
}
public double getGPA(){
return gpa;
}

public void setName(String newName){


this.name = newName;
}
public String toString(){
return "Student:\nID: "+id+"\nName: "+name+"\nGPA: "+gpa;
}
}

The class Undergrad extends the Student class. Note the overridden toString() method

public class Undergrad extends Student


{
private String year;
public Undergrad(int id, String name, double gpa, String year)
{
super(id, name, gpa);
this.year = year;
}

public String getYear() {


return year;
}

public void setYear(String newYear) {


this.year = newYear;
}

public String toString() {


Graduate.java: return "Undergraduate "+super.toString()+"\nYear: "+year;
The class}Graduate extends the Student class too. Note the overridden toString() method
}
public class Graduate extends Student
{
private String thesisTitle;
public Graduate(int id, String name, double gpa, String thesisTitle)
{
super(id, name, gpa);
this.thesisTitle = thesisTitle;
}
public String getthesisTitle() {
return thesisTitle;
}

public void setThesisTitle(String newthesisTitle) {


this.thesisTitle = newthesisTitle;
}

public String toString() {


return "Graduate " +super.toString()+"\nThesis: "+thesisTitle;
}
}

TestStudents is a driver class to test the above classes


public class TestStudents
{
public static void main(String[] args)
{
Student s1 = new Student(97000, "Sameer", 3.51);
Student s2 = new Student(98000, 3.22);
Undergrad u1 = new Undergrad(99000, "Shahid", 2.91, "Junior");
Graduate g1 = new Graduate(200000, "Mubin", 3.57,
"Algorithms and Complexity");

System.out.println(s1);
System.out.println(s2);
System.out.println(u1);
System.out.println(g1);
}
}

Student:
ID: 97000
Name: Sameer
GPA: 3.51
Student:
ID: 98000
Name:
GPA: 3.22
Undergraduate Student:
ID: 99000
Output Name: Shahid
GPA: 2.91
Year: Junior
Graduate Student:
ID: 200000
Name: Mubin
GPA: 3.57
Thesis: Algorithms and Complexity
Press any key to continue...
Exercise1:
Consider a superclass PurchaseItem which models customer’s purchases. This class has:
- two private instance variables name (String) and unitPrice (double).
- One constructor to initialize the instance variables.
- A default constructor to initialize name to “no item”, and unitPrice to 0. use this()
- A method getPrice that returns the unitPrice.
- Accessor and mutator methods.
- A toString method to return the name of the item followed by @ symbol, then the
unitPrice.
Consider two subclasses WeighedItem and CountedItem. WeighedItem has an additional
instance variable weight (double) in Kg while CountedItem has an additional variable
quantity (int) both private.
- Write an appropriate constructor for each of the classes making use of the constructor of
the superclass in defining those of the subclasses.
- Override getPrice method that returns the price of the purchasedItem based on its unit
price and weight (WeighedItem), or quantity (CountedItem). Make use of getPrice of the
superclass
- Override also toString method for each class making use of the toString method of the
superclass in defining those of the subclasses.
toString should return something that can be printed on the receipt.
For example
Banana @ 3.00 1.37Kg 4.11 SR (in case of WeighedItem class)
Pens @ 4.5 10 units 45 SR (in case of CountedItem class)
Write an application class where you construct objects from the two subclasses and print
them on the screen.

Exercise 2:
Use the sample Java programs provided above and modify them to answer the following
questions.
a- Can an instance method override a static method?
b- Can a static method override (hide) an instance method?
c- Can you override a final instance method?
d- Can you override an instance method and make it final?
e- Can you override an instance method and change its return type?
f- Can you hide a final static method ?
g- Can an instance field hide a static field?
h- Can a static field hide an instance field?
i- Can an instance method with public visibility override an instance method with
default visibility?
j- Can an instance method with default visibility override an instance method with
public visibility?
k- Can an instance method with protected visibility override an instance method with
default visibility?
l- Can an instance method with default visibility override an instance method with
protected visibility?
m- Based on the last four question, order the access visibility from the widest to the
narrowest (weakest) and state the rule for overriding (instance methods) or hiding
(static methods) ?

You might also like