Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
ITEC200 – Week03 Inheritance and Class Hierarchies.
More about classes and objects Classes in Visual Basic.NET.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Object-Oriented Application Development Using VB.NET 1 Creating a String Array Code to create a String array: ' declare a String array with 4 elements.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Object-Oriented Application Development Using VB.NET 1 Chapter 7 Adding Responsibilities to Problem Domain Classes.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance in the Java programming language J. W. Rider.
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.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Dr. Azeddine Chikh IS444: Modern tools for applications development.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Object-Oriented Application Development Using VB.NET 1 Chapter 9 Implementing Association Relationships.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance and Polymorphism
Object Oriented Programming
Can perform actions and provide communication
Can perform actions and provide communication
Inheritance, Polymorphism, and Interfaces. Oh My
Programming with Microsoft Visual Basic 2008 Fourth Edition
Can perform actions and provide communication
Java Inheritance.
Object-Oriented Programming: Inheritance and Polymorphism
CIS 199 Final Review.
Presentation transcript:

Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces

Object-Oriented Application Development Using VB.NET 2 Objectives In this chapter, you will: Implement the Boat generalization/specialization class hierarchy Understand abstract and final classes and the MustInherit and NotInheritable keywords Override a superclass method Understand private versus protected access

Object-Oriented Application Development Using VB.NET 3 Objectives In this chapter, you will: Explore the Lease subclasses and abstract methods Understand and use interfaces Use custom exceptions Understand the Object class and inheritance

Object-Oriented Application Development Using VB.NET 4 Implementing the Boat Generalization/Specification Hierarchy Boat class –Stores information about a boat’s State registration number Length Manufacturer Model year

Object-Oriented Application Development Using VB.NET 5 Implementing the Boat Generalization/Specification Hierarchy Boat class –Parameterized constructor Accepts values for all four attributes –Eight standard accessor methods Four setter methods Four getter methods –TellAboutSelf method

Object-Oriented Application Development Using VB.NET 6 Testing the Boat Superclass with a Windows Form A Windows application and a Windows form with buttons can be used to test all problem domain classes Windows form testers –Accomplish the same objective as a class module To systematically test each problem domain class to demonstrate that all functionality works as intended

Object-Oriented Application Development Using VB.NET 7 Using the Inherits Keyword to Create the Sailboat Subclass Generalization/specialization hierarchy –Superclass Includes attributes and methods that are common to specialized subclasses –Instances of the subclasses Inherit attributes and methods of the superclass Include additional attributes and methods

Object-Oriented Application Development Using VB.NET 8 Using the Inherits Keyword to Create the Sailboat Subclass Superclass –Boat class Four attributes and eight accessor methods Subclasses –Sailboat Three additional attributes –Powerboat Two additional attributes

Object-Oriented Application Development Using VB.NET 9 Using the Inherits Keyword to Create the Sailboat Subclass

Object-Oriented Application Development Using VB.NET 10 Using the Inherits Keyword to Create the Sailboat Subclass Inherits keyword –Used in the class header to implement a subclass –Indicates which class the new class is extending –Example: Class header to define the Sailboat class as a subclass of Boat: Public Class Sailboat Inherits Boat

Object-Oriented Application Development Using VB.NET 11 Using the Inherits Keyword to Create the Sailboat Subclass Class definition of a subclass –Includes any attributes and methods in addition to those inherited from the superclass Sailboat constructor –Accepts seven parameters Four for attributes defined in the Boat superclass Three for additional attributes defined in Sailboat

Object-Oriented Application Development Using VB.NET 12 Using the Inherits Keyword to Create the Sailboat Subclass Sailboat constructor –MyBase.New call Used to set attributes for registration number, length, manufacturer, and year Invokes constructor of the superclass Must be the first statement in the constructor Required unless the superclass includes a default constructor without parameters –Sailboat set accessor methods Used to set the three remaining attribute values

Object-Oriented Application Development Using VB.NET 13 Adding a Second Subclass – Powerboat Powerboat class –Extends the Boat class –Declares two attributes: numberEngines fuelType –Constructor expects six parameters Four required by Boat Two additional attributes for Powerboat

