Java Programming Assignments Inheritance

Before I get into the details of your next assignment, I’ll release my solution to the first assignment for you to look over and understand.

Click Here to Download Practice Assignment 1 Solution

Here’s a video with a full walk-through of my solution:

Assignment 2 – People, Employees and Organizations

Click Here to View Practice Assignment 2 on GitHub

Or you can clone this repository with this GitHub URI in your IDE:

My goal for this assignment is to get you familiar with inheritance and the importance of dealing with the methods available in the Java class. In practice assignment 2, you’ll learn how to use both an and an .

Important Notes:

  1. I’ve included two library files (JAR files) in the source code of this assignment. You still need to add them to the classpath when you extract this assignment and begin working on it. To do this, just right click on the Project and select Properties, then Java Build Path, then “Add JARs”, and navigate to the “src/lib” directory to add both JAR files.
  2. As with the first assignment there are Tests available that must pass. Currently they should all fail AND they will have compilation errors. The compilation errors are expected, as you’ll need to implement the appropriate methods from the interface and abstract classes (and then some). Once you’ve successfully coded the assignment, you won’t have any compilation errors and all the tests will pass. To run the tests, just right click on the “Tests” class name and select “Run As->JUnit test”.

Assignment Requirements

Okay so here’s the breakdown of the requirements for this assignment. You will need to develop a system that can track employee information for two Organizations (Google and Microsoft). The Employee information you must track is as follows:

  • Name
  • Sex
  • Job Title
  • Organization they work for
  • Birthday

As for the Organization that the Employee works for, you must also track this information:

  • Organization Name
  • Number of Employees

The system must be able to properly compare any two employees against each other to determine if they are the same Person. This means that if you compared two People with the same Name, Sex, Birthday and Organization, the system should think that they are equals to one another. If any of these properties are different, then the two People are not the same Person.

The same rules apply to comparing Organizations to one another. Organizations with the same Organization name are to be thought of as equal, different names means different organizations.

Composition

There are two ways to reuse existing classes, namely, composition and inheritance. With composition (aka aggregation), you define a new class, which is composed of existing classes. With inheritance, you derive a new class based on an existing class, with modifications or extensions.

We shall begin with reusing classes via composition - through examples.

Composition EG. 1: The Author and Book Classes

Let's start with the Author class

A class called is designed as shown in the class diagram. It contains:

  • Three member variables: (), (), and ( of either or - you might also use a variable called having value of or ).
  • A constructor to initialize the , and with the given values.
    (There is no default constructor, as there is no default value for , and .)
  • Public getters/setters: , , , and .
    (There are no setters for and , as these properties are not designed to be changed.)
  • A method that returns "", e.g., "".
