Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing with C# and the .NET Framework

Similar presentations


Presentation on theme: "Computing with C# and the .NET Framework"— Presentation transcript:

1 Computing with C# and the .NET Framework
Inheritance

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

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

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

5 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

6 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

7 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

8 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

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

10 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)

11 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

12 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

13 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

14 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

15 Figure 10.6 Two BankAccount Objects
BankAccount b1 = new CheckingAccount( ,.50) balance = 0.0 minBalance = 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

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

17 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

18 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

19 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 Moving and drawing shapes

20 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

21 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

22 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 Access modifiers for data fields and methods


Download ppt "Computing with C# and the .NET Framework"

Similar presentations


Ads by Google