Object-Oriented Application Development Using VB.NET 14 Adding a Second Subclass – Powerboat Once the boats are created –Four getter methods inherited from Boat Can be invoked for both sailboats and powerboats –Three additional getter methods of Sailboat Not present in powerboats –Two additional getter methods of Powerboats Not present in sailboats

Object-Oriented Application Development Using VB.NET 15 Understanding Abstract and Final Classes Concrete classes –Classes that can be instantiated –Examples Sailboat class Powerboat class

Object-Oriented Application Development Using VB.NET 16 Using the MustInherit Keyword Abstract class –Not intended to be instantiated –Only used to extend into subclasses –Used to facilitate reuse MustInherit keyword –Used in class header to declare an abstract class –Example: Class header to make the Boat class abstract: Public MustInherit Class Boat

Object-Oriented Application Development Using VB.NET 17 Using the NotInheritable Keyword A Final class –A class that cannot be extended –Created for security purposes or efficiency –Created using the NotInheritable keyword –Example Class header for the Powerboat class : Public NotInheritable Class Powerboat Inherits Boat

Object-Oriented Application Development Using VB.NET 18 Overriding a Superclass Method Method overriding –Method in subclass will be invoked instead of method in the superclass if both methods have the same signature –Allows the subclass to modify the behavior of the superclass

Object-Oriented Application Development Using VB.NET 19 Overriding a Superclass Method Method overriding vs. method overloading –Overloading Two or more methods in the same class have the same name but a different return type or parameter list –Overriding Methods in both the superclass and subclass have the same signature

Object-Oriented Application Development Using VB.NET 20 Overriding the Boat TellAboutSelf Method TellAboutSelf method –Demonstrates method overriding –Used to make it easier to get information about an instance of the class Overridable keyword –Included in method header to make a method overridable

Object-Oriented Application Development Using VB.NET 21 Overriding the Boat TellAboutSelf Method To override the TellAboutSelf method –Use the same method signature in the subclass, except for using Overrides keyword in place of Overridable keyword Once a method is overriden –Statements in the subclass method control what system does when a sailboat instance is asked to tell about itself –Method in Boat is ignored

Object-Oriented Application Development Using VB.NET 22 Overriding and Invoking a Superclass Method Sometimes the programmer needs to override a method by extending what the method does For example –Powerboat TellAboutSelf method invokes the superclass method using MyBase keyword Superclass method name

Object-Oriented Application Development Using VB.NET 23 Overriding, Polymorphism, and Dynamic Binding Polymorphism –Objects of different classes can respond to the same message in their own way Dynamic binding –Used to resolve which method to invoke when the system runs and finds more than one method with the same name –Provides flexibility when adding new subclasses that override superclass methods

Object-Oriented Application Development Using VB.NET 24 Understanding Private Versus Protected Access Declaring attributes as private –Done by using Private keyword –No other object can directly read or modify the values Other objects must use methods of the class to get or set values –Ensures encapsulation and information hiding –Limits the ability of an instance of a subclass to directly access attributes defined by the superclass

Object-Oriented Application Development Using VB.NET 25 Understanding Private Versus Protected Access Declaring attributes as Protected –Done by using Protected keyword –Values can be directly accessed by subclasses Local variable –Accessible only to statements within a method where it is declared –Exists only as long as the method is executing –Does not need to be declared as private, protected, friend, or public

Object-Oriented Application Development Using VB.NET 26 Understanding Private Versus Protected Access Methods –Private methods Can only be invoked from a method within the class –Private methods Cannot be invoked even by subclass methods –Protected methods Can be invoked by a method in a subclass

Object-Oriented Application Development Using VB.NET 27 Introducing the Lease Subclasses and Abstract Methods Lease class –Subclasses AnnualLease DailyLease –Attributes Amount: a numeric value Start date: a reference variable End date : a reference variable –Defined as abstract Includes MustInherit keyword in header

Object-Oriented Application Development Using VB.NET 28 Introducing the Lease Subclasses and Abstract Methods

Object-Oriented Application Development Using VB.NET 29 Introducing the Lease Subclasses and Abstract Methods Lease class constructor –Accepts one parameter A reference to a DateTime instance for start date of the lease –Sets end date to Nothing –Sets amount of the lease to zero Subclasses set end date and calculate amount depending on type of the lease

