Svetlin Nakov Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Svetlin Nakov Telerik Corporation
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
1 CS1001 Lecture Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 CS1001 Lecture Overview Object Oriented Design Object Oriented Design.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Object-Oriented Programming Fundamental Principles – Part I
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
Namespaces, Cohesion and Coupling Veselin Georgiev National Academy for Software Development academy.devbg.org Svetlin Nakov Telerik Corporation
2.5 OOP Principles Part 2 academy.zariba.com 1. Lecture Content 1.Polymorphism 2.Cohesion 3.Coupling 2.
Abstraction, Inheritance, and Polymorphism in Java.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1. 2 Object-Oriented Concept Class & Object Object-Oriented Characteristics How does it work? Relationships Between Classes Development Tools Advantage.
Advanced Concepts Svetlin Nakov Telerik Corporation
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance, Abstraction, Encapsulation, Polymorphism
Encapsulation and Polymorphism Encapsulation, Polymorphism, Class Hierarchies, Cohesion and Coupling SoftUni Team Technical Trainers Software University.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Inheritance, Abstraction, Encapsulation. 1. Fundamental Principles of OOP 2. Inheritance  Class Hierarchies  Inheritance and Access Levels 3. Abstraction.
Object-Oriented Design Justin Callanan CS 597. Object-Oriented Basics ● Data is stored and processed in “objects” ● Objects represent generalized real.
Defining Classes Classes, Fields, Constructors, Methods, Properties SoftUni Team Technical Trainers Software University
Svetlin Nakov Telerik Software Academy Manager Technical Trainer
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties.
Polymorphism, Class Hierarchies, Exceptions, Strong Cohesion and Loose Coupling Telerik Software Academy Object-Oriented Programming.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Abstraction Class Hierarchies, Abstract Classes, Interfaces SoftUni Team Technical Trainers Software University
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Polymorphism, Class Hierarchies, Exceptions, Strong Cohesion and Loose Coupling.
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Best Practices in the Object-Oriented Design Vesko Kolev Telerik Corporation
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Encapsulation and Polymorphism
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Abstract Classes, Abstract Methods, Override Methods
Object-Oriented Programming Concepts
Inheritance Class Hierarchies SoftUni Team Technical Trainers C# OOP
Inheritance and Polymorphism
One class is an extension of another.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Analysis and Design
One class is an extension of another.
Inheritance Basics Programming with Inheritance
Computer Programming with JAVA
Fundaments of Game Design
Object-Oriented Programming
Object-Oriented PHP (1)
Computer Science II for Majors
Presentation transcript:

Svetlin Nakov Telerik Corporation

1. Fundamental Principles of OOP 2. Inheritance 3. Abstraction 4. Encapsulation 5. Polymorphism 6. Cohesion and Coupling 2

 Inheritance  Inherit members from parent class  Abstraction  Define and execute abstract actions  Encapsulation  Hide the internals of a class  Polymorphism  Access a class through its parent interface 4

 Classes define attributes and behavior  Fields, properties, methods, etc.  Methods contain code for execution  Interfaces define a set of operations  Empty methods and properties, left to be implemented later 6 public class Labyrinth { … } public interface IFigure { … }

 Inheritance allows child classes inherits the characteristics of existing parent class  Attributes (fields and properties)  Operations (methods)  Child class can extend the parent class  Add new fields and methods  Redefine methods (modify existing behavior)  A class can implement an interface by providing implementation for all its methods 7

 Inheritance terminology derived class base class / parent class inherits derived interface base interface implements classinterface implements 8

 Inheritance has a lot of benefits  Extensibility  Reusability  Provides abstraction  Eliminates redundant code  Use inheritance for buidling is-a relationships  E.g. dog is-a animal (dogs are kind of animals)  Don't use it to build has-a relationship  E.g. dog has-a name (dog is not kind of name) 9

Person +Name: String +Address: String Employee +Company: String +Salary: double Student +School: String Base class Derived class 10

 Inheritance leads to a hierarchy of classes and/or interfaces in an application: 11 Game MultiplePlayersGame BoardGame Chess Backgammon SinglePlayerGame Minesweeper Solitaire … …

 We must specify the name of the base class after the name of the derived  In the constructor of the derived class we use the keyword base to invoke the constructor of the base class 12 public class Shape {...} public class Circle : Shape {...} public Circle (int x, int y) : base(x) {...}

public class Mammal { public int Age { get; set; } public int Age { get; set; } public Mammal(int age) public Mammal(int age) { this.Age = age; this.Age = age; } public void Sleep() public void Sleep() { Console.WriteLine("Shhh! I'm sleeping!"); Console.WriteLine("Shhh! I'm sleeping!"); }} 13

