Professional Documents
Culture Documents
Unit VII Data Abstraction and Object Orientation
Unit VII Data Abstraction and Object Orientation
UNIT VII
Abstraction Properties
www.saiadityait2010-14.blogspot.com Page 1
Unit VII Data Abstraction and Object Orientation
The abstraction provided by modules and module types has at least three important
benefits.
1. It reduces conceptual load by minimizing the amount of detail that the
programmer must think about at one time.
2. It provides fault containment by preventing the programmer from using a
program component in inappropriate ways, and by limiting the portion of a
program’s text in which a given component can be used, thereby limiting the
portion that must be considered when searching for the cause of a bug.
3. It provides a significant degree of independence among program components,
making it easier to assign their construction to separate individuals, to modify their
internal implementations without changing external code that uses them, or to
install them in a library where they can be used by other programs.
• By deriving new classes from old ones, the programmer can create
arbitrarily deep class hierarchies, with additional functionality at every level
of the tree.
When created with new, an object is allocated in the heap; when created via
elaboration of a declaration it is allocated statically or on the stack, depending on
lifetimecreation causes the invocation of a programmer-specified initialization
routine, known as a constructor. In C++ and its descendants, Java and C#, the
name of the constructor is the same as that of the class itself. C++ also allows the
programmer to specify a destructor method that will be invoked automatically
when an object is destroyed, either by explicit programmer action or by return
from the subroutine in which it was declared. The destructor’s name is also the
same as that of the class, but with a leading tilde (~).
The Access specifier for the OOPs are : Private, Public and Protected.
In Java,
Private:
– The most restrictive access level is private.
– A private member is accessible only to the class in which it is defined.
Inheritance does not apply on the private members. They are Just like
secrets.
– Use private keyword to create private members.
Protected:
www.saiadityait2010-14.blogspot.com Page 2
Unit VII Data Abstraction and Object Orientation
– Allows the class itself, subclasses, and all classes in the same package
to access the members.
– Use the protected access level when it's appropriate for a class's
subclasses to have access to the member, but not unrelated classes.
Protected members are like family secrets.
– To declare a protected member, use the keyword protected
Public:
– The easiest access specifier is public.
– Any class, in any package, has access to a class's public members.
– Declare public members only if such access cannot produce
undesirable results if an outsider uses them.
– There are no personal or family secrets here; this is for stuff you don't
mind anybody else knowing.
– To declare a public member, use the keyword public.
In C++
• Declared in the body of the class
• May be public or private
• Exist throughout the life of the object.
• Stored in class object.
• Each object has its own copy.
• May be objects of any type
Access-specifier private
• Makes any member accessible only to member functions of the class.
• May be applied to data members and member functions
• Default access for class members
• Encourages “information hiding”
• As a rule of thumb, data members should be declared private
• Member functions should be declared public.
• Except member functions that are accessed only by other member
functions of the class.
• Often useful to have get and set functions
• To access private members in controlled ways
www.saiadityait2010-14.blogspot.com Page 3
Unit VII Data Abstraction and Object Orientation
Tiny Subroutines
Derived Classes
– New classes created from existing ones is called inheritance..
Existing class is called Base class/parent class / Sub Class.
New class is called Derived/Child/Sub Class.
www.saiadityait2010-14.blogspot.com Page 4
Unit VII Data Abstraction and Object Orientation
In C++,
class Shape
{
public:
int GetColor ( ) ;
protected: // so derived classes can access it
int color;
};
class Two_D : public Shape
{
// put members specific to 2D shapes here
};
class Three_D : public Shape
{
// put members specific to 3D shapes here
};
Shape is the base class and two-D & Three-D are derived classes and can
access the method getColor().
In java,
we can create a general purpose element base class that contains only the
fields and methods needed to implement list operations:
www.saiadityait2010-14.blogspot.com Page 5
Unit VII Data Abstraction and Object Orientation
class gp_list_node {
gp_list_node* prev;
gp_list_node* next;
gp_list_node* head_node;
public:
gp_list_node(); // assume method bodies given separately
gp_list_node* predecessor();
gp_list_node* successor();
bool singleton();
void insert_before(gp_list_node* new_node);
void remove();
~gp_list_node();
};
Now we can use this general purpose class to derive lists and queues with
specific
types of fields:
www.saiadityait2010-14.blogspot.com Page 6
Unit VII Data Abstraction and Object Orientation
Overloaded Constructor
• A Class can have more than one constructor in a class, as long as each
has a different list of arguments.
C++,
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
public:
rectangle(float, float); // constructor
rectangle(); // another constructor
void draw(); // draw member function
void posn(int, int); // position member function
void move(int, int); // move member function
rectangle::rectangle()
{
height = 10;
width = 10;
xpos = 0;
www.saiadityait2010-14.blogspot.com Page 7
Unit VII Data Abstraction and Object Orientation
ypos = 0;
}
void main()
{
rectangle rc1(3.0, 2.0);
rectangle rc2();
rc1.draw();
rc2.draw();
In addition to defining new fields and methods, a derived class can hide or redefine
members of base class(es). To redefine a method of a base class, a derived class
simply declares a new version.
In C++,
gp_list_node::remove(); // C++
super.remove(); // Java
base.remove(); // C#
super remove. // Smalltalk
[super remove] // Objective-C _
In Eiffel, onemust explicitly renamemethods inherited froma base class, in order
to make them accessible:
class int_list_node
inherit
gp_list_node
rename
remove as old_remove
... -- other renames
end
www.saiadityait2010-14.blogspot.com Page 8
Unit VII Data Abstraction and Object Orientation
Containers/Collections
In object-oriented programming, an abstraction that holds a collection of objects
of some given class is often called a container. There are several different ways to
build containers.
Modules
Scope rules for data hiding were one of the principal innovations of Clu, Modula,
Euclid, and other module-based languages of the 1970s. In Clu and Euclid, the
declaration and definition (header and body) of a module always appear together.
The header clearly states which of the module’s names are to be exported. In Clu, a
module (called a cluster) implements a single abstract type. Assignment and
equality testing are permitted for that type, but because Clu uses a reference model
for variables, these operations copy or compare references to objects, not the
objects themselves.
• If a module M exports a type T, the rest of the program can only pass T to
subroutines exported from M.
– T is said to be an opaque type.
var Database : module
exports (tuple with (:=, name))
…
type tuple = record
var name : packed array 1..80 of char
www.saiadityait2010-14.blogspot.com Page 9
Unit VII Data Abstraction and Object Orientation
…
end tuple
The “this” Parameter
This replication would be highly wasteful, however, as the copies would vary only
in the details of address computations. A better technique is to create a single
instance of each module subroutine, and to pass that instance, at run time, the
address of the storage of the appropriate module instance.
www.saiadityait2010-14.blogspot.com Page 10
Unit VII Data Abstraction and Object Orientation
Choosing a Constructor
Smalltalk, Eiffel, C++, Java, and C# all allow the programmer to specify more than
one constructor for a given class. In C++, Java, and C#, the constructors behave
like overloaded subroutines: they must be distinguished by their numbers and types
of arguments. In Smalltalk and Eiffel, different constructors can have different
names; code that creates an object must name a constructor explicitly. The !!
operator is Eiffel’s equivalent of new.
www.saiadityait2010-14.blogspot.com Page 11
Unit VII Data Abstraction and Object Orientation
Execution Order
• If class B is derived from class A, A constructor is called before B
constructor
– To get arguments to the A constructor, you must use an intializer list
class foo : bar {
...
}
foo::foo (foo_params) : bar(bar_params) {
…
– The part after the colon is a call to bar’s constructor
the object’s class (call it B) is derived from some other class (call itA), C++ insists
on calling an A constructor before calling a B constructor, so the derived class is
guaranteed never to see its inherited fields in an inconsistent state.When the
programmer creates an object of class B (either via declaration or with a call to
new), the creation operation specifies arguments for a B constructor. constructor
when the surrounding object is created. Smalltalk, Eiffel, and CLOS are all more
lax than C++ regarding the initialization of base classes. The compiler or
interpreter arranges to call the constructor (creator, initializer) for each newly
created object automatically, but it does not arrange to call constructors for base
classes automatically; all it does is initialize base class data members to default (0
or null) values
Garbage Collection
• When an object is destroyed, the destructor is called for the derived class
first, then the destructors of the base classes are called.
– Reverse order of derivation
• Destructors purpose is to return allocated space back to the heap
www.saiadityait2010-14.blogspot.com Page 12
Unit VII Data Abstraction and Object Orientation
In C++,
• Destructor:– a function used to clean up an object of a class prior to deleting
that object
– Class name preceeded by '~'
– No parameters, no result
• Called automatically
– When function exits scope of automatic class object
– By delete operator
• In Java, you can override the finalize() method
• This allows code to be executed when the object is about to be deleted
– But you shouldn’t extend the object’s lifetime by doing this
– As the finalize() method is only called once per object
y->print_mailing_label();
Abstract Classes
• Class that contains one or more abstract methods
– Java: called an interface (which has only abstract methods)
• Generally not possible to declare object of an abstract class b/c it would be
missing at least one member
– But you can do so in C++
• Serves as a base for concrete classes.
– Concrete class must provide a definition for every abstract method it
inherits
Application to dynamic method binding: allows code that calls methods of
objects of a base class, assuming that the concrete methods will be invoked
at run time
Member Lookup
• In dynamic binding each object is represented with a record whose first field
contains the address of a virtual method table (vtable) for that object’s class
• Our objects are being more complicated for the compiler to manage
– Virtual method tables
– Reference counts
– Etc…
www.saiadityait2010-14.blogspot.com Page 14
Unit VII Data Abstraction and Object Orientation
• Polymorphism
A derived class (D) has all the members of its base class (C)
– Class D can be used anytime class C is expected.
– If class D does not hide any publicly visible members of C then D is a
subtype of C.
• If class D is used in place of class C, this is a form of polymorphism.
www.saiadityait2010-14.blogspot.com Page 15
Unit VII Data Abstraction and Object Orientation
• class person { …
• class student : public person { …
• class professor : public person { …
• student s;
• professor p;
• …
• person *x = &s;
• person *y = &p;
5) Multiple Inheritance
It means derived class to inherit features from more than one base class. For
Example College and Department are two base classes then the students is a
class which can use features of both the College and Department class.
College Department
Students
In C++, The multiple inheritance is implemented as
interface College
{ …….}
interface Department
{…………………}
• Here you get all the members of College and all the members of Department
www.saiadityait2010-14.blogspot.com Page 16
Unit VII Data Abstraction and Object Orientation
• Ada 95
– tagged types
– single inheritance
– no constructors or destructors
– class-wide parameters:
• methods static by default
• can define a parameter or pointer that grabs the object-specific
version of all methods
– base class doesn't have to decide what will be virtual
– notion of child packages as an alternative to friends
• Java
– interfaces, mix-in inheritance
– alternative to multiple inheritance
• basically you inherit from one real parent and one or more
interfaces, each of which contains only virtual functions and no
data
• this avoids the contiguity issues in multiple inheritance above,
allowing a very simple implementation
– all methods virtual
• Is C++ object-oriented?
– Uses all the right buzzwords
– Has (multiple) inheritance and generics (templates)
– Allows creation of user-defined classes that look just like built-in ones
– Has all the low-level C stuff to escape the paradigm
– Has friends
– Has static type checking
www.saiadityait2010-14.blogspot.com Page 18
Unit VII Data Abstraction and Object Orientation
RESOLVE
www.saiadityait2010-14.blogspot.com Page 19