6.lab 06-OOP
6.lab 06-OOP
Laboratory 06
Submission Due: End of laboratory class, demonstrate and submit the file on Google
classroom at least 10 minutes before the end of laboratory class.
Marks will be given only to students who attend and participate during the 2 hours’ laboratory
class. Demonstration and Submission on google classroom is mandatory as evidence of
participation.
Method Overloading
Methods of the same name can be declared in the same class, as long as they have different sets of
parameters (determined by the number, types and order of the parameters)—this is called
method overloading. When an overloaded method is called, the compiler selects the
appropriate method by examining the number, types and order of the arguments in the call.
Method overloading is commonly used to create several methods with the same name that
perform the same or similar tasks, but on different types or different numbers of arguments. For
example, Math methods abs, min and max are overloaded with four versions each:
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
The Java API provides several predefined data structures, called collections, used to store
groups of related objects in memory. These classes provide efficient methods that organize, store
and retrieve your data without requiring knowledge of how the data is being stored. This reduces
application-development time. You’ve used arrays to store sequences of objects. Arrays do not
automatically change their size at execution time to accommodate additional elements. The
collection class
ArrayList<String> list;
declares list as an ArrayList collection that can store only Strings. Classes with this kind of
placeholder that can be used with any type are called generic classes. Only nonprimitive types can be
used to declare variables and create objects of generic classes. However, Java provides a mechanism—
known as boxing—that allows primitive values to be wrapped as objects for use with generic
classes. So, for example,
ArrayList<Integer> integers;
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
declares integers as an ArrayList that can store only Integers. When you place an int value into
an ArrayList<Integer>, the int value is boxed (wrapped) as an Integer object, and when you get
an Integer object from an ArrayList<Integer>, then assign the object to an int variable, the int
value inside the object is unboxed (unwrapped).
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
Shape Hierarchy
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
All public and protected superclass members retain their original access modifier when they
become members of the subclass—public members of the superclass become public members of
the subclass, and protected members of the superclass become protected members of the
subclass. A superclass’s private members are not accessible outside the class itself. Rather, they’re
hidden from its subclasses and can be accessed only through the public or protected methods
inherited from the superclass.
Subclass methods can refer to public and protected members inherited from the superclass
simply by using the member names. When a subclass method overrides an inherited superclass
method, the superclass version of the method can be accessed from the subclass by preceding the
superclass method name with keyword super and a dot (.) separator.
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
super(), or super(parameters);
The statement super() invokes the no-arg constructor of its superclass, and the statement
super(arguments) invokes the superclass constructor that matches the arguments. The
statement super() or super(arguments) must be the first statement of the subclass’s
constructor; this is the only way to explicitly invoke a superclass constructor.
Constructor Chaining
A constructor may invoke an overloaded constructor or its superclass constructor. If neither is
invoked explicitly, the compiler automatically puts super() as the first statement in the
constructor. For example:
In any case, constructing an instance of a class invokes the constructors of all the superclasses
along the inheritance chain. When constructing an object of a subclass, the subclass constructor
first invokes its superclass constructor before performing its own tasks. If the superclass is
derived from another class, the superclass constructor invokes its parent-class constructor before
performing its own tasks. This process continues until the last constructor along the inheritance
hierarchy is called. This is called constructor chaining.
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
The program produces the preceding output. Why? Let us discuss the reason. In line 3, new
Faculty() invokes Faculty’s no-arg constructor. Since Faculty is a subclass of Employee,
Employee’s no-arg constructor is invoked before any statements in Faculty’s constructor are
executed. Employee’s no-arg constructor invokes Employee’s second constructor (line 13).
Since Employee is a subclass of Person, Person’s no-arg constructor is invoked before any
statements in Employee’s second constructor are executed. This process is illustrated in the
following figure.
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
superclass. The syntax is:
super.method(parameters);
You could rewrite the printCircle() method in the Circle class as follows:
It is not necessary to put super before getDateCreated() in this case, however, because
getDateCreated is a method in the GeometricObject class and is inherited by the Circle class.
Nevertheless, in some cases, as shown in the next section, the keyword super is needed.
Overriding Methods
To override a method, the method must be defined in the subclass using the same signature and the same return
type as in its superclass.
A subclass inherits methods from a superclass. Sometimes it is necessary for the subclass to
modify the implementation of a method defined in the superclass. This is referred to as method
overriding. The toString method in the GeometricObject class returns the string representation
of a geometric object. This method can be overridden to return the string representation of a
circle. To override it, add the following new method in the Circle class.
The toString() method is defined in the GeometricObject class and modified in the Circle
class. Both methods can be used in the Circle class. To invoke the toString method defined in
the GeometricObject class from the Circle class, use super.toString() (line 7). Can a subclass
of Circle access the toString method defined in the GeometricObject class using syntax such
as super.super.toString()? No. This is a syntax error. Several points are worth noting:
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
To override a method, the method must be defined in the subclass using the same signature and
the same return type. Let us use an example to show the differences between overriding and
overloading. In (a) below, the method p(double i) in class A overrides the same method defined
in class B. In (b), however, the class A has two overloaded methods: p(double i) and p(int i).
The method p(double i) is inherited from B.
When you run the Test class in (a), both a.p(10) and a.p(10.0) invoke the p(double i) method
defined in class A to display 10.0. When you run the Test class in (b), a.p(10) invokes the p(int
i) method defined in class A to display 10, and a.p(10.0) invokes the p(double i) method
defined in class B to display 20.0.
To avoid mistakes, you can use a special Java syntax, called override annotation, to place
@Override before the method in the subclass. For example:
@Override
public String toString() {
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
return super.toString() + "\nradius is " + radius;
}
}
This annotation denotes that the annotated method is required to override a method in the
superclass. If a method with this annotation does not override its superclass’s method, the
compiler will report an error. For example, if toString is mistyped as tostring, a compile error is
reported. If the override annotation isn’t used, the compile won’t report an error. Using
annotation avoids mistakes.
Exercise 2: (Shape Inheritance Hierarchy) The world of shapes is much richer than the
shapes included in the inheritance hierarchy of Figure below. Write down all the shapes you can
think of—both two-dimensional and three-dimensional—and form them into a more complete
Shape hierarchy with as many levels as possible. Your hierarchy should have class Shape at the
top. Classes TwoDimensionalShape and ThreeDimensionalShape should extend Shape. Add
additional subclasses, such as Quadrilateral and Sphere, at their correct locations in the hierarchy
as necessary.
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka
Object Oriented Programming: Java
Laboratory 06
Prepared by: Dr. Md Ashraf Uddin, Associate Professor, Jagannath University, Dhaka