Inheritance, Shared. Projectiles Program Demonstrates – Inheritance – MustInherit – Shared vs. Non-shared methods A variation on the Multiball example.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Graphics Procedure (IDrawable) 1. Basic Procedure For Drawing ThingsToDraw Create Objects From Classes Add Them to PictureBox Use the Paint Event to draw.
Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Classes and Class Libraries Examples and Hints November 9,
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Lecture 9 Polymorphism Richard Gesick.
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.
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
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.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Animations Demonstrates more animation techniques.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Object-Oriented Programming: Inheritance and Polymorphism.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
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.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
User-Written Functions
Lecture 23 Polymorphism Richard Gesick.
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Interfaces.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Java Inheritance.
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Presentation transcript:

Inheritance, Shared

Projectiles Program

Demonstrates – Inheritance – MustInherit – Shared vs. Non-shared methods A variation on the Multiball example that I gave earlier

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.

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.

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.

Projectile Class Code

Projectile Class Code (Velocity, Speed)

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.

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.

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

CollidesWith Code You will see both Shared and non-shared methods in built-in classes as well. For example:

The ball subclass: Getting Started Note that the constructors are in the subclass since the parent class is MustInherit.

Must Override what must be Overridden! The football class is very similar, with a more complex Draw sub and a different radius.

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.

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.

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.

Math Class Examples

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 =

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.

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

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.

File System Classes Directory (shared methods) File (shared methods) DriveInfo (object methods) DirectoryInfo (object methods) FileInfo (object methods)

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.

Non-Shared IO Classes

Shared IO Classes

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.