Download presentation
Presentation is loading. Please wait.
1
Inheritance, Shared
2
Projectiles Program
3
Demonstrates – Inheritance – MustInherit – Shared vs. Non-shared methods A variation on the Multiball example that I gave earlier
5
The Class Diagram The class diagram on the previous slide shows the three custom classes in the program. The ball and football classes inherit from the Projectile class. This means that they have all of the properties and procedures of the Projectile class. They either inherit these directly, or override them to match their specific characteristics. In this program, the Draw procedure and Radius property are both overridden in the subclasses.
6
Abstract Class (MustInherit) Projectiles is an abstract class, meaning that you can’t make objects directly from it. In VB, abstract classes are indicated by the “MustInherit” keyword. You probably ran into an abstract class in assignment 4: the Brush class. The “Fill” subroutines all took a Brush as a parameter, but you weren’t allowed to create a Brush object. Instead you had to create a SolidBrush, TextureBrush, or GradientBrush object; since all of these classes inherit from the abstract Brush class, they can all be considered to be Brushes.
7
Abstract Classes and Interfaces An abstract class is similar to an interface (like Idrawable). It defines the procedures and properties that all classes that inherit from it will have. Unlike an interface, however, an abstract class can contain some implementation—some actual code to do something. In this case, the Projectile class contains all of the properties and methods needed to make the objects move around the screen according to the laws of physics. Only one procedure (Draw) and one property (Radius) that pertain to the objects’ appearance are left to be defined in the subclasses.
8
Projectile Class Code
9
Projectile Class Code (Velocity, Speed)
10
Projectile Class Code (MustOverride) Notice that the MustOverride sub has a header only; no implementation. This is just like in an Interface. This tells all subclasses that they must be able to draw themselves; There is no default Draw method for Projectiles, but every Projectile object (of any subclass) must be able to draw itself. The same is true for the Radius property; every subclass must define it in its own way.
11
Projectile Class: Shared Function Collides A Shared procedure belongs to the class, not to a particular object. You call a Shared procedure by using the name of the class, followed by a period, followed by the name of the procedure. The code for the Collides function is on the next slide.
12
Collides Code You can achieve the same result with a non-shared function, as shown on the next slide. The CollidesWith function determines if the current projectile (Me) collides with the projectile passed as a parameter (a).
13
CollidesWith Code You will see both Shared and non-shared methods in built-in classes as well. For example:
14
The ball subclass: Getting Started Note that the constructors are in the subclass since the parent class is MustInherit.
15
Must Override what must be Overridden! The football class is very similar, with a more complex Draw sub and a different radius.
16
Using Inherited Classes The key concept to understand for the next few slides is: Inheritance is an “is-a” relationship. Since ball inherits from Projectile, a ball object IS A Projectile. Similarly for football. We can create collections of Projectiles and puts balls and footballs in them. We can declare parameters to be of type Projectile and pass balls or footballs through them.
18
Making it work The Timer tick invalidates the picture box, causing the Paint event to occur. Most of what actually happens in this program happens in the Paint event, which is shown in the next slide. The comments will serve as explanation.
20
Shared Function Libraries Related functions are often grouped into a class. These classes generally don’t have any data, and you can’t make objects from them (no constructor). Instead, they simply contain functions and constants.
21
Math Class Examples
22
Creating a Shared Function Library Simply create a class, and – Add Public Shared functions Public Shared Function Exp(d As Double) As Double – Add Public Constants Public Const PI As Double = 3.1415926535897931
23
Public Interface is a Contract! A class’s interface is the collection of Public properties and procedures available for client programs to use. Users of the class rely on this interface; these properties and procedures are how they make your class work in their programs. Therefore, the interface becomes like a contract between you and the other programmer; if you change the interface, their programs may not work anymore.
24
Changes You Shouldn’t Make Once other students start seriously using your class, you should protect the interface (contract) by: 1.Do not remove or rename any public properties or enumerations. 2.Do not remove, rename, or change the parameters on any procedure (including New).
25
Changes You Can Make You can improve your classes in the following ways without breaking the contract: 1.Improve the implementation (internal code) of a procedure or property. Perhaps you make it run faster, or draw itself in a better way. As long as you don’t change the interface, you can change the implementation.* 2.Add new procedures and properties. 3.Overload existing procedures (including New) with different parameter lists. 4.Inherit from the class and make modifications in the subclass. * Be careful when changing the implementation. It may not break the other students’ code when you do, but it may not work the way they want anymore. If your new draw routine makes prettier objects, they’ll probably like it; but if the new draw routine makes objects that are 10 times as big, or does something totally unrelated to drawing, they won’t be happy.
26
File System Classes Directory (shared methods) File (shared methods) DriveInfo (object methods) DirectoryInfo (object methods) FileInfo (object methods)
27
Using Non-Shared File System Classes DriveInfo DirectoryInfo FileInfo The code on the next two slides is from the “FileSystemClasses” project available in today’s lecture materials.
28
Non-Shared IO Classes
29
Shared IO Classes
30
File System There are many more methods available in these classes; If you want to create classes that work with the file system, check these classes out in more detail.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.