The Author Class (Author.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 public class Author { private String name; private String email; private char gender; public Author(String name, String email, char gender) { this.name = name; this.email = email; this.gender = gender; } public String getName() { return name; } public char getGender() { return gender; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String toString() { return name + " (" + gender + ") at " + email; } }
A Test Driver for the Author Class (TestAuthor.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class TestAuthor { public static void main(String[] args) { Author ahTeck = new Author("Tan Ah Teck", "teck@nowhere.com", 'm'); System.out.println(ahTeck); ahTeck.setEmail("teck@somewhere.com"); System.out.println(ahTeck); System.out.println("name is: " + ahTeck.getName()); System.out.println("gender is: " + ahTeck.getGender()); System.out.println("email is: " + ahTeck.getEmail()); } }
A Book is written by one Author - Using an "Object" Member Variable

Let's design a class. Assume that a book is written by one (and exactly one) author. The Book class (as shown in the class diagram) contains the following members:

  • Four member variables: (), (an instance of the class we have just created, assuming that each book has exactly one author), (), and ().
  • The getters and setters: , , , , , .
  • A that returns "". You could reuse the 's method, which returns "".
The Book Class (Book.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 public class Book { private String name; private Author author; private double price; private int qty; public Book(String name, Author author, double price, int qty) { this.name = name; this.author = author; this.price = price; this.qty = qty; } public String getName() { return name; } public Author getAuthor() { return author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } public String toString() { return "'" + name + "' by " + author; } }
A Test Driver Program for the Book Class (TestBook.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class TestBook { public static void main(String[] args) { Author ahTeck = new Author("Tan Ah Teck", "ahTeck@somewhere.com", 'm'); System.out.println(ahTeck); Book dummyBook = new Book("Java for dummies", ahTeck, 9.99, 99); System.out.println(dummyBook); dummyBook.setPrice(8.88); dummyBook.setQty(88); System.out.println(dummyBook); System.out.println("name is: " + dummyBook.getName()); System.out.println("price is: " + dummyBook.getPrice()); System.out.println("qty is: " + dummyBook.getQty()); System.out.println("author is: " + dummyBook.getAuthor()); System.out.println("author's name is: " + dummyBook.getAuthor().getName()); System.out.println("author's email is: " + dummyBook.getAuthor().getEmail()); System.out.println("author's gender is: " + dummyBook.getAuthor().getGender()); Book moreDummyBook = new Book("Java for more dummies", new Author("Peter Lee", "peter@nowhere.com", 'm'), 19.99, 8); System.out.println(moreDummyBook); } }

Composition EG. 2: The Point and Line Classes

As an example of reusing a class via composition, suppose that we have an existing class called , defined as shown in the above class diagram. The source code is HERE.

Suppose that we need a new class called , we can design the class by re-using the class via composition. We say that "A line is composed of two points", or "A line has two points". Composition exhibits a "has-a" relationship.

UML Notation: In UML notations, composition is represented as a diamond-head line pointing to its constituents.

The Line Class via Composition (Line.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 public class Line { Point begin, end; public Line(int x1, int y1, int x2, int y2) { begin = new Point(x1, y1); end = new Point(x2, y2); } public Line(Point begin, Point end) { this.begin = begin; this.end = end; } public Point getBegin() { return begin; } public Point getEnd() { return end; } public void setBegin(Point begin) { this.begin = begin; } public void setEnd(Point end) { this.end = end; } public int getBeginX() { return begin.getX(); } public void setBeginX(int x) { begin.setX(x); } public int getBeginY() { return begin.getY(); } public void setBeginY(int y) { begin.setY(y); } public int[] getBeginXY() { return begin.getXY(); } public void setBeginXY(int x, int y) { begin.setXY(x, y); } public int getEndX() { return end.getX(); } public void setEndX(int x) { end.setX(x); } public int getEndY() { return end.getY(); } public void setEndY(int y) { end.setY(y); } public int[] getEndXY() { return end.getXY(); } public void setEndXY(int x, int y) { end.setXY(x, y); } public String toString() { return "Line[begin=" + begin + ",end=" + end + "]"; } public double getLength() { return begin.distance(end); } }
A Test Driver for Line Class (TestLine.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class TestLine { public static void main(String[] args) { Line l1 = new Line(1, 2, 3, 4); System.out.println(l1); Line l2 = new Line(new Point(5,6), new Point(7,8)); System.out.println(l2); l1.setBegin(new Point(11, 12)); l1.setEnd(new Point(13, 14)); System.out.println(l1); System.out.println("begin is: " + l1.getBegin()); System.out.println("end is: " + l1.getEnd()); l1.setBeginX(21); l1.setBeginY(22); l1.setEndX(23); l1.setEndY(24); System.out.println(l1); System.out.println("begin's x is: " + l1.getBeginX()); System.out.println("begin's y is: " + l1.getBeginY()); System.out.println("end's x is: " + l1.getEndX()); System.out.println("end's y is: " + l1.getEndY()); l1.setBeginXY(31, 32); l1.setEndXY(33, 34); System.out.println(l1); System.out.println("begin's x is: " + l1.getBeginXY()[0]); System.out.println("begin's y is: " + l1.getBeginXY()[1]); System.out.println("end's x is: " + l1.getEndXY()[0]); System.out.println("end's y is: " + l1.getEndXY()[1]); System.out.printf("length is: %.2f%n", l1.getLength()); } }
TRY

Try writing these more complex methods for the class:

public double getGradient(); public double distance(int x, int y); public double distance(Point p); public boolen intersects(Line another);

Composition EG. 3: The Point and Circle Classes

Suppose that we have an existing class called , defined as shown in the class diagram. The source code is HERE.

A class called is designed as shown in the class diagram.

It contains:

  • Two member variables: a (double) and a (an instance of class, which we created earlier).
  • The constructors, getters and setters.
  • Methods , , , , , , etc.
  • A method that returns a string description of instance in the format of "". You should re-use the 's to print "".
  • A method that returns the distance from the center of instance to the center of the given instance (called ).
The Circle class (Circle.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 public class Circle { private Point center; private double radius; public Circle() { this.center = new Point(); this.radius = 1.0; } public Circle(int xCenter, int yCenter, double radius) { center = new Point(xCenter, yCenter); this.radius = radius; } public Circle(Point center, double radius) { this.center = center; this.radius = radius; } public double getRadius() { return this.radius; } public void setRadius(double radius) { this.radius = radius; } public Point getCenter() { return this.center; } public void setCenter(Point center) { this.center = center; } public int getCenterX() { return center.getX(); } public void setCenterX(int x) { center.setX(x); } public int getCenterY() { return center.getY(); } public void setCenterY(int y) { center.setY(y); } public int[] getCenterXY() { return center.getXY(); } public void setCenterXY(int x, int y) { center.setXY(x, y); } public String toString() { return "Circle[center=" + center + ",radius=" + radius + "]"; } public double getArea() { return Math.PI * radius * radius; } public double getCircumference() { return 2.0 * Math.PI * radius; } public double distance(Circle another) { return center.distance(another.center); } }
A Test Driver for the Circle Class (TestCircle.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public class TestCircle { public static void main(String[] args) { Circle c1 = new Circle(); System.out.println(c1); Circle c2 = new Circle(1, 2, 3.3); System.out.println(c2); Circle c3 = new Circle(new Point(4, 5), 6.6); System.out.println(c3); c1.setCenter(new Point(11, 12)); c1.setRadius(13.3); System.out.println(c1); System.out.println("center is: " + c1.getCenter()); System.out.println("radius is: " + c1.getRadius()); c1.setCenterX(21); c1.setCenterY(22); System.out.println(c1); System.out.println("center's x is: " + c1.getCenterX()); System.out.println("center's y is: " + c1.getCenterY()); c1.setCenterXY(31, 32); System.out.println(c1); System.out.println("center's x is: " + c1.getCenterXY()[0]); System.out.println("center's y is: " + c1.getCenterXY()[1]); System.out.printf("area is: %.2f%n", c1.getArea()); System.out.printf("circumference is: %.2f%n", c1.getCircumference()); System.out.printf("distance is: %.2f%n", c1.distance(c2)); System.out.printf("distance is: %.2f%n", c2.distance(c1)); } }

Exercises

LINK TO EXERCISES

Inheritance

In OOP, we often organize classes in hierarchy to avoid duplication and reduce redundancy. The classes in the lower hierarchy inherit all the variables (static attributes) and methods (dynamic behaviors) from the higher hierarchies. A class in the lower hierarchy is called a subclass (or derived, child, extended class). A class in the upper hierarchy is called a superclass (or base, parent class). By pulling out all the common variables and methods into the superclasses, and leave the specialized variables and methods in the subclasses, redundancy can be greatly reduced or eliminated as these common variables and methods do not need to be repeated in all the subclasses. For example,

A subclass inherits all the variables and methods from its superclasses, including its immediate parent as well as all the ancestors. It is important to note that a subclass is not a "subset" of a superclass. In contrast, subclass is a "superset" of a superclass. It is because a subclass inherits all the variables and methods of the superclass; in addition, it extends the superclass by providing more variables and methods.

In Java, you define a subclass using the keyword "", e.g.,

class Goalkeeper extends SoccerPlayer {......} class MyApplet extends java.applet.Applet {.....} class Cylinder extends Circle {......}

UML Notation: The UML notation for inheritance is a solid line with a hollow arrowhead leading from the subclass to its superclass. By convention, superclass is drawn on top of its subclasses as shown.

Inheritance EG. 1: The Circle and Cylinder Classes

In this example, we derive a subclass called from the superclass , which we have created in the previous chapter. It is important to note that we reuse the class . Reusability is one of the most important properties of OOP. (Why reinvent the wheels?) The class inherits all the member variables ( and ) and methods (, , among others) from its superclass . It further defines a variable called , two public methods - and and its own constructors, as shown:

Circle.java (Re-produced)
public class Circle { private double radius; private String color; public Circle() { this.radius = 1.0; this.color = "red"; } public Circle(double radius) { this.radius = radius; this.color = "red"; } public Circle(double radius, String color) { this.radius = radius; this.color = color; } public double getRadius() { return this.radius; } public String getColor() { return this.color; } public void setRadius(double radius) { this.radius = radius; } public void setColor(String color) { this.color = color; } public String toString() { return "Circle[radius=" + radius + ",color=" + color + "]"; } public double getArea() { return radius * radius * Math.PI; } }
Cylinder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 public class Cylinder extends Circle { private double height; public Cylinder() { super(); this.height = 1.0; } public Cylinder(double height) { super(); this.height = height; } public Cylinder(double height, double radius) { super(radius); this.height = height; } public Cylinder(double height, double radius, String color) { super(radius, color); this.height = height; } public double getHeight() { return this.height; } public void setHeight(double height) { this.height = height; } public double getVolume() { return getArea()*height; } public String toString() { return "This is a Cylinder"; } }
A Test Drive for the Cylinder Class (TestCylinder.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class TestCylinder { public static void main(String[] args) { Cylinder cy1 = new Cylinder(); System.out.println("Radius is " + cy1.getRadius() + " Height is " + cy1.getHeight() + " Color is " + cy1.getColor() + " Base area is " + cy1.getArea() + " Volume is " + cy1.getVolume()); Cylinder cy2 = new Cylinder(5.0, 2.0); System.out.println("Radius is " + cy2.getRadius() + " Height is " + cy2.getHeight() + " Color is " + cy2.getColor() + " Base area is " + cy2.getArea() + " Volume is " + cy2.getVolume()); } }

Keep the "" and "" in the same directory as "" (because we are reusing the class ). Compile and run the program. The expected output is as follows:

Radius is 1.0 Height is 1.0 Color is red Base area is 3.141592653589793 Volume is 3.141592653589793 Radius is 5.0 Height is 2.0 Color is red Base area is 78.53981633974483 Volume is 157.07963267948966

Method Overriding & Variable Hiding

A subclass inherits all the member variables and methods from its superclasses (the immediate parent and all its ancestors). It can use the inherited methods and variables as they are. It may also override an inherited method by providing its own version, or hide an inherited variable by defining a variable of the same name.

For example, the inherited method in a object computes the base area of the cylinder. Suppose that we decide to override the to compute the surface area of the cylinder in the subclass . Below are the changes:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Cylinder extends Circle { ...... @Override public double getArea() { return 2*Math.PI*getRadius()*height + 2*super.getArea(); } public double getVolume() { return super.getArea()*height; } @Override public String toString() { return "Cylinder[" + super.toString() + ",height=" + height + "]"; } }

If is called from a object, it computes the area of the circle. If is called from a object, it computes the surface area of the cylinder using the overridden implementation. Note that you have to use public accessor method to retrieve the of the , because is declared and therefore not accessible to other classes, including the subclass .

But if you override the in the , the () no longer works. It is because the overridden will be used in , which does not compute the base area. You can fix this problem by using to use the superclass' version of . Note that can only be issued from the subclass definition, but no from an instance created, e.g. , as it break the information hiding and encapsulation principle.

Annotation @Override (JDK 1.5)

The "" is known as annotation (introduced in JDK 1.5), which asks compiler to check whether there is such a method in the superclass to be overridden. This helps greatly if you misspell the name of the method to be overridden. For example, suppose that you wish to override method in a subclass. If is not used and is misspelled as , it will be treated as a new method in the subclass, instead of overriding the superclass. If is used, the compiler will signal an error.

annotation is optional, but certainly nice to have.

Annotations are not programming constructs. They have no effect on the program output. It is only used by the compiler, discarded after compilation, and not used by the runtime.

Keyword "super"

Recall that inside a class definition, you can use the keyword to refer to this instance. Similarly, the keyword refers to the superclass, which could be the immediate parent or its ancestor.

The keyword allows the subclass to access superclass' methods and variables within the subclass' definition. For example, and can be used invoke the superclass’ constructor. If the subclass overrides a method inherited from its superclass, says , you can use to invoke the superclass' version within the subclass definition. Similarly, if your subclass hides one of the superclass' variable, you can use to refer to the hidden variable within the subclass definition.

More on Constructors

Recall that the subclass inherits all the variables and methods from its superclasses. Nonetheless, the subclass does not inherit the constructors of its superclasses. Each class in Java defines its own constructors.

In the body of a constructor, you can use to invoke a constructor of its immediate superclass. Note that , if it is used, must be the first statement in the subclass' constructor. If it is not used in the constructor, Java compiler automatically insert a statement to invoke the no-arg constructor of its immediate superclass. This follows the fact that the parent must be born before the child can be born. You need to properly construct the superclasses before you can construct the subclass.

Default no-arg Constructor

If no constructor is defined in a class, Java compiler automatically create a no-argument (no-arg) constructor, that simply issues a call, as follows:

public ClassName () { super(); }

Take note that:

  • The default no-arg constructor will not be automatically generated, if one (or more) constructor was defined. In other words, you need to define no-arg constructor explicitly if other constructors were defined.
  • If the immediate superclass does not have the default constructor (it defines some constructors but does not define a no-arg constructor), you will get a compilation error in doing a call. Note that Java compiler inserts a as the first statement in a constructor if there is no .

Single Inheritance

Java does not support multiple inheritance (C++ does). Multiple inheritance permits a subclass to have more than one direct superclasses. This has a serious drawback if the superclasses have conflicting implementation for the same method. In Java, each subclass can have one and only one direct superclass, i.e., single inheritance. On the other hand, a superclass can have many subclasses.

Common Root Class - java.lang.Object

Java adopts a so-called common-root approach. All Java classes are derived from a common root class called . This class defines and implements the common behaviors that are required of all the Java objects running under the JRE. These common behaviors enable the implementation of features such as multi-threading and garbage collector.

Inheritance EG. 2: The Point2D and Point3D Classes

The Superclass Point2D.java
public class Point2D { private int x, y; public Point2D() { this.x = 0; this.y = 0; } public Point2D(int x, int y) { this.x = x; this.y = y; } public int getX() { return this.x; } public void setX(int x) { this.x = x; } public int getY() { return this.y; } public void setY(int y) { this.y = y; } public String toString() { return "(" + this.x + "," + this.y + ")"; } }
The Subclass Point3D.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class Point3D extends Point2D { private int z; public Point3D() { super(); this.z = 0; } public Point3D(int x, int y, int z) { super(x, y); this.z = z; } public int getZ() { return this.z; } public void setZ(int z) { this.z = z; } @Override public String toString() { return "(" + super.getX() + "," + super.getY() + "," + this.z + ")"; } }
A Test Driver for Point2D and Point3D Classes (TestPoint2DPoint3D.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class TestPoint2DPoint3D { public static void main(String[] args) { Point2D p2a = new Point2D(1, 2); System.out.println(p2a); Point2D p2b = new Point2D(); System.out.println(p2b); p2a.setX(3); p2a.setY(4); System.out.println(p2a); System.out.println("x is: " + p2a.getX()); System.out.println("x is: " + p2a.getY()); Point3D p3a = new Point3D(11, 12, 13); System.out.println(p3a); Point2D p3b = new Point3D(); System.out.println(p3b); p3a.setX(21); p3a.setY(22); p3a.setZ(23); System.out.println(p3a); System.out.println("x is: " + p3a.getX()); System.out.println("y is: " + p3a.getY()); System.out.println("z is: " + p3a.getZ()); } }

Inheritance EG. 3: Superclass Person and its Subclasses

Suppose that we are required to model students and teachers in our application. We can define a superclass called to store common properties such as and , and subclasses and for their specific properties. For students, we need to maintain the courses taken and their respective grades; add a course with grade, print all courses taken and the average grade. Assume that a student takes no more than 30 courses for the entire program. For teachers, we need to maintain the courses taught currently, and able to add or remove a course taught. Assume that a teacher teaches not more than 5 courses concurrently.

We design the classes as follows.

The Superclass Person.java
public class Person { private String name, address; public Person(String name, String address) { this.name = name; this.address = address; } public String getName() { return name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return name + "(" + address + ")"; } }
The Subclass Student.java
public class Student extends Person { private int numCourses; private String[] courses; private int[] grades; private static final int MAX_COURSES = 30; public Student(String name, String address) { super(name, address); numCourses = 0; courses = new String[MAX_COURSES]; grades = new int[MAX_COURSES]; } @Override public String toString() { return "Student: " + super.toString(); } public void addCourseGrade(String course, int grade) { courses[numCourses] = course; grades[numCourses] = grade; ++numCourses; } public void printGrades() { System.out.print(this); for (int i = 0; i < numCourses; ++i) { System.out.print(" " + courses[i] + ":" + grades[i]); } System.out.println(); } public double getAverageGrade() { int sum = 0; for (int i = 0; i < numCourses; i++ ) { sum += grades[i]; } return (double)sum/numCourses; } }
The Subclass Teacher.java
public class Teacher extends Person { private int numCourses; private String[] courses; private static final int MAX_COURSES = 5; public Teacher(String name, String address) { super(name, address); numCourses = 0; courses = new String[MAX_COURSES]; } @Override public String toString() { return "Teacher: " + super.toString(); } public boolean addCourse(String course) { for (int i = 0; i < numCourses; i++) { if (courses[i].equals(course)) return false; } courses[numCourses] = course; numCourses++; return true; } public boolean removeCourse(String course) { boolean found = false; int courseIndex = -1; for (int i = 0; i < numCourses; i++) { if (courses[i].equals(course)) { courseIndex = i; found = true; break; } } if (found) { for (int i = courseIndex; i < numCourses-1; i++) { courses[i] = courses[i+1]; } numCourses--; return true; } else { return false; } } }
A Test Driver (TestPerson.java)
public class TestPerson { public static void main(String[] args) { Student s1 = new Student("Tan Ah Teck", "1 Happy Ave"); s1.addCourseGrade("IM101", 97); s1.addCourseGrade("IM102", 68); s1.printGrades(); System.out.println("Average is " + s1.getAverageGrade()); Teacher t1 = new Teacher("Paul Tan", "8 sunset way"); System.out.println(t1); String[] courses = {"IM101", "IM102", "IM101"}; for (String course: courses) { if (t1.addCourse(course)) { System.out.println(course + " added."); } else { System.out.println(course + " cannot be added."); } } for (String course: courses) { if (t1.removeCourse(course)) { System.out.println(course + " removed."); } else { System.out.println(course + " cannot be removed."); } } } } Tan Ah Teck(1 Happy Ave) Tan Ah Teck 8 Sunrise Place Student: Mohd Ali(8 Kg Java) Mohd Ali 9 Kg Satu Student: Mohd Ali(9 Kg Satu) IM101:97 IM102:68 Average is: 82.5 Teacher: Paul Tan(8 sunset way) IM101 added. IM102 added. IM101 cannot be added. IM101 removed. IM102 removed. IM101 cannot be removed.

Exercises

LINK TO EXERCISES

Composition vs. Inheritance

"A line is composed of 2 points" vs. "A line is a point extended by another point"

Recall that there are two ways of reusing existing classes: composition and inheritance. We have seen that a class can be implemented using composition of class - "A line is composed of two points", in the previous section.

A can also be implemented, using inheritance from the class - "A line is a point extended by another point". Let's call this subclass (to differentiate from the class using composition).

The Superclass Point.java

As above.

The Subclass LineSub.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 public class LineSub extends Point { Point end; public LineSub(int x1, int y1, int x2, int y2) { super(x1, y1); this.end = new Point(x2, y2); } public LineSub(Point begin, Point end) { super(begin.getX(), begin.getY()); this.end = end; } public Point getBegin() { return this; } public Point getEnd() { return end; } public void setBegin(Point begin) { super.setX(begin.getX()); super.setY(begin.getY()); } public void setEnd(Point end) { this.end = end; } public int getBeginX() { return super.getX(); } public void setBeginX(int x) { super.setX(x); } public int getBeginY() { return super.getY(); } public void setBeginY(int y) { super.setY(y); } public int[] getBeginXY() { return super.getXY(); } public void setBeginXY(int x, int y) { super.setXY(x, y); } public int getEndX() { return end.getX(); } public void setEndX(int x) { end.setX(x); } public int getEndY() { return end.getY(); } public void setEndY(int y) { end.setY(y); } public int[] getEndXY() { return end.getXY(); } public void setEndXY(int x, int y) { end.setXY(x, y); } public String toString() { return "LineSub[begin=" + super.toString() + ",end=" + end + "]"; } // Return the length of this Line public double getLength() { return super.distance(end); } }
A Test Driver (TestLineSub.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class TestLineSub { public static void main(String[] args) { LineSub l1 = new LineSub(1, 2, 3, 4); System.out.println(l1); LineSub l2 = new LineSub(new Point(5,6), new Point(7,8)); System.out.println(l2); l1.setBegin(new Point(11, 12)); l1.setEnd(new Point(13, 14)); System.out.println(l1); System.out.println("begin is: " + l1.getBegin()); System.out.println("end is: " + l1.getEnd()); l1.setBeginX(21); l1.setBeginY(22); l1.setEndX(23); l1.setEndY(24); System.out.println(l1); System.out.println(l1); System.out.println("begin's x is: " + l1.getBeginX()); System.out.println("begin's y is: " + l1.getBeginY()); System.out.println("end's x is: " + l1.getEndX()); System.out.println("end's y is: " + l1.getEndY()); l1.setBeginXY(31, 32); l1.setEndXY(33, 34); System.out.println(l1); System.out.println("begin's x is: " + l1.getBeginXY()[0]); System.out.println("begin's y is: " + l1.getBeginXY()[1]); System.out.println("end's x is: " + l1.getEndXY()[0]); System.out.println("end's y is: " + l1.getEndXY()[1]); System.out.printf("length is: %.2f%n", l1.getLength()); } }

Notes: This is the same test driver used in the earlier example on composition, except change in classname.

Study both versions of the Line class ( and ). I suppose that it is easier to say that "A line is composed of two points" than that "A line is a point extended by another point".

Rule of Thumb: Use composition if possible, before considering inheritance. Use inheritance only if there is a clear hierarchical relationship between classes.

Exercises

LINK TO EXERCISES ON COMPOSITION VS INHERITANCE

Polymorphism

The word "polymorphism" means "many forms". It comes from Greek word "poly" (means many) and "morphos" (means form). For examples, in chemistry, carbon exhibits polymorphism because it can be found in more than one form: graphite and diamond. But, each of the form has it own distinct properties (and price).

Substitutability

A subclass possesses all the attributes and operations of its superclass (because a subclass inherited all attributes and operations from its superclass). This means that a subclass object can do whatever its superclass can do. As a result, we can substitute a subclass instance when a superclass instance is expected, and everything shall work fine. This is called substitutability.

In our earlier example of and : is a subclass of . We can say that "is-a" (actually, it "is-more-than-a" ). Subclass-superclass exhibits a so called "is-a" relationship.

Circle.java
public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public double getRadius() { return this.radius; } public double getArea() { return radius * radius * Math.PI; } public String toString() { return "Circle[radius=" + radius + "]"; } }
Cylinder.java
public class Cylinder extends Circle { private double height; public Cylinder(double height, double radius) { super(radius); this.height = height; } public double getHeight() { return this.height; } public double getVolumne() { return super.getArea() * height; } @Override public double getArea() { return 2.0 * Math.PI * getRadius() * height; } @Override public String toString() { return "Cylinder[height=" + height + "," + super.toString() + "]"; } }

Via substitutability, we can create an instance of , and assign it to a (its superclass) reference, as follows:

Circle c1 = new Cylinder(1.1, 2.2);

You can invoke all the methods defined in the class for the reference , (which is actually holding a object), e.g.

c1.getRadius();

This is because a subclass instance possesses all the properties of its superclass.

However, you CANNOT invoke methods defined in the class for the reference , e.g.

c1.getHeight(); // compilation error c1.getVolume(); // compilation error

This is because is a reference to the class, which does not know about methods defined in the subclass .

is a reference to the class, but holds an object of its subclass . The reference , however, retains its internal identity. In our example, the subclass overrides methods and . or invokes the overridden version defined in the subclass , instead of the version defined in . This is because is in fact holding a object internally.

c1.toString(); c1.getArea();
Summary
  1. A subclass instance can be assigned (substituted) to a superclass' reference.
  2. Once substituted, we can invoke methods defined in the superclass; we cannot invoke methods defined in the subclass.
  3. However, if the subclass overrides inherited methods from the superclass, the subclass (overridden) versions will be invoked.

Polymorphism EG. 1: Shape and its Subclasses

Polymorphism is very powerful in OOP to separate the interface and implementation so as to allow the programmer to program at the interface in the design of a complex system.

Consider the following example. Suppose that our program uses many kinds of shapes, such as triangle, rectangle and so on. We should design a superclass called , which defines the public interfaces (or behaviors) of all the shapes. For example, we would like all the shapes to have a method called , which returns the area of that particular shape. The class can be written as follow.

The Superclass Shape.java
public class Shape { private String color; public Shape (String color) { this.color = color; } @Override public String toString() { return "Shape[color=" + color + "]"; } public double getArea() { System.err.println("Shape unknown! Cannot compute area!"); return 0; } }

Take note that we have a problem writing the method in the class, because the area cannot be computed unless the actual shape is known. We shall print an error message for the time being. In the later section, I shall show you how to resolve this problem.

We can then derive subclasses, such as and , from the superclass .

The Subclass Rectangle.java
public class Rectangle extends Shape { private int length; private int width; public Rectangle(String color, int length, int width) { super(color); this.length = length; this.width = width; } @Override public String toString() { return "Rectangle[length=" + length + ",width=" + width + "," + super.toString() + "]"; } @Override public double getArea() { return length*width; } }
The Subclass Triangle.java
public class Triangle extends Shape { private int base; private int height; public Triangle(String color, int base, int height) { super(color); this.base = base; this.height = height; } @Override public String toString() { return "Triangle[base=" + base + ",height=" + height + "," + super.toString() + "]"; } @Override public double getArea() { return 0.5*base*height; } }

The subclasses override the method inherited from the superclass, and provide the proper implementations for .

A Test Driver (TestShape.java)

In our application, we could create references of , and assigned them instances of subclasses, as follows:

public class TestShape { public static void main(String[] args) { Shape s1 = new Rectangle("red", 4, 5); System.out.println(s1); System.out.println("Area is " + s1.getArea()); Shape s2 = new Triangle("blue", 4, 5); System.out.println(s2); System.out.println("Area is " + s2.getArea()); } }

The expected outputs are:

Rectangle[length=4,width=5,Shape[color=red]] Area is 20.0 Triangle[base=4,height=5,Shape[color=blue]] Area is 10.0

The beauty of this code is that all the references are from the superclass (i.e., programming at the interface level). You could instantiate different subclass instance, and the code still works. You could extend your program easily by adding in more subclasses, such as , , etc, with ease.

Nonetheless, the above definition of class poses a problem, if someone instantiate a object and invoke the from the object, the program breaks.

public class TestShape { public static void main(String[] args) { Shape s3 = new Shape("green"); System.out.println(s3); System.out.println("Area is " + s3.getArea()); } }

This is because the class is meant to provide a common interface to all its subclasses, which are supposed to provide the actual implementation. We do not want anyone to instantiate a instance. This problem can be resolved by using the so-called class.

Polymorphism EG. 2: Monster and its Subclasses

Polymorphism is a powerful mechanism in OOP to separate the interface and implementation so as to allow the programmer to program at the interface in the design of a complex system. For example, in our game app, we have many types of monsters that can attack. We shall design a superclass called and define the method in the superclass. The subclasses shall then provides their actual implementation. In the main program, we declare instances of superclass, substituted with actual subclass; and invoke method defined in the superclass.

Superclass Monster.java
public class Monster { private String name; public Monster(String name) { this.name = name; } public String attack() { return "!^_&^$@+%$* I don't know how to attack!"; } }
Subclass FireMonster.java
public class FireMonster extends Monster { public FireMonster(String name) { super(name); } @Override public String attack() { return "Attack with fire!"; } }
Subclass WaterMonster.java
public class WaterMonster extends Monster { public WaterMonster(String name) { super(name); } @Override public String attack() { return "Attack with water!"; } }
Subclass StoneMonster.java
public class StoneMonster extends Monster { public StoneMonster(String name) { super(name); } @Override public String attack() { return "Attack with stones!"; } }
A Test Driver TestMonster.java
public class TestMonster { public static void main(String[] args) { Monster m1 = new FireMonster("r2u2"); Monster m2 = new WaterMonster("u2r2"); Monster m3 = new StoneMonster("r2r2"); System.out.println(m1.attack()); System.out.println(m2.attack()); System.out.println(m3.attack()); m1 = new StoneMonster("a2b2"); System.out.println(m1.attack()); Monster m4 = new Monster("u2u2"); System.out.println(m4.attack()); } }

Upcasting & Downcasting

Upcasting a Subclass Instance to a Superclass Reference

Substituting a subclass instance for its superclass is called "upcasting". This is because, in a UML class diagram, subclass is often drawn below its superclass. Upcasting is always safe because a subclass instance possesses all the properties of its superclass and can do whatever its superclass can do. The compiler checks for valid upcasting and issues error "incompatible types" otherwise. For example,

Circle c1 = new Cylinder(1.1, 2.2); Circle c2 = new String();
Downcasting a Substituted Reference to Its Original Class

You can revert a substituted instance back to a subclass reference. This is called "downcasting". For example,

Circle c1 = new Cylinder(1.1, 2.2); Cylinder cy1 = (Cylinder) c1;

Downcasting requires explicit type casting operator in the form of prefix operator . Downcasting is not always safe, and throws a runtime if the instance to be downcasted does not belong to the correct subclass. A subclass object can be substituted for its superclass, but the reverse is not true.

Another Example on Upcasting and Downcasting
public class A { public A() { System.out.println("Constructed A"); } public String toString() { return "This is A"; } } public class B extends A { public B() { super(); System.out.println("Constructed B"); } @Override public String toString() { return "This is B"; } } public class C extends B { public C() { super(); System.out.println("Constructed C"); } @Override public String toString() { return "This is C"; } }

The following program tests the upcasting an downcasting (refer to the above instance diagram):

public class TestCasting { public static void main(String[] args) { A a1 = new C(); System.out.println(a1); B b1 = (B)a1; C c1 = (C)b1; A a2 = new B(); System.out.println(a2); B b2 = (B)a2; C c2 = (C)a2; } }
Casting Operator

Compiler may not be able to detect error in explicit cast, which will be detected only at runtime. For example,

Circle c1 = new Circle(5); Point p1 = new Point(); c1 = p1; c1 = (Circle)p1;

The "instanceof" Operator

Java provides a binary operator called which returns if an object is an instance of a particular class. The syntax is as follows:

anObjectinstanceofaClass Circle c1 = new Circle(); System.out.println(c1 instanceof Circle); if (c1 instanceof Circle) { ...... }

An instance of subclass is also an instance of its superclass. For example,

Circle c1 = new Circle(1.1); Cylinder cy1 = new Cylinder(2.2, 3.3); System.out.println(c1 instanceof Circle); System.out.println(c1 instanceof Cylinder); System.out.println(cy1 instanceof Cylinder); System.out.println(cy1 instanceof Circle); Circle c2 = new Cylinder(4.4, 5.5); System.out.println(c2 instanceof Circle); System.out.println(c2 instanceof Cylinder);

Summary of Polymorphism

  1. A subclass instance processes all the attributes operations of its superclass. When a superclass instance is expected, it can be substituted by a subclass instance. In other words, a reference to a class may hold an instance of that class or an instance of one of its subclasses - it is called substitutability.
  2. If a subclass instance is assign to a superclass reference, you can invoke the methods defined in the superclass only. You cannot invoke methods defined in the subclass.
  3. However, the substituted instance retains its own identity in terms of overridden methods and hiding variables. If the subclass overrides methods in the superclass, the subclass's version will be executed, instead of the superclass's version.

Exercises

LINK TO EXERCISES

Abstract Classes & Interfaces

The abstract Method and abstract class

In the above examples of and , we encountered a problem when we create instances of and and run the or . This can be resolved via method and class.

An method is a method with only signature (i.e., the method name, the list of arguments and the return type) without implementation (i.e., the method’s body). You use the keyword to declare an method.

For example, in the class, we can declare methods , , etc, as follows:

abstract public class Shape { ...... ...... abstract public double getArea(); abstract public double getPerimeter(); abstract public void draw(); }

Implementation of these methods is NOT possible in the class, as the actual shape is not yet known. (How to compute the area if the shape is not known?) Implementation of these methods will be provided later once the actual shape is known. These methods cannot be invoked because they have no implementation.

A class containing one or more methods is called an class. An class must be declared with a class-modifier . An class CANNOT be instantiated, as its definition is not complete.

UML Notation: class and method are shown in italic.

Abstract Class EG. 1: Shape and its Subclasses

Let us rewrite our class as an class, containing an method as follows:

The abstract Superclass Shape.java
abstract public class Shape { private String color; public Shape (String color) { this.color = color; } @Override public String toString() { return "Shape of color=\"" + color + "\""; } abstract public double getArea(); }

An class is incomplete in its definition, since the implementation of its methods is missing. Therefore, an class cannot be instantiated. In other words, you cannot create instances from an class (otherwise, you will have an incomplete instance with missing method's body).

To use an class, you have to derive a subclass from the class. In the derived subclass, you have to override the methods and provide implementation to all the methods. The subclass derived is now complete, and can be instantiated. (If a subclass does not provide implementation to all the methods of the superclass, the subclass remains .)

This property of the class solves our earlier problem. In other words, you can create instances of the subclasses such as and , and upcast them to (so as to program and operate at the interface level), but you cannot create instance of , which avoid the pitfall that we have faced. For example,

public class TestShape { public static void main(String[] args) { Shape s1 = new Rectangle("red", 4, 5); System.out.println(s1); System.out.println("Area is " + s1.getArea()); Shape s2 = new Triangle("blue", 4, 5); System.out.println(s2); System.out.println("Area is " + s2.getArea()); Shape s3 = new Shape("green"); } }

In summary, an class provides a template for further development. The purpose of an abstract class is to provide a common interface (or protocol, or contract, or understanding, or naming convention) to all its subclasses. For example, in the class , you can define abstract methods such as and . No implementation is possible because the actual shape is not known. However, by specifying the signature of the methods, all the subclasses are forced to use these methods' signature. The subclasses could provide the proper implementations.

Coupled with polymorphism, you can upcast subclass instances to , and program at the level, i,e., program at the interface. The separation of interface and implementation enables better software design, and ease in expansion. For example, defines a method called , which all the subclasses must provide the correct implementation. You can ask for a from any subclasses of Shape, the correct area will be computed. Furthermore, you application can be extended easily to accommodate new shapes (such as or ) by deriving more subclasses.

Rule of Thumb: Program at the interface, not at the implementation. (That is, make references at the superclass; substitute with subclass instances; and invoke methods defined in the superclass only.)

Notes:

  • An abstract method cannot be declared , as method cannot be overridden. An method, on the other hand, must be overridden in a descendant before it can be used.
  • An method cannot be (which generates a compilation error). This is because method are not visible to the subclass and thus cannot be overridden.

Abstract Class EG. 2: Monster

We shall define the superclass as an class, containing an method . The class cannot be instantiated (i.e., creating instances).

abstract public class Monster { private String name; public Monster(String name) { this.name = name; } abstract public String attack(); }

The Java's interface

A Java is a 100% abstract superclass which define a set of methods its subclasses must support. An contains only abstract methods (methods with signature and no implementation) and possibly constants ( variables). You have to use the keyword "" to define an (instead of keyword "" for normal classes). The keyword and are not needed for its abstract methods as they are mandatory.

(JDK 8 introduces default and static methods in the interface. JDK 9 introduces private methods in the interface. These will not be covered in this article.)

Similar to an superclass, an cannot be instantiated. You have to create a "subclass" that implements an interface, and provide the actual implementation of all the methods.

Unlike a normal class, where you use the keyword "" to derive a subclass. For interface, we use the keyword "" to derive a subclass.

An interface is a contract for what the classes can do. It, however, does not specify how the classes should do it.

An interface provides a form, a protocol, a standard, a contract, a specification, a set of rules, an interface, for all objects that implement it. It is a specification and rules that any object implementing it agrees to follow.

In Java, class and are used to separate the public interface of a class from its implementation so as to allow the programmer to program at the interface instead of the various implementation.

Interface Naming Convention: Use an adjective (typically ends with "") consisting of one or more words. Each word shall be initial capitalized (camel-case). For example, , , , , , etc.

Interface EG. 1: Shape Interface and its Implementations

We can re-write the superclass into an , containing only methods, as follows:

UML Notations: Abstract classes, Interfaces and abstract methods are shown in italics. Implementation of interface is marked by a dash-arrow leading from the subclasses to the interface.

public interface Shape { double getArea(); } public class Rectangle implements Shape { private int length; private int width; public Rectangle(int length, int width) { this.length = length; this.width = width; } @Override public String toString() { return "Rectangle[length=" + length + ",width=" + width + "]"; } @Override public double getArea() { return length * width; } } public class Triangle implements Shape { private int base; private int height; public Triangle(int base, int height) { this.base = base; this.height = height; } @Override public String toString() { return "Triangle[base=" + base + ",height=" + height + "]"; } @Override public double getArea() { return 0.5 * base * height; } }

A test driver is as follows:

public class TestShape { public static void main(String[] args) { Shape s1 = new Rectangle(1, 2); System.out.println(s1); System.out.println("Area is " + s1.getArea()); Shape s2 = new Triangle(3, 4); System.out.println(s2); System.out.println("Area is " + s2.getArea()); //Shape s3 = new Shape("green"); // Compilation Error!! } }

Interface EG. 2: Movable Interface and its Implementations

Suppose that our application involves many objects that can move. We could define an interface called , containing the signatures of the various movement methods.

Interface Moveable.java
public interface Movable { public void moveUp(); public void moveDown(); public void moveLeft(); public void moveRight(); }

Similar to an class, an cannot be instantiated; because it is incomplete (the abstract methods' body is missing). To use an interface, again, you must derive subclasses and provide implementation to all the abstract methods declared in the interface. The subclasses are now complete and can be instantiated.

MovablePoint.java

To derive subclasses from an , a new keyboard "" is to be used instead of "

One thought on “Java Programming Assignments Inheritance

Leave a Reply

Your email address will not be published. Required fields are marked *