Object-Oriented Application Development Using VB.NET 30 Adding an Abstract Method to Lease Sometimes it is desirable to require all subclasses to include a method All Lease subclasses need a CalculateFee method –Subclasses are responsible for determining what the lease amount will be –Necessary for polymorphism

Object-Oriented Application Development Using VB.NET 31 Adding an Abstract Method to Lease Abstract method –A method without any statements that must be overridden by all subclasses –Declared by using MustOverride keyword in method header –For example CalculateFee method of the Lease class

Object-Oriented Application Development Using VB.NET 32 Implementing the AnnualLease Subclass AnnualLease subclass attributes –balanceDue The amount of the annual lease that remains unpaid –payMonthly A Boolean Indicates whether monthly payments will be made for the annual lease

Object-Oriented Application Development Using VB.NET 33 Implementing the AnnualLease Subclass If payMonthly is true –balanceDue is initially set to eleven-twelfths of the lease amount If payMonthly is false –balanceDue will be zero

Object-Oriented Application Development Using VB.NET 34 Implementing the DailyLease Subclass DailyLease –A subclass of the Lease class –Used when a customer leases a slip for a short time –Additional attribute Number of days of the lease –Calculated based on the start date and end date

Object-Oriented Application Development Using VB.NET 35 Understanding and Using Interfaces An interface –Defines abstract methods and constants that must be implemented by classes that use the interface –Can be used to ensure that an instance has a defined set of methods

Object-Oriented Application Development Using VB.NET 36 Understanding and Using Interfaces Component-based development –Components interact in a system using a well- defined interface –Components might be built using a variety of technologies Interfaces –Define how components can be used –Play an important role in developing component- based systems

Object-Oriented Application Development Using VB.NET 37 Understanding and Using Interfaces A VB.NET class –Can inherit from only one superclass –Can implement one or more interfaces Interfaces allow VB.NET subclasses a form of multiple inheritance Multiple inheritance –Ability to inherit from more than one class –A subclass is part of two or more generalization/specialization hierarchies

Object-Oriented Application Development Using VB.NET 38 Creating a VB.NET Interface Interface –Name (by convention) Begins with a capital letter “I” Uses the word “interface” –Header Uses Interface keyword, followed by the interface name –Methods Include no code

Object-Oriented Application Development Using VB.NET 39 Creating a VB.NET Interface A class can implement an interface by adding the following to the class header: –Implements keyword –Name of the interface

Object-Oriented Application Development Using VB.NET 40 Implementing More Than One Interface To implement more than one interface –Use Implements keyword in the class header –Separate multiple interface names by commas –For example: Public Class DailyLease Inherits Lease Implements ILeaseInterface, ICompanyInterface

Object-Oriented Application Development Using VB.NET 41 Using Custom Exceptions Custom exception –An exception written specifically for an application Example: –LeasePaymentException A custom exception Created by defining a class that extends the Exception class Designed for use by the AnnualLease class Thrown if payment is invalid

Object-Oriented Application Development Using VB.NET 42 Throwing a Custom Exception RecordLeasePayment method –A custom method –Records a payment –Expects to receive the amount of the payment –Throws a LeasePaymentException instance if payment amount is not valid

Object-Oriented Application Development Using VB.NET 43 Understanding the Object Class and Inheritance Object class –Extended by all classes in VB.NET –Defines basic functionality that other classes need in VB.NET ToString method –A method of the Object class –Inherited by all classes –By default, returns a string representation of the class name –Overridden by other classes to provide more specific information

Object-Oriented Application Development Using VB.NET 44 Understanding the Object Class and Inheritance Some other methods of the Object class –Equals –GetHashCode –GetType –ReferenceEquals –The Default New constructor

Object-Oriented Application Development Using VB.NET 45 Summary Generalization/specialization hierarchies show superclasses and subclasses The subclass inherits attributes and methods of the superclass An abstract class: a class that is not instantiated; exists only to serve as a superclass A final class: a class that cannot be extended

Object-Oriented Application Development Using VB.NET 46 Summary An abstract method in a superclass must be implemented by all the subclasses An interface can be used to require that classes contain specific methods Custom exceptions can provide detailed information following an exception In VB.NET, the Object class is the superclass of all classes