public class Dog : Mammal { public string Breed { get; set; } public string Breed { get; set; } public Dog(int age, string breed) public Dog(int age, string breed) : base(age) : base(age) { this.Breed = breed; this.Breed = breed; } public void WagTail() public void WagTail() { Console.WriteLine("Tail wagging..."); Console.WriteLine("Tail wagging..."); }} 14

 Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones... ... relevant to the given project (with an eye to future reuse in similar projects)  Abstraction = managing complexity "Relevant" to what? 16

 Abstraction is something we do every day  Looking at an object, we see those things about it that have meaning to us  We abstract the properties of the object, and keep only what we need  E.g. students get "name" but not "color of eyes"  Allows us to represent a complex reality in terms of a simplified model  Abstraction highlights the properties of an entity that we need and hides the others 17

Live Demo

 Encapsulation hides the implementation details  Class announces some operations (methods) available for its clients – its public interface  All data members (fields) of a class should be hidden  Accessed via properties (read-only and read- write)  No interface members should be hidden 19

 Data fields are private  Constructors and accessors are defined (getters and setters) Person -name : string -age : TimeSpan +Person(string name, int age) +Name : string { get; set; } +Age : TimeSpan { get; set; } 20

 Ensures that structural changes remain local:  Changing the class internals does not affect any code outside of the class  Changing methods' implementation does not reflect the clients using them  Encapsulation allows adding some logic when accessing client's data  E.g. validation on modifying a property value  Hiding implementation details reduces complexity  easier maintenance 21

 Polymorphism = ability to take more than one form (objects have more than one type)  A class can be used through its parent interface  A child class may override some of the behaviors of the parent class  Polymorphism allows abstract operations to be defined and used  Abstract operations are defined in the base class' interface and implemented in the child classes  Declared as abstract or virtual 23

 Why handle an object of given type as object of its base type?  To invoke abstract operations  To mix different related types in the same collection  E.g. List can hold anything  To pass more specific object to a method that expects a parameter of a more generic type  To declare a more generic field which will be initialized and "specialized" later 24

 Using override we can modify a method or property  An override method provides a new implementation of a member inherited from a base class  You cannot override a non-virtual or static method  The overridden base method must be virtual, abstract, or override 25

 Polymorphism ensures that the appropriate method of the subclass is called through its base class' interface  Polymorphism is implemented using a technique called late method binding  Exact method to be called is determined at runtime, just before performing the call  Applied for all abstract / virtual methods  Note: Late binding is slower than normal (early) binding 26

override CalcSurface() { return size * size; return size * size;} override CalcSurface() { return PI * radius * raduis; return PI * radius * raduis;} Abstract class Abstract action Concrete class Overriden action Figure +CalcSurface() : double Square -x : int -y : int -size : int Circle -x : int -y : int -radius: int 27

 Cohesion describes how closely all the routines in a class or all the code in a routine support a central purpose  Cohesion must be strong  Well-defined abstractions keep cohesion strong  Classes must contain strongly related functionality and aim for single purpose  Cohesion is a useful tool for managing complexity 28

 Good: hard disk, cdrom, floppy  BAD: spaghetti code 29

 Strong cohesion example  Class Math that has methods: Sin(), Cos(), Asin() Sqrt(), Pow(), Exp() Math.PI, Math.E 30 double sideA = 40, sideB = 69; double angleAB = Math.PI / 3; double sideC = Math.Pow(sideA, 2) + Math.Pow(sideB, 2) Math.Pow(sideA, 2) + Math.Pow(sideB, 2) - 2 * sideA * sideB * Math.Cos(angleAB); - 2 * sideA * sideB * Math.Cos(angleAB); double sidesSqrtSum = Math.Sqrt(sideA) + Math.Sqrt(sideB) + Math.Sqrt(sideC);

 Bad cohesion example  Class Magic that has these methods:  Another example: 31 public void PrintDocument(Document d); public void Send ( string recipient, string subject, string text); string recipient, string subject, string text); public void CalculateDistanceBetweenPoints( int x1, int y1, int x2, int y2) int x1, int y1, int x2, int y2) MagicClass.MakePizza("Fat Pepperoni"); MagicClass.WithdrawMoney("999e6");MagicClass.OpenDBConnection();

 Coupling describes how tightly a class or routine is related to other classes or routines  Coupling must be kept loose  Modules must depend little on each other  All classes and routines must have small, direct, visible, and flexible relations to other classes and routines  One module must be easily used by other modules 32

 Loose Coupling:  Easily replace old HDD  Easily place this HDD to another motherboard  Tight Coupling:  Where is the video adapter?  Can you change the video controller? 33

 OOP fundamental principals are: inheritance, encapsulation, abstraction, polymorphism  Inheritance allows inheriting members form another class  Abstraction and encapsulation hide internal data and allow working through abstract interface  Polymorphism allows working with objects through their parent interface and invoke abstract actions  Strong cohesion and loose coupling avoid spaghetti code 34

Questions?