Computing with C# and the .NET Framework

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
OOP: Inheritance By: Lamiaa Said.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance using Java
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Topic 4 Inheritance.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 TCSS 143, Autumn 2004 Lecture Notes Inheritance.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
© 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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
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 Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance.
Chapter 13 - Inheritance.
Modern Programming Tools And Techniques-I
EKT472: Object Oriented Programming
Web Design & Development Lecture 9
Inheritance In the real world, objects aren’t usually one-of-a-kind.
CSC 222: Object-Oriented Programming Spring 2017
Overriding Method.
Inheritance and Polymorphism
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
Phil Tayco Slide version 1.1 Created Oct 30, 2017
12 Data abstraction Packages and encapsulation
CSC 205 Java Programming II
CSC 205 Programming II Lecture 2 Subclassing.
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Introduction to Inheritance
Fundaments of Game Design
By Rajanikanth B OOP Concepts By Rajanikanth B
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Programming in C# CHAPTER 5 & 6
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Computing with C# and the .NET Framework Inheritance

Classification Organizes knowledge Reflects the IS-A relationship A Mammal is an Animal Class Hierarchies base class may have derived classes

Figure 10.1 The account hierarchy BankAccount SavingsAccount CheckingAccount TimedAccount Figure 10.1 The account hierarchy

Defining a subclass public class SimpleSavings1 : BankAccount { } SimpleSavings1 IS-A BankAccount Inherits balance field and Deposit, Withdraw, and GetBalance methods

Subclass constructors new SimpleSavings1(); //C# creates default constructor, balance=0 SimpleSavings2 subclass adds a constructor public SimpleSaving2(double amount) : base(amount); // superclass constructor { } Initializes balance = amount

Adding State and Behavior SavingsAccount class derived from BankAccount Adds interestRate field and PostInterest method. Its constructor calls base class constructor in declaration and then initializes interestRate The private balance field is not accessible in the derived class. PostInterest uses Deposit method

Overriding behavior Redefine inherited method CheckingAccount class derived from BankAccount adds charge and minBalance fields and ProcessCheck behavior. charge imposed for each check unless balance above minBalance Overrides Withdraw to use same rules as a check

Figure 10.2 withdraw method overrides BankAccount withdraw public override double Withdraw(double amount) { return ProcessCheck(amount); } Figure 10.2 withdraw method overrides BankAccount withdraw

BankAccount balance GetBalance CheckingAccount SavingsAccount minBalance interestRate charge PostInterest Withdraw ProcessCheck Figure 10.3 Inheritance

The Object class Every class inherits from Object directly or indirectly BankAccount implicitly extends Object, as if public class BankAccount : Object Object defines ToString to display empty string so b.ToString() is empty for BankAccount b (need to override ToString)

Figure 10.4 Inheriting from Object ToString // other methods Person BankAccount id balance name address GetBalance Deposit GetId Withdraw ToString Figure 10.4 Inheriting from Object

Polymorphism Many structures, for example Eat() Animal Eat() Each Animal eats differently Lion Eat vs. Giraffe Eat Animal herman = new Lion() Animal is compile-time type, Lion run-time Base class variable can refer to derived class object

Figure 10.5 Illustrating polymorphism /* Each Animal implements its own eat() * method depending on its run-time type public void feed(Animal[] theZoo) { for (int i= 0; i < theZoo.Length; i++) theZoo[i].Eat(); } Figure 10.5 Illustrating polymorphism

Polymorphic BankAccount BankAccount b2 created as SavingsAccount Withdraw does not assess service charge Then reference b2 assigned to CheckingAccount Withdraw does assess service charge Each object executes its own version of Withdraw

Figure 10.6 Two BankAccount Objects BankAccount b1 = new CheckingAccount(1500.00,.50) balance = 0.0 minBalance = 1500.00 charge = .50 :CheckingAccount b1 BankAccount b2 = new SavingsAccount(500.00,4.0) :SavingsAccount balance = 500.0 interestRate = 4.0 b2 Figure 10.6 Two BankAccount Objects

Figure 10.7 Variable b2 refers to a checking account balance = 349.50 minBalance = 1500.0 b2 charge = .50 Figure 10.7 Variable b2 refers to a checking account

Figure 10.8 Making the Animal class abstract // An abstract class has no instances // Defines behavior common to all subclasses // Every Animal can eat public abstract class Animal { public void Eat() { } Figure 10.8 Making the Animal class abstract

Figure 10.9 The abstract Shape class Shape {abstract} center : Point Draw ToString Move Draw method is abstract. Subclasses must override it. center is a Point associated with the Shape Move moves the center by the amounts passed to it Figure 10.9 The abstract Shape class

Figure 10.10 Moving and drawing shapes // Each Shape moves and draws in its own way public void MoveDraw (Shape[] theShapes, int xAmount, int yAmount, Graphics g) { for (int i = 0; i < theShapes.Length; i++) { theShapes[i].Move(x,y); theShapes[i].Draw(g); } Figure 10.10 Moving and drawing shapes

Line and Circle Line adds an end Point Line Move calls base.Move to move center then translates the end point Line Draw draws a line Circle adds a radius Circle inherits Shape Move, no need to override

Class Visibility A class can be public or not public class X { … } // visible everywhere class Y { … } //only visible within program Classes used outside of the program must be public

Figure 10.13 Access modifiers for data fields and methods   public Accessible anywhere the class name is accessible. protected internal Accessible in derived classes and in the program in which the data field or method is declared protected Accessible in derived classes.    internal Accessible in the program in which the data field or method is (can omit) declared. private Accessible only in the class in which the data field or method is declared. Figure 10.13 Access modifiers for data fields and methods