0% found this document useful (0 votes)
9 views8 pages

303 Note 2

The document outlines the key attributes of Object-Oriented Programming (OOP), including inheritance, polymorphism, encapsulation, and abstraction. It explains how these concepts help in reducing redundancy, enhancing code reusability, and hiding implementation details. Additionally, it describes the structure of classes and objects, emphasizing their roles in modeling real-world entities.

Uploaded by

nessa04nj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

303 Note 2

The document outlines the key attributes of Object-Oriented Programming (OOP), including inheritance, polymorphism, encapsulation, and abstraction. It explains how these concepts help in reducing redundancy, enhancing code reusability, and hiding implementation details. Additionally, it describes the structure of classes and objects, emphasizing their roles in modeling real-world entities.

Uploaded by

nessa04nj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ATTRIBUTES OF OOP

OOP has some important attributes that distinguishes it from other


programming paradigms. These attributes include:

1 Inheritance: Otherwise referred to as Hierarchy of Objects De>nitions,


supports the building of an hierarchy of object or class de>nitions in a way that
avoids duplication which in turn reduces redundancies. The classes in the lower
hierarchies inherit all the variables (static attributes) and methods (dynamic
behaviors) from the higher hierarchies. A class in the lower hierarchy is called a
subclass, or derived class, or child class or extended class. A class in the upper
hierarchy is called a super class or base class, or parent class. By pulling out all
the common variables and methods into the super classes, and leaving only the
specialized variables and methods in the sub classes, redundancy is greatly
reduced or eliminated because all the common variables and methods are not
duplicated or repeated in the sub classes.

A subclass inherits all the variables and methods from its super class/es,
including its immediate parent as well as all the ancestors. It is important to
note that a subclass is not a "subset" of a super class. In contrast, a subclass is a
"superset" of a superclass. This is because a subclass inherits all the variables
and methods of the super class and in addition, it extends the super class by
providing itself with more variables and methods. The Soccer Player example is
used to illustrate this concept.

46
Types of Inheritance: There are basically two types of inheritance structure in
OOP. These are single or simple inheritance and multiple or complex inheritance.
Multiple inheritance permits a subclass to have more than one direct super
class/es. The C++ programming language supports this type of multiple
inheritance. This has a serious drawback if the super classes have conOicting
implementation for the same method. In Java, each subclass can have one and
only one direct superclass, that is, single inheritance. On the other hand, a
superclass can have many subclasses.

Java adopts a so-called common-root approach. All Java classes are derived from
a common root class called java.lang.Object. This Object class de>nes and
implements the common behaviors that are required of all the Java objects
running under the JRE. These common behaviors enable the implementation of
features such as multi-threading and garbage collector.

For example, let us attempt to model a course system involving students and
teachers.

First, we can de>ne a super class called Person to store common properties
such as name and address, and then, we can create subclasses Student and
Teacher for their speci>c properties. For students, we need the records of the
courses taken, the respective grades, functionality to add a course and, input
grade, print all courses taken, and the average grade. We assume that a student
is entitled to takes only 30units of courses for the session. And for teachers, we
need to record the courses currently being taken, we have to be able to add or
remove a course already assigned, and no teacher is allowed to take courses in
excess of 5 courses concurrently within the session. The class diagram below is
used to model the required solution to this course system example:

