Advanced Object Oriented Concepts Tonga Institute of Higher Education.

Slides:



Advertisements
Similar presentations
By Waqas Over the many years the people have studied software-development approaches to figure out which approaches are quickest, cheapest, most.
Advertisements

General OO Concepts Objectives For Today: Discuss the benefits of OO Programming Inheritance and Aggregation Abstract Classes Encapsulation Introduce Visual.
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Chapter 10: Introduction to Inheritance
More about classes and objects Classes in Visual Basic.NET.
1 Inheritance & Interface. 2 Why Inheritance? Common properties? Methods to send congratulations to Employees and Customers Public Sub SendCongratulationsE(ByVal.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Compunet Corporation1 Programming with Visual Basic.NET Inheritance Lecture # 10 Tariq Ibn Aziz.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Computer Science I Inheritance Professor Evan Korth New York University.
Object-Oriented Programming: Inheritance
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Module 7: Object-Oriented Programming in Visual Basic .NET
An Object-Oriented Approach to Programming Logic and Design
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 8: Writing Graphical User Interfaces
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Programming Pillars Introduction to Object- Oriented Programming.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Working With Objects Tonga Institute of Higher Education.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
1.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
These materials where developed by Martin Schray
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming in Java: lecture 7
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
JAVA By Waqas.
Lecture 12 Inheritance.
University of Central Florida COP 3330 Object Oriented Programming
Inheritance and Polymorphism
One class is an extension of another.
Microsoft Visual Basic 2005: Reloaded Second Edition
Table of Contents Class Objects.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Inheritance
One class is an extension of another.
Object-Oriented Programming: Inheritance
Chapter 11 – Object-Oriented Programming
IS437: Fall 2004 Instructor: Dr. Boris Jukic
Object-Oriented Programming: Inheritance
Tonga Institute of Higher Education
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Object-Oriented Programming
Object-Oriented Programming: Inheritance
Presentation transcript:

Advanced Object Oriented Concepts Tonga Institute of Higher Education

Inheritance Similarities “You look just like your father.”  People inherit features from their parents: Eye color Hair color Height  You are similar to your parents

Inheritance in Visual Basic.Net Inheritance – When a class inherits members from another class Objects are like people. They can have children!  Children can inherit variables from a parent  Children can inherit methods from a parent Parent / Base Class Child / Sub Class

History of Inheritance In the sixties, a programmers created a program to simulate traffic  They used objects for their vehicles Cars Trucks Vans  They noticed that all vehicles did the same things Turn left Turn right Brake Go

Plan #1 - Van, Car and Truck Objects Create one class for each vehicle  Van  Car  Truck Van TurnLeft() TurnRight() Brake() Go() Car TurnLeft() TurnRight() Brake() Go() Truck TurnLeft() TurnRight() Brake() Go()

Plan #1 - Advantages It’s quick and easy to understand Van TurnLeft() TurnRight() Brake() Go() Car TurnLeft() TurnRight() Brake() Go() Truck TurnLeft() TurnRight() Brake() Go()

Plan #1 - Disadvantages Code is repeated in each object  Changing the code in Brake() requires 3 changes to 3 different objects Method names can be changed.  After a while, the objects are not similar Van TurnLeft() -> Left() TurnRight() -> Right() Brake() Go() Car TurnLeft() TurnRight() Brake() Go() -> Move() Truck TurnLeft() TurnRight() Brake() Go() -> Start()

Plan #2 - Inheritance Make one object with common methods. The code in the parent object is used in the child objects. Vehicle TurnLeft() TurnRight() Brake() Go() Car TurnLeft() TurnRight() Brake() Go() Truck TurnLeft() TurnRight() Brake() Go() Methods in the parent come down to the children!

Plan #2 – Advantages A change in the method code in the parent automatically changes the children classes  Method code is consistent and easy to maintain A change in the method name in the parent automatically changes the children.  Names are consistent and easy to maintain We can change a class that someone else created  It is difficult to write your own button class. But we can add changes to the button class using inheritance Vehicle TurnLeft() -> Left() TurnRight() -> Right() Brake() Go() Car TurnLeft() -> Left() TurnRight() -> Right() Brake() Go() Truck TurnLeft() -> Left() TurnRight() -> Right() Brake() Go()

Plan #2 – Disadvantages Inheritance requires special code Inheritance requires more understanding Vehicle TurnLeft() TurnRight() Brake() Go() Car TurnLeft() TurnRight() Brake() Go() Truck TurnLeft() TurnRight() Brake() Go()

Inheritance Differences “You look just like your father.”  People inherit features from their parents: Eye color Hair color Height  You are similar to your parents. “But you are much taller”  You are also different from them.

Inheritance Differences Each child object can have additional different members. Vehicle TurnLeft() TurnRight() Brake() Go() Car TurnLeft() TurnRight() Brake() Go() ConservePetrol() Truck TurnLeft() TurnRight() Brake() Go() CarryLoad()

Example of Parent / Base Class All non-private members are shared with child classes

Example of Child / Sub Class Special keyword that brings members from parent Child specific classes

Example of Driver for Inheritance Inherited classes work exactly like a normal class There is no difference when using an inherited method and a non-inherited method

Demonstration Inheritance Code

Inheritance in VB.Net Classes Look in the class definitions. Almost every class inherits from another class!

Demonstration Inheritance in VB.Net Classes

Access Specifiers Public  Can be used by everything Private  Can only be used by code inside the same class Dim  Same as Private Friend  Can be used by code inside the same project Dim FirstName as String Access Specifier Name Type Protected  Can be used by code that inherits from this class Protected Friend  Combination of Protected and Friend

Demonstration Access Specifiers

Multi-Level Inheritance When a subclass is also a base class. Vehicle TurnLeft() TurnRight() Brake() Go() Car TurnLeft() TurnRight() Brake() Go() ConservePetrol() RentalCar TurnLeft() TurnRight() Brake() Go() ConservePetrol() RentalPrice()

Demonstration Multi-Level Inheritance

Overriding Base Methods Overriding – When a child class replaces the behavior of a method defined in a base class. To override (replace) a method:  Define a method in the parent class to be overridable  Define a method in the child class to override Parent Class Child Class

Demonstration Overriding Base Methods

MyBase Use in a child class Use the MyBase keyword to call methods in a base class.

Demonstration MyBase

MyClass and Me 1 If inheritance is not used, they work the same way.

MyClass and Me 2 If inheritance is used, use these in parent classes Use the MyClass keyword to call methods in the class this keyword is used. Use the Me keyword to call methods in the current instance.

Demonstration MyClass and Me

Abstract Classes and Methods Abstract class – A class that must be inherited from  An abstract class cannot be instantiated  MustInherit – Used to make a class abstract Abstract method – A method that must be overridden  Method code does not exist in the base class because it will be overridden  MustOverride – Used to make a method abstract

Demonstration Abstract Class or Method

Preventing Inheritance NotInheritable – Used to make a class uninheritable.

Demonstration Preventing Inheritance

Introduction to Interfaces 1 All DVD players have the same controls, even if they are made by different companies (Sony, Panasonic, Toshiba, etc.) The buttons are a contract for how to operate the DVD player An interface is a contract detailing how an object is used Using this interface, I know how to use the DVDPlayer Object DVD Player Play() Pause() Stop() Rewind() FastForward()

Introduction to Interfaces 2 All DVD players have the same controls, even if they are made by different companies (Sony, Panasonic, Toshiba, etc.) But these different companies are not related to each other This is the difference between interfaces and inheritance

Defining an Interface Defining an interface tells the computer that any class that uses this interface is guaranteed to have the elements described in the interface Example: Any class that implements the ISummary will have these functions and properties  GetShortSummary  GetFullSummary  HasFullSummary

Demonstration Defining an Interface

Implementing an Interface

Demonstration Implementing an Interface

Primary/Native Interfaces vs. Secondary Interfaces Primary/Native Interface of a class is composed of all the members defined in a class Secondary Interfaces involves the implementation of other interfaces Secondary Interface Primary Interface

Abstract Classes vs. Interfaces It is sometimes difficult to tell the difference between an abstract class and an interface  An abstract class is a class that cannot be instantiated, but must be inherited from  An interface, by contrast, is a set of members that defines a contract for conduct

Abstract Classes vs. Interfaces 2 It is sometimes also difficult to decide whether to use an abstract class or an interface  A consideration is that a class may implement more than one interface. A class may only inherit from only 1 class.  Abstract classes are best for objects that are closely related. Interfaces are best for providing common functionality to unrelated classes.  Use an abstract class to provide members that use the same code among many objects. Interfaces only force classes to have the same member names.