Draw a Circle From a Subclass in Java
Java Programming Tutorial
OOP - Limerick, Inheritance & Polymorphism
At that place are 2 ways to reuse existing classes, namely, composition and inheritance. With composition (aka aggregation), you lot define a new class, which is equanimous of existing classes. With inheritance, yous derive a new class based on an existing class, with modifications or extensions.
Composition
We shall begin with reusing classes via composition - through examples.
Limerick EG. one: The Author and Book Classes
Let'southward showtime with the Author class
A class called Writer is designed as shown in the course diagram. Information technology contains:
- 3
privatemember variables:name(String),email(String), andgender(charof either'm'or'f'- you might also utilize abooleanvariable calledisMalehaving value oftrueorsimulated). - A constructor to initialize the
proper noun,electronic mailandgenderwith the given values.
(There is no default constructor, as there is no default value forname,emailandgender.) - Public getters/setters:
getName(),getEmail(),setEmail(), andgetGender().
(At that place are no setters fornameandgender, as these properties are non designed to be inverse.) - A
toString()method that returns "proper name (gender) at email", e.k., "Tan Ah Teck (thousand) at ahTeck@somewhere.com".
The Author Class (Author.java)
1 2 iii four v 6 seven viii ix 10 11 12 13 xiv fifteen xvi 17 eighteen 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | public class Author { individual Cord name; individual String email; private char gender; public Author(String name, String e-mail, char gender) { this.name = proper name; this.electronic mail = email; this.gender = gender; } public String getName() { return name; } public char getGender() { return gender; } public String getEmail() { return email; } public void setEmail(String e-mail) { this.email = email; } */ public Cord toString() { return name + " (" + gender + ") at " + electronic mail; } } |
A Test Driver for the Author Class (TestAuthor.coffee)
public grade TestAuthor { public static void main(String[] args) { Writer ahTeck = new Writer("Tan Ah Teck", "teck@nowhere.com", '1000'); 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("electronic mail is: " + ahTeck.getEmail()); } } A Book is written by one Author - Using an "Object" Member Variable
Allow's blueprint a Volume class. Assume that a book is written by one (and exactly one) writer. The Book course (equally shown in the class diagram) contains the following members:
- 4
individualmember variables:proper noun(String),author(an instance of theAuthorclass nosotros accept just created, assuming that each book has exactly one author),price(double), andqty(int). - The
publicgetters and setters:getName(),getAuthor(),getPrice(),setPrice(),getQty(),setQty(). - A
toString()that returns "'book-name' by writer-name (gender) at email". Y'all could reuse theAuthor'stoString()method, which returns "author-name (gender) at electronic mail".
The Book Class (Book.java)
1 ii 3 4 5 6 vii 8 9 10 11 12 13 14 15 16 17 18 19 twenty 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 | public class Book { private Cord proper noun; private Writer author; private double toll; private int qty; public Book(String proper name, Author writer, double price, int qty) { this.proper name = name; this.author = writer; this.price = cost; this.qty = qty; } public String getName() { render proper name; } public Author getAuthor() { return author; } public double getPrice() { return price; } public void setPrice(double toll) { this.price = cost; } public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } public Cord toString() { return "'" + proper name + "' by " + author; } } |
A Test Driver Programme for the Volume Class (TestBook.java)
public grade TestBook { public static void main(String[] args) { Writer ahTeck = new Writer("Tan Ah Teck", "ahTeck@somewhere.com", 'm'); System.out.println(ahTeck); Book dummyBook = new Book("Java for dummies", ahTeck, ix.99, 99); Arrangement.out.println(dummyBook); dummyBook.setPrice(8.88); dummyBook.setQty(88); System.out.println("name is: " + dummyBook.getName()); Organization.out.println("price is: " + dummyBook.getPrice()); System.out.println("qty is: " + dummyBook.getQty()); System.out.println("writer is: " + dummyBook.getAuthor()); System.out.println("author's proper 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()); Volume moreDummyBook = new Book("Coffee for more dummies", new Author("Peter Lee", "peter@nowhere.com", 'm'), 19.99, eight); System.out.println(moreDummyBook); } } Notes: In this example, I used "proper noun" for Book class instead of "title" to illustrate that yous can have a variable proper name in both the Author and Book classes, but they are distinct.
Limerick EG. 2: The Point and Line Classes
As an example of reusing a class via composition, suppose that we have an existing class called Point, defined as shown in the to a higher place course diagram. The source code is HERE.
Suppose that we demand a new form chosen Line, we can design the Line class past re-using the Betoken class via composition. We say that "A line is composed of ii 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)
i 2 three 4 5 half dozen 7 viii 9 10 11 12 13 14 fifteen 16 17 eighteen 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 xl 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 seventy 71 72 73 74 75 76 77 78 79 eighty 81 | public class Line { Point begin, end; public Line(int x1, int y1, int x2, int y2) { begin = new Betoken(x1, y1); end = new Signal(x2, y2); } public Line(Point begin, Point end) { this.brainstorm = begin; this.stop = cease; } public Point getBegin() { return brainstorm; } public Betoken getEnd() { return end; } public void setBegin(Point begin) { this.begin = begin; } public void setEnd(Point terminate) { this.end = end; } public int getBeginX() { render begin.getX(); } public void setBeginX(int 10) { begin.setX(10); } 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) { brainstorm.setXY(10, y); } public int getEndX() { return end.getX(); } public void setEndX(int x) { cease.setX(10); } 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) { finish.setXY(x, y); } public String toString() { render "Line[brainstorm=" + begin + ",end=" + end + "]"; } public double getLength() { return brainstorm.distance(end); } } |
A Exam Driver for Line Form (TestLine.coffee)
import java.util.Arrays; public grade TestLine { public static void main(String[] args) { Line l1 = new Line(ane, 2, 3, 4); System.out.println(l1); Line l2 = new Line(new Point(five,6), new Point(7,8)); Organization.out.println(l2); l1.setBegin(new Point(xi, 12)); l1.setEnd(new Point(thirteen, 14)); System.out.println(l1); System.out.println("brainstorm is: " + l1.getBegin()); Organisation.out.println("cease is: " + l1.getEnd()); l1.setBeginX(21); l1.setBeginY(22); l1.setEndX(23); l1.setEndY(24); System.out.println(l1); System.out.println("begin's ten is: " + l1.getBeginX()); System.out.println("begin'due south y is: " + l1.getBeginY()); System.out.println("end'southward x is: " + l1.getEndX()); Organization.out.println("terminate'south y is: " + l1.getEndY()); l1.setBeginXY(31, 32); l1.setEndXY(33, 34); System.out.println(l1); Arrangement.out.println("begin's x and y are: " + Arrays.toString(l1.getBeginXY())); System.out.println("finish'due south x and y are: " + Arrays.toString(l1.getEndXY())); Organization.out.printf("length is: %.2f%n", l1.getLength()); } } Practise: Endeavor writing these more than complex methods for the Line class:
public double getGradient() public double distance(int x, int y) public double distance(Bespeak p) public boolen intersects(Line another)
Composition EG. 3: The Betoken and Circle Classes
Suppose that we have an existing class called Point, defined as shown in the form diagram. The source code is HERE.
A course chosen Circumvolve is designed every bit shown in the course diagram.
Information technology contains:
- Two
individualmember variables: aradius(double) and acenter(an instance ofBespeakform, which we created earlier). - The constructors,
publicgetters and setters. - Methods
getCenterX(),setCenterX(),getCenterY(),setCenterY(),getCenterXY(),setCenterXY(), etc. - A
toString()method that returns a cord description ofthisinstance in the format of "Circumvolve[middle=(x,y),radius=r]". You lot should re-use thePoint'due southtoString()to impress "(ten,y)". - A
distance(Circle another)method that returns the distance from the center ofthisinstance to the centre of the givenCircleinstance (chosenanother).
The Circle class (Circle.java)
1 2 3 four 5 6 vii 8 9 10 11 12 xiii 14 15 16 17 18 19 twenty 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 lxx 71 72 73 74 75 76 77 78 79 lxxx | public class Circle { individual Point center; private double radius; public Circle() { this.centre = new Bespeak(); this.radius = i.0; } public Circle(int xCenter, int yCenter, double radius) { center = new Point(xCenter, yCenter); this.radius = radius; } public Circle(Indicate center, double radius) { this.center = heart; this.radius = radius; } public double getRadius() { render this.radius; } public void setRadius(double radius) { this.radius = radius; } public Point getCenter() { return this.middle; } public void setCenter(Bespeak eye) { this.eye = center; } public int getCenterX() { return center.getX(); } public void setCenterX(int 10) { heart.setX(ten); } public int getCenterY() { return center.getY(); } public void setCenterY(int y) { centre.setY(y); } public int[] getCenterXY() { return middle.getXY(); } public void setCenterXY(int x, int y) { center.setXY(x, y); } public String toString() { return "Circle[center=" + center + ",radius=" + radius + "]"; } public double getArea() { render Math.PI * radius * radius; } public double getCircumference() { return 2.0 * Math.PI * radius; } public double altitude(Circumvolve another) { render center.distance(another.middle); } } |
A Test Driver for the Circle Course (TestCircle.coffee)
1 ii 3 iv v 6 seven 8 9 10 11 12 xiii 14 15 16 17 xviii xix 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public form TestCircle { public static void main(Cord[] args) { Circle c1 = new Circle(); Organisation.out.println(c1); Circle c2 = new Circumvolve(1, two, 3.iii); Arrangement.out.println(c2); Circle c3 = new Circle(new Signal(4, five), 6.half dozen); Organization.out.println(c3); c1.setCenter(new Point(11, 12)); c1.setRadius(xiii.iii); System.out.println(c1); System.out.println("eye is: " + c1.getCenter()); Organization.out.println("radius is: " + c1.getRadius()); c1.setCenterX(21); c1.setCenterY(22); System.out.println(c1); System.out.println("center's ten is: " + c1.getCenterX()); Arrangement.out.println("center's y is: " + c1.getCenterY()); c1.setCenterXY(31, 32); System.out.println(c1); System.out.println("eye's 10 is: " + c1.getCenterXY()[0]); Organization.out.println("center'southward y is: " + c1.getCenterXY()[1]); Organization.out.printf("surface area is: %.2f%northward", c1.getArea()); System.out.printf("circumference is: %.2f%northward", c1.getCircumference()); System.out.printf("altitude 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 bureaucracy to avoid duplication and reduce redundancy. The classes in the lower bureaucracy inherit all the variables (static attributes) and methods (dynamic behaviors) from the higher hierarchies. A course in the lower hierarchy is called a subclass (or derived, child, extended form). A class in the upper hierarchy is called a superclass (or base, parent course). By pulling out all the common variables and methods into the superclasses, and exit the specialized variables and methods in the subclasses, redundancy can exist greatly reduced or eliminated every bit these common variables and methods do non demand to be repeated in all the subclasses. For case,
A bracket inherits all the variables and methods from its superclasses, including its immediate parent as well every bit all the ancestors. It is important to notation 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 Coffee, you define a subclass using the keyword "extends", e.one thousand.,
class Goalkeeper extends SoccerPlayer {......} class MyApplet extends java.applet.Applet {.....} class Cylinder extends Circumvolve {......}
UML Note: 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 chosen Cylinder from the superclass Circle, which we have created in the previous affiliate. It is important to notation that we reuse the class Circle. Reusability is one of the most important properties of OOP. (Why reinvent the wheels?) The grade Cylinder inherits all the fellow member variables (radius and color) and methods (getRadius(), getArea(), among others) from its superclass Circumvolve. It further defines a variable called acme, two public methods - getHeight() and getVolume() and its ain constructors, as shown:
Circle.java (Re-produced)
public class Circumvolve { private double radius; individual String color; public Circle() { this.radius = 1.0; this.color = "reddish"; System.out.println("Construced a Circle with Circle()"); } public Circumvolve(double radius) { this.radius = radius; this.color = "red"; System.out.println("Construced a Circumvolve with Circle(radius)"); } public Circle(double radius, String color) { this.radius = radius; this.colour = color; Organisation.out.println("Construced a Circle with Circle(radius, 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 = colour; } public String toString() { return "Circumvolve[radius=" + radius + ",color=" + color + "]"; } public double getArea() { render radius * radius * Math.PI; } } Cylinder.java
public course Cylinder extends Circle { private double superlative; public Cylinder() { super(); this.superlative = 1.0; Arrangement.out.println("Constructed a Cylinder with Cylinder()"); } public Cylinder(double tiptop) { super(); this.height = height; System.out.println("Constructed a Cylinder with Cylinder(height)"); } public Cylinder(double peak, double radius) { super(radius); this.height = height; System.out.println("Constructed a Cylinder with Cylinder(meridian, radius)"); } public Cylinder(double meridian, double radius, Cord color) { super(radius, color); this.peak = acme; System.out.println("Constructed a Cylinder with Cylinder(height, radius, colour)"); } public double getHeight() { return this.height; } public void setHeight(double height) { this.height = superlative; } public String toString() { return "This is a Cylinder"; } } A Exam Drive for the Cylinder Form (TestCylinder.java)
public class TestCylinder { public static void chief(String[] args) { Cylinder cy1 = new Cylinder(); System.out.println("Radius is " + cy1.getRadius() + ", Height is " + cy1.getHeight() + ", Colour is " + cy1.getColor() + ", Base of operations surface area is " + cy1.getArea() + ", Volume is " + cy1.getVolume()); Cylinder cy2 = new Cylinder(5.0, ii.0); System.out.println("Radius is " + cy2.getRadius() + ", Tiptop is " + cy2.getHeight() + ", Colour is " + cy2.getColor() + ", Base of operations area is " + cy2.getArea() + ", Volume is " + cy2.getVolume()); } } Continue the "Cylinder.java" and "TestCylinder.java" in the same directory as "Circle.class" (because we are reusing the class Circle). Compile and run the program.
Method Overriding & Variable Hiding
A subclass inherits all the member variables and methods from its superclasses (the immediate parent and all its ancestors). Information technology tin utilise the inherited methods and variables as they are. It may likewise override an inherited method past providing its own version, or hide an inherited variable past defining a variable of the aforementioned name.
For example, the inherited method getArea() in a Cylinder object computes the base area of the cylinder. Suppose that we decide to override the getArea() to compute the surface surface area of the cylinder in the subclass Cylinder. Below are the changes:
ane ii three iv 5 half-dozen 7 8 nine 10 11 12 xiii 14 15 xvi 17 | public class Cylinder extends Circle { ...... @Override public double getArea() { return 2*Math.PI*getRadius()*elevation + 2*super.getArea(); } public double getVolume() { return super.getArea()*height; } @Override public Cord toString() { render "Cylinder[" + super.toString() + ",height=" + height + "]"; } } |
If getArea() is called from a Circle object, it computes the area of the circle. If getArea() is called from a Cylinder object, it computes the surface surface area of the cylinder using the overridden implementation. Note that you have to utilize public accessor method getRadius() to retrieve the radius of the Circumvolve, because radius is declared private and therefore not accessible to other classes, including the subclass Cylinder.
But if y'all override the getArea() in the Cylinder, the getVolume() (=getArea()*height) no longer works. It is because the overridden getArea() will exist used in Cylinder, which does not compute the base of operations area. You lot can set up this problem past using super.getArea() to use the superclass' version of getArea(). Annotation that super.getArea() can just exist issued from the subclass definition, but no from an case created, east.g. c1.super.getArea(), equally information technology break the data hiding and encapsulation principle.
Annotation @Override (JDK i.five)
The "@Override" is known as notation (introduced in JDK i.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 lot wish to override method toString() in a bracket. If @Override is non used and toString() is misspelled as TOString(), it will exist treated every bit a new method in the subclass, instead of overriding the superclass. If @Override is used, the compiler will signal an fault.
@Override notation is optional, merely certainly nice to have.
Annotations are not programming constructs. They have no upshot 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 utilize the keyword this to refer to this example. Similarly, the keyword super refers to the superclass, which could be the immediate parent or its ancestor.
The keyword super allows the subclass to access superclass' methods and variables within the bracket' definition. For case, super() and super(argumentList) can be used invoke the superclass' constructor. If the subclass overrides a method inherited from its superclass, says getArea(), you can use super.getArea() to invoke the superclass' version inside the subclass definition. Similarly, if your subclass hides one of the superclass' variable, you can use super.variableName 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 bracket does not inherit the constructors of its superclasses. Each course in Java defines its own constructors.
In the trunk of a constructor, y'all can apply super(args) to invoke a constructor of its immediate superclass. Note that super(args), if it is used, must be the kickoff statement in the subclass' constructor. If it is non used in the constructor, Java compiler automatically insert a super() statement to invoke the no-arg constructor of its immediate superclass. This follows the fact that the parent must be built-in before the kid 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 just issues a super() telephone call, every bit follows:
public ClassName () { super(); } Have note that:
- The default no-arg constructor will not be automatically generated, if i (or more) constructor was divers. In other words, you need to ascertain no-arg constructor explicitly if other constructors were divers.
- If the immediate superclass does not have the default constructor (information technology defines some constructors only does not define a no-arg constructor), you will get a compilation mistake in doing a
super()call. Notation that Java compiler inserts asuper()as the offset statement in a constructor if there is nosuper(args).
Single Inheritance
Java does not back up multiple inheritance (C++ does). Multiple inheritance permits a bracket 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 just one directly superclass, i.eastward., single inheritance. On the other mitt, a superclass tin have many subclasses.
Common Root Course - java.lang.Object
Java adopts a so-called common-root approach. All Java classes are derived from a common root form called coffee.lang.Object. This Object class defines and implements the common behaviors that are required of all the Coffee objects running under the JRE. These common behaviors enable the implementation of features such every bit multi-threading and garbage collector.
Inheritance EG. 2: The Point2D and Point3D Classes
The Superclass Point2D.java
public form Point2D { private int x, y; public Point2D() { this.x = 0; this.y = 0; } public Point2D(int ten, int y) { this.10 = x; this.y = y; } public int getX() { render this.x; } public void setX(int x) { this.x = x; } public int getY() { return this.y; } public void setY(int y) { this.y = y; } @Override public String toString() { return "(" + this.x + "," + this.y + ")"; } } The Subclass Point3D.java
1 2 3 4 5 6 vii eight 9 x 11 12 13 14 15 sixteen 17 xviii nineteen 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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() { render "(" + super.getX() + "," + super.getY() + "," + this.z + ")"; } } |
A Test Commuter for Point2D and Point3D Classes (TestPoint2DPoint3D.java)
1 2 3 four 5 6 7 8 9 10 11 12 thirteen 14 fifteen 16 17 xviii 19 twenty 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); Organisation.out.println(p2a); Point2D p2b = new Point2D(); System.out.println(p2b); p2a.setX(three); p2a.setY(4); Organization.out.println(p2a); System.out.println("x is: " + p2a.getX()); Arrangement.out.println("ten 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); Organization.out.println(p3a); System.out.println("x is: " + p3a.getX()); System.out.println("y is: " + p3a.getY()); Organisation.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 Person to store common properties such as proper name and address, and subclasses Student and Instructor for their specific properties. For students, we need to maintain the courses taken and their corresponding grades; add a grade with grade, impress all courses taken and the average grade. Presume that a student takes no more than 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 five courses concurrently.
Nosotros blueprint the classes as follows.
The Superclass Person.java
public class Person { individual String name, accost; public Person(Cord proper noun, String accost) { this.name = proper noun; this.address = address; } public String getName() { return proper noun; } public String getAddress() { return address; } public void setAddress(String address) { this.address = accost; } @Override public String toString() { return proper noun + "(" + address + ")"; } } The Subclass Student.java
public form Student extends Person { private int numCourses; individual String[] courses; private int[] grades; private static final int MAX_COURSES = 30; public Pupil(String proper noun, Cord accost) { super(name, address); numCourses = 0; courses = new String[MAX_COURSES]; grades = new int[MAX_COURSES]; } @Override public String toString() { render "Pupil: " + super.toString(); } public void addCourseGrade(Cord course, int grade) { courses[numCourses] = class; grades[numCourses] = grade; ++numCourses; } public void printGrades() { System.out.impress(this); for (int i = 0; i < numCourses; ++i) { System.out.print(" " + courses[i] + ":" + grades[i]); } Arrangement.out.println(); } public double getAverageGrade() { int sum = 0; for (int i = 0; i < numCourses; i++ ) { sum += grades[i]; } render (double)sum/numCourses; } } The Subclass Teacher.java
public class Instructor extends Person { individual int numCourses; individual String[] courses; individual static terminal int MAX_COURSES = 5; public Teacher(Cord name, String accost) { super(proper noun, 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 simulated; } courses[numCourses] = course; numCourses++; return truthful; } public boolean removeCourse(String class) { boolean establish = faux; int courseIndex = -1; for (int i = 0; i < numCourses; i++) { if (courses[i].equals(form)) { courseIndex = i; establish = true; pause; } } if (found) { for (int i = courseIndex; i < numCourses-i; i++) { courses[i] = courses[i+ane]; } numCourses--; render true; } else { return false; } } } A Examination Driver (TestPerson.coffee)
public class TestPerson { public static void master(String[] args) { Educatee s1 = new Student("Tan Ah Teck", "one Happy Ave"); s1.addCourseGrade("IM101", 97); s1.addCourseGrade("IM102", 68); s1.printGrades(); System.out.println("Boilerplate is " + s1.getAverageGrade()); Teacher t1 = new Instructor("Paul Tan", "8 sunset way"); Arrangement.out.println(t1); String[] courses = {"IM101", "IM102", "IM101"}; for (String course: courses) { if (t1.addCourse(course)) { Organisation.out.println(course + " added"); } else { System.out.println(class + " cannot be added"); } } for (String grade: courses) { if (t1.removeCourse(course)) { System.out.println(course + " removed"); } else { System.out.println(form + " 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 indicate"
Remember that there are two ways of reusing existing classes: limerick and inheritance. We have seen that a Line class can exist implemented using composition of Point class - "A line is composed of two points", in the previous section.
A Line tin can also be implemented, using inheritance from the Signal class - "A line is a point extended by some other indicate". Let'southward call this subclass LineSub (to differentiate from the Line class using composition).
The Superclass Point.java
As above.
The Bracket LineSub.java
1 2 3 4 five 6 7 8 9 ten 11 12 xiii fourteen xv 16 17 18 xix 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 fifty 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 lxx 71 72 73 74 75 76 77 78 79 80 81 82 | public course 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, Indicate finish) { super(begin.getX(), brainstorm.getY()); this.end = end; } public Point getBegin() { return this; } public Signal getEnd() { return finish; } public void setBegin(Indicate brainstorm) { super.setX(begin.getX()); super.setY(begin.getY()); } public void setEnd(Point end) { this.end = finish; } public int getBeginX() { render super.getX(); } public void setBeginX(int ten) { 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 ten) { terminate.setX(ten); } public int getEndY() { render 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 Cord toString() { return "LineSub[begin=" + super.toString() + ",end=" + end + "]"; } public double getLength() { return super.altitude(cease); } } |
A Test Commuter (TestLineSub.java)
1 2 3 4 v 6 7 8 9 10 xi 12 thirteen 14 fifteen xvi 17 eighteen 19 20 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 36 37 38 39 xl 41 | public class TestLineSub { public static void main(String[] args) { LineSub l1 = new LineSub(i, ii, 3, 4); System.out.println(l1); LineSub l2 = new LineSub(new Point(5,six), new Point(7,eight)); Organisation.out.println(l2); l1.setBegin(new Point(11, 12)); l1.setEnd(new Point(thirteen, fourteen)); Arrangement.out.println(l1); Organization.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("brainstorm'southward ten is: " + l1.getBeginX()); System.out.println("begin'south y is: " + l1.getBeginY()); Arrangement.out.println("end's x is: " + l1.getEndX()); System.out.println("cease'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'due south y is: " + l1.getBeginXY()[1]); System.out.println("stop'southward ten is: " + l1.getEndXY()[0]); System.out.println("end's y is: " + l1.getEndXY()[ane]); Arrangement.out.printf("length is: %.2f%n", l1.getLength()); } } |
Notes: This is the same test driver used in the earlier example on composition, except alter in classname.
Written report both versions of the Line class (Line and LineSub). 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 some other betoken".
Rule of Thumb: Use composition if possible, before because inheritance. Apply 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" (ways form). For examples, in chemistry, carbon exhibits polymorphism considering it tin be plant in more one grade: graphite and diamond. Merely, each of the form has it own distinct properties (and cost).
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 bracket object can practise whatsoever its superclass can do. As a event, we can substitute a subclass example when a superclass example is expected, and everything shall work fine. This is chosen substitutability.
In our before example of Circle and Cylinder: Cylinder is a bracket of Circle. We tin say that Cylinder "is-a" Circle (actually, it "is-more than-than-a" Circle). Subclass-superclass exhibits a so called "is-a" relationship.
Circle.java
public class Circumvolve { private double radius; public Circle(double radius) { this.radius = radius; } public double getRadius() { render this.radius; } public double getArea() { return radius * radius * Math.PI; } public String toString() { return "Circle[radius=" + radius + "]"; } } Cylinder.java
public class Cylinder extends Circumvolve { private double height; public Cylinder(double height, double radius) { super(radius); this.elevation = height; } public double getHeight() { return this.elevation; } public double getVolumne() { return super.getArea() * superlative; } @Override public double getArea() { render 2.0 * Math.PI * getRadius() * summit; } @Override public String toString() { return "Cylinder[pinnacle=" + height + "," + super.toString() + "]"; } } Via substitutability, we tin create an example of Cylinder, and assign it to a Circle (its superclass) reference, as follows:
Circle c1 = new Cylinder(1.ane, 2.2);
Yous tin invoke all the methods defined in the Circle grade for the reference c1, (which is actually holding a Cylinder object), e.thou.
System.out.println(c1.getRadius()); This is because a subclass instance possesses all the properties of its superclass.
However, you CANNOT invoke methods defined in the Cylinder class for the reference c1, e.k.
c1.getHeight(); //compilation error: cannot find symbol method getHeight() c1.getVolume(); //compilation error: cannot detect symbol method getVolume()
This is because c1 is a reference to the Circle class, which does not know nigh methods defined in the subclass Cylinder.
c1 is a reference to the Circle class, merely holds an object of its subclass Cylinder. The reference c1, however, retains its internal identity. In our example, the subclass Cylinder overrides methods getArea() and toString(). c1.getArea() or c1.toString() invokes the overridden version divers in the subclass Cylinder, instead of the version defined in Circle. This is because c1 is in fact holding a Cylinder object internally.
System.out.println(c1.toString()); Cylinder[height=1.ane,Circle[radius=two.2]] System.out.println(c1.getArea());
Summary
- A subclass example tin can be assigned (substituted) to a superclass' reference.
- Once substituted, we can invoke methods defined in the superclass; we cannot invoke methods defined in the subclass.
- However, if the subclass overrides inherited methods from the superclass, the subclass (overridden) versions volition exist invoked.
Polymorphism EG. 1: Shape and its Subclasses
Polymorphism is very powerful in OOP to split the interface and implementation so as to let the programmer to program at the interface in the pattern of a complex system.
Consider the following example. Suppose that our program uses many kinds of shapes, such equally triangle, rectangle and so on. We should design a superclass called Shape, which defines the public interfaces (or behaviors) of all the shapes. For example, we would like all the shapes to take a method called getArea(), which returns the expanse of that item shape. The Shape class can be written as follow.
Superclass Shape.coffee
public course Shape { private Cord color; public Shape (Cord color) { this.color = color; } @Override public String toString() { return "Shape[color=" + colour + "]"; } public double getArea() { System.err.println("Shape unknown! Cannot compute expanse!"); render 0; } } Accept note that we have a problem writing the getArea() method in the Shape class, because the area cannot be computed unless the bodily shape is known. We shall print an error message for the fourth dimension being. In the afterwards section, I shall show yous how to resolve this problem.
We tin then derive subclasses, such as Triangle and Rectangle, from the superclass Shape.
Bracket Rectangle.coffee
public class Rectangle extends Shape { private int length, width; public Rectangle(Cord colour, int length, int width) { super(color); this.length = length; this.width = width; } @Override public String toString() { render "Rectangle[length=" + length + ",width=" + width + "," + super.toString() + "]"; } @Override public double getArea() { return length*width; } } Subclass Triangle.java
public class Triangle extends Shape { individual int base, elevation; public Triangle(String color, int base of operations, int height) { super(color); this.base = base of operations; this.height = height; } @Override public String toString() { return "Triangle[base=" + base of operations + ",height=" + height + "," + super.toString() + "]"; } @Override public double getArea() { return 0.5*base of operations*height; } } The subclasses override the getArea() method inherited from the superclass, and provide the proper implementations for getArea().
A Test Driver (TestShape.java)
In our application, we could create references of Shape, and assigned them instances of subclasses, as follows:
public class TestShape { public static void main(String[] args) { Shape s1 = new Rectangle("cherry-red", 4, 5); Arrangement.out.println(s1); Rectangle[length=iv,width=5,Shape[color=red]] Arrangement.out.println("Area is " + s1.getArea()); Shape s2 = new Triangle("bluish", iv, 5); System.out.println(s2); Triangle[base=4,peak=v,Shape[color=blue]] System.out.println("Expanse is " + s2.getArea()); } } 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 bracket instance, and the lawmaking notwithstanding works. You could extend your program hands past adding in more than subclasses, such equally Circle, Square, etc, with ease.
Nevertheless, the above definition of Shape class poses a problem, if someone instantiate a Shape object and invoke the getArea() from the Shape object, the program breaks.
public class TestShape { public static void chief(String[] args) { Shape s3 = new Shape("dark-green"); System.out.println(s3); System.out.println("Area is " + s3.getArea()); } } This is considering the Shape class is meant to provide a common interface to all its subclasses, which are supposed to provide the bodily implementation. We do not desire anyone to instantiate a Shape instance. This problem tin can be resolved by using the so-called abstract class.
Polymorphism EG. two: Monster and its Subclasses
Polymorphism is a powerful mechanism in OOP to separate the interface and implementation so every bit to allow the programmer to program at the interface in the design of a complex arrangement. For instance, in our game app, we accept many types of monsters that tin attack. We shall design a superclass chosen Monster and define the method attack() in the superclass. The subclasses shall and so provides their actual implementation. In the principal 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.proper noun = proper name; } public String attack() { return "!^_&^$@+%$* I don't know how to attack!"; } } Subclass FireMonster.coffee
public class FireMonster extends Monster { public FireMonster(String proper name) { super(name); } @Override public String assault() { return "Attack with fire!"; } } Subclass WaterMonster.java
public class WaterMonster extends Monster { public WaterMonster(Cord proper name) { super(name); } @Override public Cord attack() { return "Assail with water!"; } } Subclass StoneMonster.coffee
public class StoneMonster extends Monster { public StoneMonster(Cord name) { super(name); } @Override public String assault() { render "Set on with stones!"; } } A Test Driver TestMonster.coffee
public class TestMonster { public static void principal(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"); Organization.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 course diagram, subclass is often drawn below its superclass. Upcasting is ever safety because a subclass instance possesses all the properties of its superclass and can do whatever its superclass tin can do. The compiler checks for valid upcasting and bug error "incompatible types" otherwise. For example,
Circle c1 = new Cylinder(i.1, ii.two); Circle c2 = new Cord();
Downcasting a Substituted Reference to Its Original Grade
You can revert a substituted case back to a subclass reference. This is called "downcasting". For case,
Circle c1 = new Cylinder(i.1, 2.2); Cylinder cy1 = (Cylinder) c1;
Downcasting requires explicit type casting operator in the form of prefix operator (new-blazon). Downcasting is not always rubber, and throws a runtime ClassCastException if the instance to be downcasted does not vest 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() { Organisation.out.println("Constructed an instance of A"); } @Override public Cord toString() { return "This is A"; } } public class B extends A { public B() { super(); System.out.println("Constructed an instance of B"); } @Override public Cord toString() { return "This is B"; } } public form C extends B { public C() { super(); System.out.println("Constructed an instance of C"); } @Override public Cord toString() { return "This is C"; } } The following plan tests the upcasting an downcasting (refer to the above instance diagram):
public class TestCasting { public static void master(Cord[] args) { A a1 = new C(); Constructed an instance of A System.out.println(a1); B b1 = (B)a1; Organisation.out.println(b1); C c1 = (C)b1; System.out.println(c1); A a2 = new B(); Organisation.out.println(a2); B b2 = (B)a2; C c2 = (C)a2; compilation okay, but runtime error: } } Casting Operator
Compiler may not be able to notice mistake in explicit bandage, which will be detected merely at runtime. For case,
Circle c1 = new Circumvolve(5); Point p1 = new Point(); c1 = p1; compilation error: incompatible types (Point is not a subclass of Circle) c1 = (Circle)p1; The "instanceof" Operator
Java provides a binary operator called instanceof which returns truthful if an object is an case of a detail class. The syntax is as follows:
anObject instanceof aClass
Circle c1 = new Circle(); System.out.println(c1 instanceof Circle); if (c1 instanceof Circumvolve) { ...... } An instance of bracket is also an instance of its superclass. For example,
Circle c1 = new Circle(1.ane); Cylinder cy1 = new Cylinder(2.2, 3.iii); System.out.println(c1 instanceof Circle); System.out.println(c1 instanceof Cylinder); Arrangement.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
- A subclass instance processes all the attributes operations of its superclass. When a superclass instance is expected, it can exist substituted by a subclass case. In other words, a reference to a course may hold an example of that class or an instance of one of its subclasses - it is called substitutability.
- If a subclass example is assign to a superclass reference, y'all can invoke the methods defined in the superclass only. You lot cannot invoke methods defined in the subclass.
- However, the substituted instance retains its ain identity in terms of overridden methods and hiding variables. If the bracket overrides methods in the superclass, the bracket's version will exist executed, instead of the superclass's version.
Exercises
LINK TO EXERCISES
Abstruse Classes & Interfaces
The abstruse Method and abstruse class
In the to a higher place examples of Shape and Monster, nosotros encountered a problem when we create instances of Shape and Monster and run the getArea() or assail(). This can be resolved via abstruse method and abstract course.
An abstruse method is a method with only signature (i.eastward., the method name, the listing of arguments and the return type) without implementation (i.east., the method'south torso). Yous utilise the keyword abstract to declare an abstract method.
For case, in the Shape grade, we can declare abstract methods getArea(), draw(), etc, as follows:
abstract public class Shape { ...... ...... abstract public double getArea(); abstruse public double getPerimeter(); abstract public void draw(); }
Implementation of these methods is Not possible in the Shape class, as the bodily shape is not all the same known. (How to compute the area if the shape is not known?) Implementation of these abstract methods volition be provided later once the actual shape is known. These abstruse methods cannot be invoked because they have no implementation.
A class containing one or more than abstract methods is chosen an abstruse class. An abstract course must be declared with a form-modifier abstract. An abstruse class CANNOT be instantiated, as its definition is not complete.
UML Notation: abstract class and method are shown in italic.
Abstract Grade EG. 1: Shape and its Subclasses
Let us rewrite our Shape class equally an abstract form, containing an abstract method getArea() as follows:
The abstract Superclass Shape.java
abstract public grade Shape { private Cord color; public Shape (String color) { this.color = color; } @Override public Cord toString() { return "Shape[color=" + color + "]"; } abstract public double getArea(); }
An abstract class is incomplete in its definition, since the implementation of its abstract methods is missing. Therefore, an abstract class cannot be instantiated. In other words, yous cannot create instances from an abstract class (otherwise, you will have an incomplete instance with missing method's torso).
To use an abstract form, you have to derive a subclass from the abstract grade. In the derived subclass, you have to override the abstruse methods and provide implementation to all the abstruse methods. The subclass derived is now consummate, and can be instantiated. (If a bracket does non provide implementation to all the abstruse methods of the superclass, the subclass remains abstract.)
This holding of the abstruse class solves our earlier problem. In other words, you can create instances of the subclasses such every bit Triangle and Rectangle, and upcast them to Shape (so as to program and operate at the interface level), merely yous cannot create instance of Shape, which avoid the pitfall that nosotros have faced. For example,
public class TestShape { public static void primary(String[] args) { Shape s1 = new Rectangle("red", iv, v); System.out.println(s1); System.out.println("Area is " + s1.getArea()); Shape s2 = new Triangle("blue", 4, 5); Organisation.out.println(s2); System.out.println("Area is " + s2.getArea()); Shape s3 = new Shape("green"); } } In summary, an abstract form 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 abstract form Shape, you tin can define abstract methods such equally getArea() and describe(). No implementation is possible because the actual shape is not known. However, by specifying the signature of the abstract methods, all the subclasses are forced to use these methods' signature. The subclasses could provide the proper implementations.
Coupled with polymorphism, you tin upcast subclass instances to Shape, and program at the Shape level, i,e., programme at the interface. The separation of interface and implementation enables better software design, and ease in expansion. For example, Shape defines a method chosen getArea(), which all the subclasses must provide the correct implementation. You can ask for a getArea() from any subclasses of Shape, the correct surface area volition be computed. Furthermore, you application can exist extended hands to adapt new shapes (such as Circle or Square) by deriving more subclasses.
Dominion 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
terminal, equallylastmethod cannot be overridden. Anabstractmethod, on the other hand, must be overridden in a descendant before information technology can be used. - An
abstractmethod cannot beprivate(which generates a compilation fault). This is consideringindividualmethod are non visible to the bracket and thus cannot be overridden.
Abstract Class EG. 2: Monster
We shall define the superclass Monster as an abstract course, containing an abstruse method attack(). The abstruse class cannot be instantiated (i.e., creating instances).
abstract public class Monster { private String proper noun; public Monster(String name) { this.proper noun = proper name; } abstract public String set on(); }
The Java's interface
A Coffee interface is a 100% abstract superclass which ascertain a set of methods its subclasses must support. An interface contains simply public abstract methods (methods with signature and no implementation) and possibly constants (public static last variables). You accept to use the keyword "interface" to ascertain an interface (instead of keyword "course" for normal classes). The keyword public and abstract 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 volition not be covered in this article.)
Similar to an abstract superclass, an interface cannot be instantiated. Yous have to create a "subclass" that implements an interface, and provide the actual implementation of all the abstract methods.
Unlike a normal grade, where you lot use the keyword "extends" to derive a subclass. For interface, nosotros use the keyword "implements" to derive a subclass.
An interface is a contract for what the classes can do. It, however, does non specify how the classes should do it.
An interface provides a grade, a protocol, a standard, a contract, a specification, a prepare 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, abstract class and interface are used to split the public interface of a grade from its implementation so as to allow the programmer to program at the interface instead of the various implementation.
Interface Naming Convention: Apply an adjective (typically ends with "able") consisting of ane or more words. Each word shall be initial capitalized (camel-example). For example, Serializable, Extenalizable, Movable, Clonable, Runnable, etc.
Interface EG. ane: Shape Interface and its Implementations
We tin can re-write the abstruse superclass Shape into an interface, containing but abstract methods, as follows:
UML Notations: Abstruse 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, width; public Rectangle(int length, int width) { this.length = length; this.width = width; } @Override public Cord toString() { return "Rectangle[length=" + length + ",width=" + width + "]"; } @Override public double getArea() { return length * width; } } public grade Triangle implements Shape { private int base, height; public Triangle(int base of operations, int summit) { this.base of operations = base; this.top = superlative; } @Override public String toString() { render "Triangle[base=" + base + ",height=" + height + "]"; } @Override public double getArea() { return 0.5 * base of operations * height; } } A test driver is every bit follows:
public course TestShape { public static void master(String[] args) { Shape s1 = new Rectangle(ane, two); System.out.println(s1); Organization.out.println("Surface area is " + s1.getArea()); Shape s2 = new Triangle(three, 4); System.out.println(s2); Organization.out.println("Area is " + s2.getArea()); } } Interface EG. ii: Movable Interface and its Implementations
Suppose that our application involves many objects that can motility. Nosotros could define an interface chosen movable, containing the signatures of the various motility methods.
Interface Moveable.coffee
public interface Movable { public void moveUp(); public void moveDown(); public void moveLeft(); public void moveRight(); } Similar to an abstruse class, an interface cannot exist instantiated; because information technology is incomplete (the abstract methods' trunk is missing). To use an interface, again, you must derive subclasses and provide implementation to all the abstract methods alleged in the interface. The subclasses are now complete and tin can be instantiated.
MovablePoint.java
To derive subclasses from an interface, a new keyboard "implements" is to be used instead of "extends" for deriving subclasses from an ordinary form or an abstract class. Information technology is of import to note that the subclass implementing an interface need to override ALL the abstract methods defined in the interface; otherwise, the bracket cannot be compiled. For example,
public class MovablePoint implements Movable { private int x, y; public MovablePoint(int x, int y) { this.x = x; this.y = y; } @Override public Cord toString() { return "(" + x + "," + y + ")"; } @Override public void moveUp() { y--; } @Override public void moveDown() { y++; } @Override public void moveLeft() { x--; } @Override public void moveRight() { x++; } } Other classes in the application can similarly implement the Movable interface and provide their own implementation to the abstract methods divers in the interface Movable.
TestMovable.java
We can also upcast subclass instances to the Movable interface, via polymorphism, similar to an abstract class.
public grade TestMovable { public static void master(Cord[] args) { MovablePoint p1 = new MovablePoint(1, two); System.out.println(p1); p1.moveDown(); System.out.println(p1); p1.moveRight(); Organization.out.println(p1); Movable p2 = new MovablePoint(three, 4); p2.moveUp(); System.out.println(p2); MovablePoint p3 = (MovablePoint)p2; Organization.out.println(p3); } } Implementing Multiple Interfaces
As mentioned, Coffee supports just unmarried inheritance. That is, a subclass can be derived from one and only one superclass. Java does not support multiple inheritance to avert inheriting alien backdrop from multiple superclasses. Multiple inheritance, yet, does have its place in programming.
A subclass, however, can implement more than i interfaces. This is permitted in Java as an interface merely defines the abstract methods without the actual implementations and less likely leads to inheriting conflicting properties from multiple interfaces. In other words, Java indirectly supports multiple inheritances via implementing multiple interfaces. For example,
public class Circle extends Shape implements Movable, Adaptable { ....... } interface Formal Syntax
The formal syntax for declaring interface is:
[public|protected|package] interface interfaceName [extends superInterfaceName] { static last ...; ... } All methods in an interface shall exist public and abstract (default). You cannot use other access modifier such every bit private, protected and default, or modifiers such every bit static, final.
All fields shall be public, static and final (default).
An interface may "extends" from a super-interface.
UML Notation: The UML note uses a solid-line arrow linking the subclass to a physical or abstract superclass, and dashed-line arrow to an interface as illustrated. Abstract class and abstruse method are shown in italics.
Why interfaces?
An interface is a contract (or a protocol, or a mutual agreement) of what the classes tin do. When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface. Interface defines a set up of common behaviors. The classes implement the interface agree to these behaviors and provide their own implementation to the behaviors. This allows you to program at the interface, instead of the actual implementation. One of the main usage of interface is provide a communication contract betwixt two objects. If you know a class implements an interface, then you lot know that class contains concrete implementations of the methods alleged in that interface, and you lot are guaranteed to be able to invoke these methods safely. In other words, two objects tin can communicate based on the contract defined in the interface, instead of their specific implementation.
Secondly, Coffee does not support multiple inheritance (whereas C++ does). Multiple inheritance permits you to derive a subclass from more than than ane direct superclass. This poses a problem if ii direct superclasses have conflicting implementations. (Which 1 to follow in the subclass?). However, multiple inheritance does accept its place. Coffee does this by permitting you to "implements" more than ane interfaces (but y'all tin simply "extends" from a single superclass). Since interfaces incorporate simply abstract methods without actual implementation, no conflict can arise amid the multiple interfaces. (Interface can concur constants but is not recommended. If a bracket implements two interfaces with conflicting constants, the compiler will flag out a compilation error.)
Interface vs. Abstruse Superclass
Which is a better design: interface or abstruse superclass? There is no clear answer.
Use abstract superclass if at that place is a clear class hierarchy. Abstract class can incorporate partial implementation (such as instance variables and methods). Interface cannot comprise any implementation, only merely defines the behaviors.
As an instance, Coffee's thread tin be built using interface Runnable or superclass Thread.
Exercises
LINK TO EXERCISES ON POLYMORPHISM, ABSTRACT CLASSES AND INTERFACES
(Advanced) Dynamic Binding or Late Binding
We often care for an object not as its own type, simply equally its base blazon (superclass or interface). This allows you lot to write codes that do non depends on a specific implementation blazon. In the Shape example, nosotros tin can always use getArea() and do not have to worry whether they are triangles or circles.
This, however, poses a new trouble. The compiler cannot know at compile fourth dimension precisely which piece of codes is going to be executed at run-time (eastward.g., getArea() has dissimilar implementation for Rectangle and Triangle).
In the procedural language like C, the compiler generates a call to a specific function name, and the linkage editor resolves this call to the accented address of the code to be executed at run-time. This machinery is called static binding (or early binding).
To back up polymorphism, object-oriented linguistic communication uses a different mechanism called dynamic bounden (or tardily-binding or run-time binding). When a method is invoked, the lawmaking to be executed is only determined at run-fourth dimension. During the compilation, the compiler checks whether the method exists and performs blazon bank check on the arguments and return type, but does not know which slice of codes to execute at run-time. When a message is sent to an object to invoke a method, the object figures out which piece of codes to execute at run-time.
Although dynamic binding resolves the problem in supporting polymorphism, it poses another new trouble. The compiler is unable to check whether the type casting operator is safe. Information technology tin simply exist checked during runtime (which throws a ClassCastException if the blazon check fails).
JDK one.five introduces a new feature called generics to tackle this outcome. We shall talk over this trouble and generics in details in the later chapter.
Exercises
LINK TO EXERCISES
(Advanced) Object-Oriented Design Problems
Encapsulation, Coupling & Cohesion
In OO Design, it is desirable to design classes that are tightly encapsulated, loosely coupled and highly cohesive, then that the classes are easy to maintain and suitable for re-utilize.
Encapsulation refers to keeping the data and method within a class such users do non access the data directly but via the public methods. Tight encapsulation is desired, which tin can be achieved by declaring all the variable private, and providing public getter and setter to the variables. The benefit is you have complete control on how the data is to be read (eastward.g., in how format) and how to the data is to be changed (e.g., validation).
[TODO] Instance: Fourth dimension class with private variables hour (0-23), minute (0-59) and second (0-59); getters and setters (throws IllegalArgumentException). The internal time could likewise be stored as the number of seconds since midnight for ease of operation (data hiding).
Information Hiding: Another key do good of tight encapsulation is information hiding, which means that the users are not aware (and do not need to exist aware) of how the data is stored internally.
The benefit of tight encapsulation out-weights the overhead needed in additional method calls.
Coupling refers to the degree to which one form relies on knowledge of the internals of another grade. Tight coupling is undesirable considering if 1 grade changes its internal representations, all the other tightly-coupled classes demand to exist rewritten.
[TODO] Example: A class uses Time and relies on the variables hr, minute and 2nd.
Clearly, Loose Coupling is oft associated with tight encapsulation. For example, well-defined public method for accessing the data, instead of directly access the data.
Cohesion refers to the degree to which a class or method resists being broken downward into smaller pieces. High caste of cohesion is desirable. Each class shall be designed to model a unmarried entity with its focused set of responsibilities and perform a drove of closely related tasks; and each method shall achieve a unmarried chore. Low cohesion classes are difficult to maintain and re-utilise.
[TODO] Example of low cohesion: Book and Author in one grade, or Auto and Commuter in one class.
Again, high cohesion is associated with loose coupling. This is because a highly cohesive form has fewer (or minimal) interactions with other classes.
"Is-a" vs. "has-a" relationships
"Is-a" relationship: A subclass object processes all the data and methods from its superclass (and information technology could accept more). Nosotros tin can say that a bracket object is-a superclass object (is more than than a superclass object). Refer to "polymorphism".
"has-a" relationship: In composition, a class contains references to other classes, which is known as "has-a" human relationship.
You can employ "is-a" and 'has-a" to examination whether to pattern the classes using inheritance or composition.
Program at the interface specification, non the implementation
Refer to polymorphism
LINK TO JAVA REFERENCES & RESOURCES
mascarenasahme1941.blogspot.com
Source: https://www3.ntu.edu.sg/home/ehchua/programming/java/J3b_OOPInheritancePolymorphism.html
Belum ada Komentar untuk "Draw a Circle From a Subclass in Java"
Posting Komentar