Download presentation
Presentation is loading. Please wait.
Published byAlexis Norton Modified over 8 years ago
1
Chapter 12: Support for Object- Oriented Programming Lecture # 18
2
Chapter 12 Topics Object-Oriented Programming Design Issues for OOP Languages Support for OOP in C# Implementation of Object-Oriented Constructs Chapter 12: Support for Object-Oriented Programming 2
3
Object-Oriented Programming Abstract data types. Inheritance. Inheritance is the central theme in OOP and languages that support it. Polymorphism. Chapter 12: Support for Object-Oriented Programming 3
4
Inheritance Inheritance allows new classes defined in terms of existing ones, i.e., by allowing them to inherit common parts. Inheritance addresses reuse ADTs after minor changes and define classes in a hierarchy. Chapter 12: Support for Object-Oriented Programming 4
5
Object-Oriented Concepts (cont.) Inheritance can be complicated by access controls to encapsulated entities. A class can hide entities from its subclasses. A class can hide entities from its clients. Besides inheriting methods as is, a class can modify an inherited method. The new one overrides the inherited one. The method in the parent is overridden. Chapter 12: Support for Object-Oriented Programming 5
6
Single vs. Multiple Inheritance. One disadvantage of inheritance for reuse: Creates inter-dependencies among classes that complicate maintenance. Chapter 12: Support for Object-Oriented Programming 6 Object-Oriented Concepts (cont.)
7
Design Issues for OOP Languages Single and Multiple Inheritance. Object Allocation and DeAllocation. Dynamic and Static Binding. Nested Classes. Chapter 12: Support for Object-Oriented Programming 7
8
Support for OOP in C# General characteristics: Support for OOP similar to Java. Includes both classes and structs. Classes are similar to Java’s classes. structs are less powerful stack-dynamic constructs (e.g., no inheritance). Chapter 12: Support for Object-Oriented Programming 8
9
Support for OOP in C# (cont.) Inheritance Uses the syntax of C++ for defining classes. A method inherited from parent class can be replaced in the derived class by marking its definition with new. The parent class version can still be called explicitly with the prefix base: base.Draw(); Chapter 12: Support for Object-Oriented Programming 9
10
Support for OOP in C# (cont.) Dynamic binding: To allow dynamic binding of method calls to methods: The base class method is marked virtual. The corresponding methods in derived classes are marked override. Abstract methods are marked abstract and must be implemented in all subclasses. All C# classes are ultimately derived from a single root class, Object. Chapter 12: Support for Object-Oriented Programming 10
11
Support for OOP in C# (cont.) //Program that introduces virtual method in C# using System; class A { public virtual void Test() { Console.WriteLine("A.Test"); } } class B : A { public override void Test() { Console.WriteLine("B.Test"); } } class Program { static void Main() { A ref1 = new A(); ref1.Test(); A ref2 = new B(); ref2.Test(); } } //Output: A.Test B.Test Chapter 12: Support for Object-Oriented Programming 11
12
Implementing OO Constructs Two interesting and challenging parts: Storage structures for instance variables. Dynamic binding of messages to methods. Chapter 12: Support for Object-Oriented Programming 12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.