Technology
Rahim Yar Khan
Department of Computer Engineering
Activity Outcomes:
Declaration of base class constructors and using them with child classes.
Usage of function overriding while inheriting classes.
Inheritance Recap:
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in
java by which one class is allow to inherit the features(fields and methods) of another class.
Important terminology:
Super Class: The class whose features are inherited is known as super class(or a base
class or a parent class).
Sub Class: The class that inherits the other class is known as sub class(or a derived
class, extended class, or child class). The subclass can add its own fields and methods in
addition to the superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create
a new class and there is already a class that includes some of the code that we want, we
can derive our new class from the existing class. By doing this, we are reusing the fields
and methods of the existing class.
Method Overriding
The definition of an inherited method can be changed in the definition of a derived class so that it
has a meaning in the derived class that is different from what it is in the base class. This is called
overriding the definition of the inherited method.
In a derived class, you can override (change) the definition of a method from the base class. As a
general rule, when overriding a method definition, you may not change the type returned by the
method, and you may not change a void method to a method that returns a value, nor a method
that returns a value to a void method. The one exception to this rule is if the returned type is a
class type, then you may change the returned type to that of any descendent class of the returned
type. For example, if a function returns the type Employee, when you override the function
definition in a derived class, you may change the returned type to HourlyEmployee,
SalariedEmployee, or any other descendent class of the class Employee. This sort of changed
return type is known as a covariant return type and is new in Java version 5.0; it was not allowed
in earlier versions of Java.
Lab Activities:
Activity 1:
Design a class named Person and its Student.
A person has a (Protected) name, id, phone number with two constructors. First
constructor assigns empty values and second constructor assigns values that are passed in
constructor and a display() Function with setters getters for all attributes as they are
protected.
A student has a (Private) roll number, marks with two constructors first constructor
assigns empty values and second constructor assigns values that are passed in constructor
and a display() Function with setters getters for all attributes as they are protected.
Create Objects of both with different constructors and test.
Solution:
public class person {
public person() {
name = "NaginaNazar" ;
id = "sp14bcs039" ;
phone = 12345 ; }
name = a ; id = b ; phone = c ; }
public student() {
super.display();
s.display(); }
Activity 2:
Design a class Complex.
A Complex class has a Real Number and Imaginary number as private attributes.
With one constructor which assigns both attributes a value passed into constructor.
Now if we create the object of this class and print its value in “System.out.println(c1)” or
compares two objects as c1 and c2 with same values it gives wrong output.
Our goal is to override the toString() and equals() methods to fix their functionality.
Solution:
class Complex {
this.re = re;
this.im = im;
@Override
@Override
if (o == this) {
return true;
return false;
Complex c = (Complex) o;
System.out.println(c1);
if (c2.equals(c3)) {
System.out.println("Equal ");
} else {
Activity 3:
Design following inheritance in java Program.
Assignment:
Implement a Clock class that simulates time in hr:min:sec, derive a child class that overrides the
display method and displays the time in both AM/PM and 24 hour format.