Anda di halaman 1dari 7

Khawaja Fareed University of Engineering & Information

Technology
Rahim Yar Khan
Department of Computer Engineering

Object Oriented Programming Lab


Lab 05: Constructors and Method Overriding
with Inheritance
Lab Instructor: Mr. Arslan Qayyum
Purpose:
The objective of this lab is to future teach inheritance and how it works with constructors and
method overriding.

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.

Protected Access Specifier


Protected members are directly accessible by derived classes but not by other users. A class
member labeled protected is accessible to member functions of derived classes as well as to
member functions of the same class.

Derived Class Constructor


Constructors are not inherited, even though they have public visibility. However, the super
reference can be used within the child's constructor to call the parent's constructor. In that case,
the call to parent's constructor must be the first statement.

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 {

protected String name ; protected String id ; protected int phone ;

public person() {

name = "NaginaNazar" ;

id = "sp14bcs039" ;

phone = 12345 ; }

public person(String a , String b , int c) {

name = a ; id = b ; phone = c ; }

public void setName(String a){ name = a ; }


public void setId(String j){ id = j ; }

public void setPhone(int a) { phone = a ; }

public String getName() { return name ; }

public String getid() { return id ; }

public int getPhone() { return phone ; }

public void display( ) {

System.out.println("Name : " + name + "ID : " + id + "Phone : " + phone ) ; }

public class student extends person {

private String rollNo ; private int marks ;

public student() {

super() ; rollNo = "sp14bcs039" ; marks = 345 ; }

public student(String a , String b , int c , String d , int e){

super(a,b,c) ; rollNo = d ; marks = e ; }

public void setRollNo(String a){ rollNo = a ; }

public void setMarks(int a ){ marks = a ; }

public String getRollNo() { returnrollNo ; }

publicintgetMarks() { return marks ; }

public void display( ) {

super.display();

System.out.println("Roll # : " + rollNo + "\nMarks : " + marks) ; }

public class Runner {


public static void main(String []args) {

Student s = new Student (“s-09",Ahmed",”xyz”,”sp16-bcs-98,50);

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 {

private double re, im;

public Complex(double re, double im) {

this.re = re;

this.im = im;

/* Returns the string representation of this Complex number.

The format of string is "Re + iIm" where Re is real part

and Im is imagenary part.*/

@Override

public String toString() {

return String.format(re + " + i" + im);


}

// Overriding equals() to compare two Complex objects

@Override

public boolean equals(Object o) {

// If the object is compared with itself then return true

if (o == this) {

return true;

/* Check if o is an instance of Complex or not

"null instanceof [type]" also returns false */

if (!(o instanceof Complex)) {

return false;

// typecast o to Complex so that we can compare data members

Complex c = (Complex) o;

// Compare the data members and return accordingly

return Double.compare(re, c.re) == 0

&& Double.compare(im, c.im) == 0;

// Driver class to test the Complex class

public class Main {

public static void main(String[] args) {


Complex c1 = new Complex(10, 15);

System.out.println(c1);

Complex c2 = new Complex(10, 15);

Complex c3 = new Complex(10, 15);

if (c2.equals(c3)) {

System.out.println("Equal ");

} else {

System.out.println("Not Equal ");

Activity 3:
Design following inheritance in java Program.

 Class Animal: Data members isVegetarian, number of legs, eats


 Class Cat extended form Animal: Data member color. With 2 methods first to set the
values of data members including its parent class members. Second method to display the
data members including its parent class members.
 Class Dog extended form Animal: Data members color, breed. With 2 methods first to set
the values of data members including its parent class members. Second method to display
the data members including its parent class members.

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.

Anda mungkin juga menyukai