Professional Documents
Culture Documents
Lab 3 - Inheritance and Association
Lab 3 - Inheritance and Association
Lab-03
Inheritance and Association
Lab 3: Inheritance and Association
Table of Contents
1. Introduction 21
4. Concept Map 23
6. Procedure& Tools 25
6.1 Tools 25
6.2 Setting-up JDK 1.7 [Expected time = 5mins] 25
6.2.1 Compile a Program 25
6.2.2 Run a Program 26
6.3 Walkthrough Task[Expected time = 30mins] 26
6.3.1 Implementing Inheritance 26
6.3.2 Implementing Association 27
6.3.3 Aggregation 28
6.3.4 Composition 31
7. Practice Tasks 33
7.1 Out comes 34
7.2 Testing 34
9. Evaluation criteria 34
1. Introduction
You have learnt Java constructs, and object-oriented relationships in java. In this lab, you will
test the theory by implementing relationships. In OOP, various kinds of relationships (i.e.
inheritance, association, aggregation, composition) exist between various classes/objects. Java is
a strong supporter of OOP. Majority of the community know Java as Pure Object-Oriented
Language. Hence Java supports all of these relationships.
Inheritance is modeled as is-a relationship. Inheritance exists between classes and the main goal
of inheritance is to make objects that would represent specialized versions of generalized original
objects. For example, a car is-a vehicle, manager is-an employee etc. Inheritance is perhaps the
strongest way of reusing the features of a class i.e. attributes (data member) and behaviors
(functions). Following the OOP paradigm, Java allows the inheritance among classes so that the
characteristic(s) of one class is/are inherited by the other class(es). Hence, you can say that the
derived classes receive some their attributes from which the derived class is inherited. A major
advantage of inheritance is that it allows the reusability of code. Hence, the programmer can
simply create new (child) class(es) by using other (parent) class(es).
While considering the advantages of commonality between classes, in Java, you are able to
manage your classes in a way that a class (child/sub class) can extend the functionality by
inheriting the attributes and behaviors of an existing class commonly known as base/super class.
In simple words, you can define a class with certain data fields and/or member functions and
then identify other class(es) that can share these data fields and functions. In typical Java
inheritance, a class or classes known as derived class(es), subclass(es) or child class(es) is/are
able to inherit certain attributes and behavior of pre-existing class(es) that are known as base
class(es), superclass(es), or parent class(es). In practice, base classes are more general while
derived classes are specialized version of base classes and due to this reason, it is said that
inheritance maintains generalization and specialization. With inheritance, you can greatly save
the time and lessen the effort to write duplicate code.
Concerning inheritance syntax in Java, if you have a base class named person inherited by a
derived class named student then you have to write code in the following way
In case of derived class’s object instantiation, In Java, unlike C++, all classes inherit from the
Object class directly or indirectly. Therefore, there is always a single inheritance tree of classes
in Java, and Object class is root of the tree. In Java, if we create a class that doesn’t inherit from
any class then it automatically inherits from Object class. In Java, methods are virtual by default.
Java doesn’t provide an inheritance specifier like public, protected or private. Therefore, we
cannot change the protection level of members of base class in Java, if some data member is
public or protected in base class then it remains public or protected in derived class. Java doesn’t
support multiple inheritance. A class cannot inherit from more than one class. A class can
implement multiple interfaces though.
Following Java example shows that Test class automatically inherits from Object class.
class Test {
// members of test
}
class Main {
public static void main(String[] args) {
Test t = new Test();
System.out.println("t is instanceof Object: " + (t instanceof Object));
}
}
Following example explains the syntax for aggregation and composition in Java:
class Vehicle
{
Department of Computer Page 22
Science,
C.U.S.T.
Lab 3: Inheritance and Association
Above example shows that aggregation exists between battery and vehicle because of the
possession of ownership but not life-cycle dependency. On the contrary, composition exists
between vehicle and engine because of the possession of ownership and life-cycle dependency.
To get basic understanding of Object Oriented relations and how they are implemented
using Java.
4. Concept Map
This section provides you the overview of the concepts that will be discussed and implemented
in this lab.
Primarily two tasks are the part of each object-oriented design i.e. identification of object types
within the problem domain and relationship modeling between the existing object types. Three
kinds of relationships usually exist between identified objects i.e.
Department of Computer Page 23
Science,
C.U.S.T.
Lab 3: Inheritance and Association
The is-a relationship exhibits a relationship between an object of specific type to its general type.
For example, a bus is-a vehicle, an employee is-a person, circle is-a geometric shape etc. The is-
a relationship has been modeled in terms of inheritance that you have already studied in Lectures
15-18.
The uses-a relationship demonstrates that an object of one specific type uses an object of
different type to perform some activity. For example, a person uses-a wiper to wash bus window
pane, a teacher uses-a course to teach student, a cyclist uses-a pump in tire-pumping activity).
The uses-a relationship can be described as association between objects i.e. a teacher has
association with student through a specific course. Similarly, university has association with its
departments through administration. In simple words, association is a relationship between
objects where exists no ownership and each object has its own lifecycle. More specifically,
association are of two types i.e. aggregation and composition that represents has-a relationship
between objects.
You must solve the following problems at home before the lab.
After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you.
Draw a UML diagram for the following task. You are required to bring this design with you
and submit to your lab instructor.
Design a class named Person and its two derived classes named Student and Teacher. There is
another class that contains many students and a teacher.
Draw UML diagram for each class and show inheritance relationship among all classes.
6. Procedure& Tools
6.1 Tools
javac Interface.java
After the execution of the above statement bytecode of your class will be generated with same
name as the .java file but its extension will change to .class.
Similarly, compile all classes implementing the interfaces.
After successfully completing the section 6.2.1, the only thing left is to execute the bytecode on
the JVM. Run the file containing main method in it. Use the following command to run the
program.
java DriverClass
If the above command executed successfully then you will see output of the program.
This task is designed to guide you towards creating your own abstract class and interface and
running the program.
import java.io.*;
// class bank
class Bank
{
private String name;
// bank name
Bank(String name)
{
this.name = name;
}
// employee class
class Employee
{
private String name;
// employee name
Employee(String name)
{
this.name = name;
}
System.out.println(emp.getEmployeeName() +
" is employee of " + bank.getBankName());
}
}
Output:
In above example two separate classes Bank and Employee are associated through their Objects.
Bank can have many employees, So it is a one-to-many relationship.
6.3.3 Aggregation
It is a special form of Association where, It represents Has-A relationship. It is a unidirectional
association i.e. a one way relationship.
For example, department can have students but vice versa is not possible and thus unidirectional
in nature.
In Aggregation, both the entries can survive individually which means ending one entity will not
effect the other entity
import java.io.*;
import java.util.*;
// student class
class Student
{
String name;
int id ;
String dept;
this.name = name;
this.id = id;
this.dept = dept;
}
}
String name;
private List<Student> students;
Department(String name, List<Student> students)
{
this.name = name;
this.students = students;
String instituteName;
private List<Department> departments;
students = dept.getStudents();
for(Student s : students)
{
noOfStudents++;
}
}
return noOfStudents;
}
// main method
class GFG
{
public static void main (String[] args)
{
Student s1 = new Student("Mia", 1, "CSE");
Student s2 = new Student("Priya", 2, "CSE");
Student s3 = new Student("John", 1, "EE");
Student s4 = new Student("Rahul", 2, "EE");
// making a List of
// CSE Students.
List <Student> cse_students = new ArrayList<Student>();
cse_students.add(s1);
cse_students.add(s2);
// making a List of
// EE Students
List <Student> ee_students = new ArrayList<Student>();
ee_students.add(s3);
ee_students.add(s4);
}
}
Output:
In this example, there is an Institute which has no. of departments like CSE, EE. Every
department has no. of students. So, we make a Institute class which has a reference to Object or
no. of Objects (i.e. List of Objects) of the Department class. That means Institute class is
associated with Department class through its Object(s). And Department class has also a
reference to Object or Objects (i.e. List of Objects) of Student class means it is associated with
Student class through its Object(s). It represents a Has-A relationship.
6.3.4 Composition
Composition is a restricted form of Aggregation in which two entities are highly dependent on
each other. It represents part-of relationship. In composition, both the entities are dependent on
each other. When there is a composition between two entities, the composed object cannot exist
without the other entity.
import java.io.*;
import java.util.*;
// class book
class Book
{
this.title = title;
this.author = author;
}
}
return books;
}
// main class
class GFG
{
public static void main (String[] args)
{
Output
In above example a library can have no. of books on same or different subjects. So, If Library
gets destroyed then All books within that particular library will be destroyed. i.e. book can not
exist without library. That’s why it is composition.
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\dataserver\assignments$\OOP\Lab123
Practice Task 1 [Expected Time = 80 mins]
Consider six classes i.e. Person, Professor, Researcher, Department, Laboratory, and
University having following specifications.
Draw UML diagram for each class and show inheritance, aggregation, and composition
relationship between these classes.
Implement all these classes while illustrating the concept of aggregation and composition in
terms of ownership and life-cycle.
After completing this lab, student will be able to implement the concepts of association,
aggregation, and composition in terms of ownership and life cycle of associated objects.
7.2 Testing
This section provides you the test cases to test the working of your program. If you get the
desired mentioned outputs for the given set of inputs then your program is right.
For Practice Task 1, Lab instructor must verify the implementation and required relationships
among all classes and objects. In addition, it is required to confirm that code has clearly
elaborated the concept of aggregation and composition in terms of ownership and life cycle.
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
10.1 Books
Text Book:
Java: How to Program by Paul J. Deitel, Harvey M. Deitel. Eighth Edition
Java Beginners Guide:
http://www.oracle.com/events/global/en/java-outreach/resources/java-a-beginners-
guide-1720064.pdf
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\