Sie sind auf Seite 1von 9

Object Oriented Software Engineering BSE-IV

2
Object Oriented Design Using Java
Object Oriented Software Engineering BSE-IV

Lab 2: The object-oriented paradigm


Objectives
 Understanding the object oriented concepts
• especially the concepts of object-oriented, inheritance and
polymorphism

1. Outline
 Introduction to object oriented paradigm
 Inheritance and polymorphism

2. Background

Question:

What is the object oriented way of getting rich?

Object-oriented analysis, design, and programming are proven powerful means to master
the complexity inherent in the development process of large, distributed applications.
The object-oriented conceptual framework, called the object model, is based on the
combination of the principles of abstraction, encapsulation, modularity, and hierarchy, often
supplemented by the principles of typing, concurrency, and persistence.
According to Booch, the usage of the object model for the design process of an application
offers several advantages over traditional techniques:

 The application of the object model helps substantially to develop complex


programs.
 Only object-oriented analysis and design permit to fully exploit the expressive power
of modern object-oriented programming languages.
 The object model facilitates the reuse of both code and designs.
 Systems designed according to object-oriented principles can evolve over time and
can be adapted easily to accommodate future demands, without necessitating the
abandon or complete restructuring of the original design.
 Object orientation reduces the risks of software development.
 The object model resembles human cognition more closely than traditional design
paradigms.
The object-oriented paradigm---especially the concepts of object-oriented decomposition,
inheritance, specialization, and polymorphism---are particularly well suited for multimedia
application programming because:

1. object-oriented decomposition leads to meaningful abstractions that allow to


construct and handle composed media objects conveniently,
2. polymorphism and inheritance hide specific details of particular hardware devices
from the rest of the program,
3. specialisation offers the flexibility necessary for adding support for new media
types and hardware devices easily.

3 OOAD - Object Model


2
Object Oriented Software Engineering BSE-IV

The object model visualizes the elements in a software application in terms of objects.
3.1 Objects and Classes
The concepts of objects and classes are intrinsically linked with each other and form the
foundation of object–oriented paradigm.

Object
An object is a real-world element in an object–oriented environment that may have a physical
or a conceptual existence. Each object has −
 Identity that distinguishes it from other objects in the system.
 State that determines the characteristic properties of an object as well as the values of
the properties that the object holds.
 Behavior that represents externally visible activities performed by an object in terms of
changes in its state.
Objects can be modelled according to the needs of the application. An object may have a physical
existence, like a customer, a car, etc.; or an intangible conceptual existence, like a project, a
process, etc.

Class
A class represents a collection of objects having same characteristic properties that exhibit
common behavior. It gives the blueprint or description of the objects that can be created from it.
Creation of an object as a member of a class is called instantiation. Thus, object is an instance of
a class.
The constituents of a class are −
 A set of attributes for the objects that are to be instantiated from the class. Generally,
different objects of a class have some difference in the values of the attributes. Attributes
are often referred as class data.
 A set of operations that portray the behavior of the objects of the class. Operations are
also referred as functions or methods.
Example
Let us consider a simple class, Circle, that represents the geometrical figure circle in a two–
dimensional space. The attributes of this class can be identified as follows −

 x–coord, to denote x–coordinate of the center


 y–coord, to denote y–coordinate of the center
 a, to denote the radius of the circle
Some of its operations can be defined as follows −

 findArea(), method to calculate area


 findCircumference(), method to calculate circumference
 scale(), method to increase or decrease the radius
During instantiation, values are assigned for at least some of the attributes. If we create an object
my_circle, we can assign values like x-coord : 2, y-coord : 3, and a : 4 to depict its state. Now, if
the operation scale() is performed on my_circle with a scaling factor of 2, the value of the variable
a will become 8. This operation brings a change in the state of my_circle, i.e., the object has
exhibited certain behavior.
3
Object Oriented Software Engineering BSE-IV

3.2 Inheritance
Inheritance is the mechanism that permits new classes to be created out of existing classes by
extending and refining its capabilities. The existing classes are called the base classes/parent
classes/super-classes, and the new classes are called the derived classes/child
classes/subclasses. The subclass can inherit or derive the attributes and methods of the super-
class(es) provided that the super-class allows so. Besides, the subclass may add its own
attributes and methods and may modify any of the super-class methods. Inheritance defines an
“is – a” relationship.
Example
From a class Mammal, a number of classes can be derived such as Human, Cat, Dog, Cow, etc.
Humans, cats, dogs, and cows all have the distinct characteristics of mammals. In addition, each
has its own particular characteristics. It can be said that a cow “is – a” mammal.
The following figure depicts the examples of different types of inheritance.

