- Complex objects are built from simpler objects like integers, strings, Booleans using constructors like tuples, sets, lists, and arrays. Sets, tuples, and lists are especially important constructors.
- Encapsulation packages code and data together in an object. It distinguishes between interface and implementation and promotes modularity. Encapsulation is based on the concept of abstract data types.
- Inheritance allows a class to inherit attributes and methods from a superclass, extending and specializing the superclass. Identical parts are defined only once to promote reuse. Some systems support multiple inheritance. Inheritance can cause problems if attributes are redefined which inheritance resolution must address.
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Object Constructors and Encapsulation in OOP
1. Prof. Neeraj Bhargava
Pooja Dixit
Department of Computer Science
School of Engineering & System Science
MDS, University Ajmer, Rajasthan, India
1
2. Complex objects are built from simpler ones by applying constructors
to them.
The simplest objects are objects such as integers, characters, byte
strings of any length, Booleans and floats (one might add other
atomic types).
There are various complex object constructors: tuples, sets, bags,
lists, and arrays are examples. The minimal set of constructors that
the system should have are set, list and tuple.
Sets are critical because they are a natural way of representing
collections from the real world.
Tuples are critical because they are a natural way of representing
properties of an entity. Of course, both sets and tuples are important
because they gained wide acceptance as object constructors through
the relational model.
Lists or arrays are important because they capture order, which
occurs in the real world, and they also arise in many scientific
applications, where people need matrices or time series data.
2
3. Encapsulation is the object model concept of including processing or
behavior with the object instances defined by the class.
Encapsulation allows code and data to be packaged together.
Encapsulation is a basic concept for all object-oriented technologies.
It was created for making a clear distinction between the specification
and the implementation of an operation and in this way for enabling
modularity.
Encapsulation is derived from the notion of Abstract Data Type (ADT).
It is motivated by the need to make a clear distinction between the
specification and the implementation of an operation. It reinforces
modularity and provides a form of logical data independence.
3
4. A superclass itself can also have a superclass. And this class
in turn can have another superclass. Given this, object-
oriented database systems build a hierarchy of their classes.
In this example, a Student is a type of
Person. Likewise, a Employee is a
type of Person. Both Student and
Employee inherit all the attributes
and methods of Person. Student has a
locally defined student ID attribute.
Employee has a locally defined
employee ID attribute. So, if you
would look at a Student object, you
would see attributes of name, date of
birth, parents, children, and student
ID.
4
5. Inheritance makes it possible to define a class as a subclass of an
already existing one (superclass).
The subclass inherits all attributes and methods from the superclass and
can additionally define its own attributes and methods.
This concept is an important mechanism for supporting reusability.
Identical parts of the structure of two different classes may be defined
only once in a common superclass. Is this way, less code has to be
written.
There are some systems that allow a class to be subclass of more than
one superclass. This feature is called multiple inheritance (in contrast to
single inheritance).
There are certain problems that can arise due to inheritance. And these
problems are even more likely to arise if multiple inheritance is used.
For example, a subclass may define an attribute with the same name as
an attribute already defined by a superclass.
The problem gets worse when two superclasses of the same subclass
define attributes and methods with the same name. Each system must
come of with a mechanism to resolve this problems.
5