Download presentation
Presentation is loading. Please wait.
Published byMoses Banks Modified over 8 years ago
1
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle
2
FEN 2014UCN Teknologi/act2learn2 OO-Principles -encapsulation Seen from the outside an object is an entity offering a number of services (public methods and properties). The implementation and data representation are hidden or encapsulated. –This makes it possible to change implementation without affecting other parts of the program. –Also it becomes possible to think, talk and use the object without knowing the implementation. –Hiding data behind methods and properties are called encapsulation or data abstraction. Encapsulation or data abstraction is one the fundamental principles of object-oriented programming.
3
FEN 2014UCN Teknologi/act2learn3 The Anatomy of a Class Classes are usually written by this pattern: class ClassName { declaration of attributes constructors properties methods }
4
FEN 2014UCN Teknologi/act2learn4 Remember this Exercise?..\modul1\demos\EmpProjectV1
5
FEN 2014UCN Teknologi/act2learn5 Inheritance in C# Every method and property is inherited – constructors are not. private members are inherited, but not directly accessible in the subclass. Every protected member of the base class is visible in subclasses, but hidden from other parts of the program. Members may be added in the subclass. Multiple inheritance is not supported (by classes). In C# every class inherits from Object.
6
FEN 2014UCN Teknologi/act2learn6 OO-Principles -inheritance Supports code-reuse. Existing classes may be extended through inheritance. Inheritance is to used as type specialisation, that is: we are modelling an “is-a” relationship between super- and subclass. For instance: CheckAccount is an Account, a special kind of account, but an account. So when we want to add specialised functionality to our system, inheritance may used by adding specialised classes. E.g.: CheckAccount may inherit Account.
7
FEN 2014UCN Teknologi/act2learn7 OO-Principles -inheritance Terminology: baseclass/superclass – subclass. Inheritance is to be used as specialisation/generalisation. Inheritance models an “is-a” relationship between super- and subclass. –A subclass is type compatible with the superclass: CheckAccount c = new CheckAccount(); if (c is Account) -- yields true, if CheckAccount inherits Account The “is-a” relation is transitive.
8
FEN 2014UCN Teknologi/act2learn8 Example: On Employee there is a method GiveBonus() which may have different implementations in the superclass and in the subclasses.
9
FEN 2014UCN Teknologi/act2learn9 Inheritance - Constructors The base-part of a subclass is initialised by the call base(param-liste) This call is executed as the first step in executing the constructor of the subclass :base(param-liste) is placed immediately after the method-heading of the constructor (C++ syntax) If you don’t provide a default constructor (a constructor with no parameters) a default constructor is generated by the compiler. This will be called implicitly when an object of the subclass is created.
10
FEN 2014UCN Teknologi/act2learn10 Inheritance - redefining methods A method inherited from the base-class may be redefined (“overridden”) in the sub-class. For instance the Withdraw-method on Account/CheckAccout. In C# the must be specified “virtual” in the base-class and “override” in sub-class. The method in the sub-class has the same signature (name and parameter list) and the same return type as the method in the base-class. In the subclass the redefined method in the base-class may be called using base.methodName();
11
FEN 2014UCN Teknologi/act2learn11 Inheritance - polymorphism All reference variables in C# may refer to objects of subtypes. In the case of virtual methods it is first at execution time it is determined which method exactly is to be called. The method called is the one defined on the object that the reference currently is referring to. This is called dynamic binding – the call is bound to an actual code segment at runtime (dynamically).
12
FEN 2014UCN Teknologi/act2learn12 Polymorphism/Dynamic Binding The way it used to be: Employee programmer = new Employee(“H. Acker","Programmer",22222); Static type = Dynamic type Static method call Static type Dynamic type Using polymorphism:: Employee boss = new Manager(”Big Boss",”CEO",52525, 500); Dynamic type must be the same or a sub-type of the static type. The compiler checks method-calls on the static type. Runtime the call is bonded to the dynamic type (dynamic binding). Dynamic binding requires methods to be specified virtual in the base-class and explicitly overridden in the sub-classes. Dynamic type Static type
13
FEN 2014UCN Teknologi/act2learn 13 Example Let’s look at the implementation of the model from earlier source Now: Employees at the cantina get double bonus.
14
FEN 2014UCN Teknologi/act2learn14 Exercise Banking2Exercises.mht
15
FEN 2014UCN Teknologi/act2learn15 Substitution Principle Whenever inheritance is used; it should always be possible to use objects of an sub-type (dynamic) instead of objects of the original (static) type. It should be possible to substitute objects of the subtype where objects of the base-type are expected. We can assure that this is meaningful by restricting the use of method-redefining by the following rule: –If pre-conditions are changed, they are only weakened –If post-conditions are changed, they are only strengthened
16
FEN 2014UCN Teknologi/act2learn16 Example: public class Shape{ private int x,y; private Colour colour; //other attributtes public void MoveTo(int newX, int newY){ //PRE: 0 <= newX <= maxX && 0 <= newY <= maxY, // where maxX and maxY are the borders of the window // POST: x' = newX && y '= newY //suitable implementation } public virtual float Area(){ //PRE: none //POST: Area' = area of the shape with 4 decimals precision //implementation using some approximation } }//end Shape public class Circle: Shape{ private int r; public override float Area(){ //--- } public override void MoveTo(){ //-- } }//end circle What can we do with Area()? What can we do to MoveTo()?
17
FEN 2014UCN Teknologi/act2learn17 Inheritance - design considerations If we need a class to hold a list of employees, and it should be possible to add employees at the end of list, but nowhere else. Should we inherit Array or ArrayList or…? We are not to inherit at all!!! But use delegation. Inheritance is not to be used senselessly trying to reuse code! Inheritance is to be seen as sub typing! Inheritance models “is-a” relationships. Code-reuse is obtainable by using existing classes (composition, delegation etc.)
18
FEN 2014UCN Teknologi/act2learn18 Inheritance - abstract classes A class that defines one or more methods that are not implemented is called abstract. And the non-implemented methods are specified abstract. An abstract class can not be instantiated. It can only serve as base-class. An abstract class can only be used as static type, not dynamic type for object. Abstract methods must be implemented in the sub- classes. Otherwise the subclass itself becomes abstract. So an abstract method defines or specifies functionality (but does not implement) what must be implemented in the subclasses. Constructors in an abstract class are only used by the constructors in the subclasses
19
FEN 2014UCN Teknologi/act2learn19 Example/Exercise: German Student ExerciseGermanStudents.pdf
20
FEN 2014UCN Teknologi/act2learn20 C# - When are objects equal? Classes ought to override the Equals-method inherited from Object public class Customer {. public override bool Equals(object obj) { Customer other; if ((obj == null) || (!(obj is Customer))) return false; // surely not equal other = (Customer) obj; // typecast to gain access return this.id == other.id; // equal, if ids are... }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.