46
2 Polymorphism: The word "polymorphism" means "many forms". It comes
from Greek word "poly" (meaning many) and "morphos" (meaning form).
Ordinarily in computer programs, polymorphism means di[erent routines or
methods with the same name. However, as a powerful attribute of OOP,
polymorphism helps to separate the software interface and implementation so as
to allow the programmer to program at the interface in the design of a complex
system.

Let us illustrate this attribute of polymorphism with a monster game application


with many types of monsters that can attack. We can design a superclass called
Monster and de>ne the method attack()in the superclass. The subclasses can
then provide their actual implementation. In the main program, we declare
instances of superclass, substituted with actual subclass; and invoke method
de>ned in the superclass. The >gure below depicts this illustration.

46
Summary of Polymorphism

1. A subclass instance possesses all the attributes operations of its


superclass. When a superclass instance is expected, it can be substituted by a
subclass instance. In other words, a reference to a class may hold an instance
of that class or an instance of one of its subclasses - it is called substitutability.
2. If a subclass instance is assigned to a superclass reference, you can invoke
the methods de>ned in the superclass only. You cannot invoke methods
de>ned in the subclass.

3. However, the substituted instance retains its own identity in terms of


overridden methods and hiding variables. If the subclass overrides methods in
the superclass, the subclass's version will be executed, instead of the
superclass's version.

3 Encapsulation: Encapsulation is also known as data hiding. In encapsulation


the variables and methods of a class are hidden from other classes, and can be
accessed only through the methods of the particular class. In the Java
programming language, this is accomplished through the use of access control
modi>er declarations. That is, an access control modi>er is used to control the
visibility of a class, or a member variable or a member method within a class.
For example, in the following access control modi>ers below:
1. Public: The class/variable (>eld)/method is accessible and available to all the
other objects in the system.

46
2. Private: The class/variable (>eld)/method is accessible and available within
this class only

In the >rst case that was declared as public, the class itself, its >elds and
methods can be accessed by other classes whereas, in the second case that was
declared private, the variables and methods of this class will only be accessible
to the class. They will be inaccessible to other classes.

The importance of this attribute in the OOP paradigm is that once a class is
de>ned, you can seal up the "box" and put the "box" on the shelve for others to
use and reuse. Anyone can pick up the "box" and use it in their application. This
cannot be done in the traditional procedural-oriented language like C, as the
static attributes (or variables) are scattered over the entire program and header
>les. You cannot "cut" out a portion of a non-object oriented program, and
append it to another program and expect the program to run without extensive
code modi>cation.

Encapsulation follows the principle of information hiding in OOP. That is, objects
communicate with one another using well-de>ned interfaces (public methods).
Objects are not allowed to know the implementation details of other objects. The
implementation details are hidden or encapsulated within the class. Information
hiding facilitates reuse of the class. The >gure below depicts this scenario.

Bene>ts of Encapsulation:

46
 The >elds of a class can be made read-only or write-only.
 A class can have total control over what is stored in its >elds.

 The users of a class do not know how the class stores its data. A class can
change the data type of a >eld and users of the class do not need to change
any of their code.

4 Abstraction: Abstraction is the act of hiding the implementation details of a


system from the users, leaving them only with the functionality or functionalities
provided by that system. In other words users will have the information on what the
object does and not how it does it.

The concept of abstraction in the context of OOP allows the software


developer to focus on the object by providing a generalized view of the
required classes or object as well as any relevant information.

Types of Abstraction: There are two basic types of abstraction. These are data and
control abstraction.

In Java, abstraction is achieved by using Abstract classes and Interfaces. A class


which contains the abstract keyword in its declaration is known as an abstract
class.

THE CONCEPT CLASS IN OOP

The class is the unit of programs in OOP. In Java, a class is a de>nition of objects of
the same kind. In other words, a class is a blueprint, template, or prototype that
de>nes and describes the static attributes and dynamic behaviors common to all
objects belonging to that class.

An instance is a realization of a particular item of a class. In other words, an


instance is an instantiation of a class. All the instances of a class have similar
properties, as described in the class de>nition. For example, you can de>ne a class
called "Student" and create three instances of the class "Student" for "Peter", "Paul"
and "Pauline".

46
The term "object" usually refers to instance. But it is often used loosely, and may
refer to a class or an instance.

Class is a 3-Compartment Box Encapsulating Data and Operations

A class can be visualized as a three-compartment box, comprising of the


following

1. Name (or identity): identi>es the class.

2. Variables (or attribute, state, >eld): contains the static attributes of the class.

3. Methods (or behaviors, function, operation): contains the dynamic behaviors


of the class.

In other words, a class encapsulates the static attributes (data) and dynamic
behaviors (operations that operate on the data) in a box. And going back to our
soccer example, the various objects can be represented as follows:

And to further illustrate how the concept of class works, the following instance
familiar to us is used to show two instances of the class Student, identi>ed as
"paul" and "peter".

46
A class is represented as a 3-compartment box, containing name, variables, and
methods, respectively. Class name is shown in bold and centralized. An instance
is also represented as a 3-compartment box, with instance name shown as
instanceName:Classname and underlined.

Brief Summary

1. A class is a programmer-de>ned, abstract, self-contained, reusable


software entity that mimics a real-world thing.
2. A class is a 3-compartment box containing the name, variables and the
methods.

3. A class encapsulates the data structures (in variables) and algorithms (in
methods). The values of the variables constitute its state. The methods
constitute its behaviors.

4. An instance is an instantiation (or realization) of a particular item of a


class.

The Concept Object in OOP


An object is a speci>c, concrete instance of a class. It is a self-contained block of
program code that carries out some action, similar to a procedure in a
procedural program. In object oriented programming, classes, attributes and
methods are encapsulated into objects that are then used much like real-world
objects.

46

You might also like