SlideShare a Scribd company logo
1 of 43
1
Chapter 10: Data Abstraction and Object
Orientation
Aaron Bloomfield
CS 415
Fall 2005
2
Fundamental OO Concepts
• Encapsulation
• Inheritance
• Dynamic Method Binding
3
Encapsulation
• 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.
4
Modules
• 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
…
end tuple
…
• What can the code outside the Database module do?
5
Module Changing
• Body is Changed
• Private Part of Header is Changed
• Public Part of Header is Changed
6
Classes can limit visibility
• Private
• Protected
• Public
• Package (in some languages, e.g. Java)
7
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.
8
Initialization and Finalization
9
Four Important Issues
• Choosing a Constructor
• References and Values
• Execution Order
• Garbage Collection
– We’ve seen that already
10
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
11
Constructors
• Eiffel code:
• class COMPLEX
creation
new_cartesian, new_polar
…
new_cartesian(x_val, y_va; : REAL) is
…
new_polar(rho, theta : REAL) is
…
• class mydata {
public:
mydata(string data);
mydata(int data);
mydata();
12
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
13
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
14
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.
15
Java’s finalize() method
• 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
16
Dynamic Method Binding
17
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.
18
Polymorphism Example
class person { …
class student : public person { …
class professor : public person { …
student s;
professor p;
…
person *x = &s;
person *y = &p;
19
Dynamic vs. Static binding
• Static method binding uses the type of the reference:
s.print_mailing_label();
p.print_mailing_label();
• Dynamic method binding uses the class of the object
that is referred/pointed to:
x->print_mailing_label();
y->print_mailing_label();
20
Which one does Java use?
public class Foo {
public String toString() {
return "Foo's toString()";
}
public static void main (String args[]) {
Object bar = new Foo();
System.out.println (bar);
}
}
• Java uses dynamic binding
21
Dynamic method binding
• Dynamic method binding: calls to virtual methods are
dispatched to the appropriate implementation at run time based
on the class of the object
– Simula: virtual methods listed at beginning of class
declaration
CLASS Person;
VIRTUAL: PROCEDURE PrintMailingLabel;
BEGIN
…
END Person;
22
Dynamic method binding
– C++: keyword “virtual” prefixes function declaration
class person {
public:
virtual void print_mailing_label ();
…
}
• This requires keeping a virtual method table
along with each object
– More on this in a bit…
23
Abstract Methods
• Bodyless virtual methods
In C++: called pure virtual method, created by following a
procedure declaration with an assignment to zero.
class person {
…
public:
virtual void print_mailing_label() = 0;
24
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.
25
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…
26
Member Lookup- vtable
27
Single Inheritance
28
Multiple Inheritance
29
Multiple Inheritance
• Derived class with two or more base classes
• E.g. - Student class
• C++:
class student : public person, public gp_list_node
{ … }
30
Multiple Inheritance
• Supported in C++, Eiffel, CLOS
• Single Inheritance only in Simula, Smalltalk, Modula-
3, Ada 95 & Oberon
• Java provides limited support – more on this later
31
Why use MI?
• Involves a number of tradeoffs
– Complexity vs. Simplicity
– Efficiency vs. Scalability
• How do you decide?
– Does it satisfy the “is a” relationship?
– Is object creation speed a constraint?
32
Multiple inheritance types
• Normal (non-repeated)
• Repeated
• Shared
• Mix-in
33
Normal (non-repeated) MI
• Recall “views” of objects
– data members
– vtables
• Compile-time constant offset d
34
35
Efficiency (or lack thereof)
• May have to determine view dynamically
• Results in less time-efficient code
• An example implementation may have:
– 3 extra cycles, 1 extra memory access over single inheritance
– 5 extra cycles, 3 extra memory accesses over static methods
36
Semantic Ambiguities
• What if two base classes have implementations
of a shared method?
– Won’t work in Eiffel or C++
– In other languages, you must call methods explicitly,
i.e. class::method()
37
Semantic Ambiguities
• What if the relationship below occurs?
• This is repeated multiple inheritance
– As one of the ancestors is repeated in the parent class of one
of the descendents
gp_list_node person
student
gp_list_node
student_prof
professor
38
Replicated Multiple Inheritance
• Default in C++
• Ex. gp_list_node
• Can only directly access one level deep
– To access a student view of gp_list_node, you must first
assign a student_prof pointer into a student or professor
pointer
39
40
Shared Multiple Inheritance
• Default in Eiffel
• Ex. Person
• Still have problem when inheriting overridden
methods
41
42
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
43
Java Interfaces
• public class String extends Object implements
Serializable, CharSequence, Comparable;
• Java interfaces can contain definition prototypes and
static variables

More Related Content

Similar to 10 - Encapsulation(object oriented programming)- java . ppt

Similar to 10 - Encapsulation(object oriented programming)- java . ppt (20)

Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Inheritance
InheritanceInheritance
Inheritance
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
3java Advanced Oop
3java Advanced Oop3java Advanced Oop
3java Advanced Oop
 
Ch03
Ch03Ch03
Ch03
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classes
 
Lecture d-inheritance
Lecture d-inheritanceLecture d-inheritance
Lecture d-inheritance
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Inheritance, Polymorphism, and Virtual Functions.pptx
Inheritance, Polymorphism, and Virtual Functions.pptxInheritance, Polymorphism, and Virtual Functions.pptx
Inheritance, Polymorphism, and Virtual Functions.pptx
 
L1
L1L1
L1
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
C++ Basics
C++ BasicsC++ Basics
C++ Basics
 
CS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptxCS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptx
 

Recently uploaded

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Recently uploaded (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 

10 - Encapsulation(object oriented programming)- java . ppt

  • 1. 1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005
  • 2. 2 Fundamental OO Concepts • Encapsulation • Inheritance • Dynamic Method Binding
  • 3. 3 Encapsulation • 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.
  • 4. 4 Modules • 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 … end tuple … • What can the code outside the Database module do?
  • 5. 5 Module Changing • Body is Changed • Private Part of Header is Changed • Public Part of Header is Changed
  • 6. 6 Classes can limit visibility • Private • Protected • Public • Package (in some languages, e.g. Java)
  • 7. 7 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.
  • 9. 9 Four Important Issues • Choosing a Constructor • References and Values • Execution Order • Garbage Collection – We’ve seen that already
  • 10. 10 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
  • 11. 11 Constructors • Eiffel code: • class COMPLEX creation new_cartesian, new_polar … new_cartesian(x_val, y_va; : REAL) is … new_polar(rho, theta : REAL) is … • class mydata { public: mydata(string data); mydata(int data); mydata();
  • 12. 12 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
  • 13. 13 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
  • 14. 14 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.
  • 15. 15 Java’s finalize() method • 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
  • 17. 17 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.
  • 18. 18 Polymorphism Example class person { … class student : public person { … class professor : public person { … student s; professor p; … person *x = &s; person *y = &p;
  • 19. 19 Dynamic vs. Static binding • Static method binding uses the type of the reference: s.print_mailing_label(); p.print_mailing_label(); • Dynamic method binding uses the class of the object that is referred/pointed to: x->print_mailing_label(); y->print_mailing_label();
  • 20. 20 Which one does Java use? public class Foo { public String toString() { return "Foo's toString()"; } public static void main (String args[]) { Object bar = new Foo(); System.out.println (bar); } } • Java uses dynamic binding
  • 21. 21 Dynamic method binding • Dynamic method binding: calls to virtual methods are dispatched to the appropriate implementation at run time based on the class of the object – Simula: virtual methods listed at beginning of class declaration CLASS Person; VIRTUAL: PROCEDURE PrintMailingLabel; BEGIN … END Person;
  • 22. 22 Dynamic method binding – C++: keyword “virtual” prefixes function declaration class person { public: virtual void print_mailing_label (); … } • This requires keeping a virtual method table along with each object – More on this in a bit…
  • 23. 23 Abstract Methods • Bodyless virtual methods In C++: called pure virtual method, created by following a procedure declaration with an assignment to zero. class person { … public: virtual void print_mailing_label() = 0;
  • 24. 24 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.
  • 25. 25 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…
  • 29. 29 Multiple Inheritance • Derived class with two or more base classes • E.g. - Student class • C++: class student : public person, public gp_list_node { … }
  • 30. 30 Multiple Inheritance • Supported in C++, Eiffel, CLOS • Single Inheritance only in Simula, Smalltalk, Modula- 3, Ada 95 & Oberon • Java provides limited support – more on this later
  • 31. 31 Why use MI? • Involves a number of tradeoffs – Complexity vs. Simplicity – Efficiency vs. Scalability • How do you decide? – Does it satisfy the “is a” relationship? – Is object creation speed a constraint?
  • 32. 32 Multiple inheritance types • Normal (non-repeated) • Repeated • Shared • Mix-in
  • 33. 33 Normal (non-repeated) MI • Recall “views” of objects – data members – vtables • Compile-time constant offset d
  • 34. 34
  • 35. 35 Efficiency (or lack thereof) • May have to determine view dynamically • Results in less time-efficient code • An example implementation may have: – 3 extra cycles, 1 extra memory access over single inheritance – 5 extra cycles, 3 extra memory accesses over static methods
  • 36. 36 Semantic Ambiguities • What if two base classes have implementations of a shared method? – Won’t work in Eiffel or C++ – In other languages, you must call methods explicitly, i.e. class::method()
  • 37. 37 Semantic Ambiguities • What if the relationship below occurs? • This is repeated multiple inheritance – As one of the ancestors is repeated in the parent class of one of the descendents gp_list_node person student gp_list_node student_prof professor
  • 38. 38 Replicated Multiple Inheritance • Default in C++ • Ex. gp_list_node • Can only directly access one level deep – To access a student view of gp_list_node, you must first assign a student_prof pointer into a student or professor pointer
  • 39. 39
  • 40. 40 Shared Multiple Inheritance • Default in Eiffel • Ex. Person • Still have problem when inheriting overridden methods
  • 41. 41
  • 42. 42 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
  • 43. 43 Java Interfaces • public class String extends Object implements Serializable, CharSequence, Comparable; • Java interfaces can contain definition prototypes and static variables