07-11-2012, 01:45 PM
Data Abstraction and Object Orientation
1Data Abstraction.ppt (Size: 216 KB / Downloads: 159)
Encapsulation
Encapsulation allows the programmer to group data and the subroutines that operate on them together in one place, and to hide irrelevant details from the user.
Information Hiding
Making objects and algorithms invisible to portions of the system that do not need them.
Derived class can restrict visibility
Private
Protected and public members of base class are private in derived class.
Protected
Protected and public members of base class are protected in derived class.
Public
Protected and public members of base class are protected and public in derived class.
Private members of base class aren’t visible in derived class.
Choosing a Constructor
Object-Oriented Languages allow classes to have zero, one or more different constructors.
Two ways to distinguish between constructors
Different Names
Different Number and Types of Arguements
References and Values
C++ vs. Java
Java uses reference, C++ you can specify
Reference
Every object is created explicitly so it is easy to make sure the correct constructor is called.
More elegant, but requires allocation from heap and extra indirections on every access of the object.
Value
More efficient but harder to control initialization
Destructors and 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
Many languages provide automatic garbage collection
Java, Smalltalk, Eiffel, etc.
Polymorphism
A derived class (D) has all the members of its base class ©
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.
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: vtable
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…
Mix-in Inheritance
Only one base class can contain method definitions
The other base class(es) contain only abstract methods
Only type of MI supported in Java, but not necessarily MI
Traditional Java inheritance uses keyword extends
Mix-in (interface) inheritance in Java uses keyword implements
Done via interfaces