3.3 Polymorphism
Polymorphism is originally a Greek word that means the ability to take multiple forms. In object-
oriented paradigm, polymorphism implies using operations in different ways, depending upon
the instance they are operating upon. Polymorphism allows objects with different internal
structures to have a common external interface. Polymorphism is particularly effective while
implementing inheritance.
Example
Let us consider two classes, Circle and Square, each with a method findArea(). Though the name
and purpose of the methods in the classes are same, the internal implementation, i.e., the
procedure of calculating area is different for each class. When an object of class Circle invokes
its findArea() method, the operation finds the area of the circle without any conflict with the
findArea() method of the Square class.
3. Tools

Eclipse

4
4. In-Class Example
Object Oriented Software Engineering BSE-IV

JAVA- Inheritance and Polymorphism

Inheritance can be defined as the process where one class acquires the properties
methodsandfields of another. With the use of inheritance the information is made manageable in
a hierarchical order. The class which inherits the properties of other is known as subclass
derivedclass, childclass and the class whose properties are inherited is known as superclass
baseclass, parentclass.

extends Keyword--- extends is the keyword used to inherit the properties of a class. Below given
is the syntax of extends keyword.
class Super{
.....
.....
}
class Sub extends Super{
.....
.....
}

Sample Code

Below given is an example demonstrating Java inheritance. In this example you can observe two
classes namely Calculation and My_Calculation.

Using extends keyword the My_Calculation inherits the methods addition and
Subtraction of Calculation class.
Copy and paste the program given belowin a file with name My_Calculation.java
class Calculation{ int z;
public void addition(int x, int y){
z=x+y;
System.out.println("The sum of the given numbers:"+z);
}
public void Substraction(int x,int y){ z=x-y;
System.out.println("The difference between the given numbers:"+z);
}

}
public class My_Calculation extends Calculation{ public void multiplication(int x, int
y){
z=x* y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]){ int a=20, b=10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Substraction(a, b);
demo.multiplication(a, b);
5
Object Oriented Software Engineering BSE-IV

}
}

Compile and execute the above code as shown below

javac My_Calculation.java java My_Calculation

After executing the program it will produce the following result.

The sum of the given num bers:30


The difference between the given numbers:10 The product of the given numbers:200

In the given program when an object to My_Calculation class is created, a copy of the contents of
the super class is made with in it. That is why, using the object of the subclass you can access the
members of a super class.

The Superclass reference variable can hold the subclass object, but using that variable you can
access only the members of the superclass, so to access the members of both classes it is
recommended to always create reference variable to the subclass.
If you consider the above program you can instantiate the class as given below as well. But using
the superclass reference variable ( cal in this case ) you cannot call the method multiplication,
which belongs to the subclass My_Calculation.

Calculation cal=new My_Calculation();


demo.addition(a,b);
demo.Subtraction(a, b);

Note: A subclass inherits all the members fields, methods, andnestedclasses from its superclass. 6
Object Oriented Software Engineering BSE-IV

Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass
can be invoked from the subclass.

Java - Polymorphism

Polymorphism means "many forms", and it occurs when we have many classes that are related
to each other by inheritance.
Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from
another class. Polymorphism uses those methods to perform different tasks. This allows us to
perform a single action in different ways.
For example, think of a superclass called Animal that has a method called animalSound().
Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own
implementation of an animal sound (the pig oinks, and the cat meows, etc.):Example
Let us look at an example.
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}

class Pig extends Animal {


public void animalSound() {
System.out.println("The pig says: wee wee");
}
}

class Dog extends Animal {


public void animalSound() {
System.out.println("The dog says: bow wow");
}
}

Now we can create Pig and Dog objects and call the animalSound() method on both of them:
Example
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}

class Pig extends Animal {


public void animalSound() {
System.out.println("The pig says: wee wee");
}
}

class Dog extends Animal {


public void animalSound() {
System.out.println("The dog says: bow wow");
}
}

class MyMainClass {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
7
myAnimal.animalSound();
Object Oriented Software Engineering BSE-IV

myPig.animalSound();
myDog.animalSound();
}
}

8
Object Oriented Software Engineering BSE-IV

LAB TASKS
Task 1:
A company pays its employees on a weekly basis. The employees are of four types:
Salaried employees are paid a fixed weekly salary regardless of the number of
hours worked, hourly employees are paid by the hour and receive overtime pay
for all hours worked in excess of 40 hours, commission employees are paid a
percentage of their sales and salaried-commission employees receive a base
salary plus a percentage of their sales. For the current pay period, the company
has decided to reward salaried-commission employees by adding 10% to their
base salaries. The company wants to implement a Java application that performs
its payroll calculations polymorphically.
Derive all four classes and calculate their salary accordingly

Das könnte Ihnen auch gefallen