Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs....

35
Computer Science 4U Unit 1 Programming Concepts and Skills Modular Design

Transcript of Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs....

Page 1: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Computer Science 4U Unit 1

Programming Concepts and Skills – Modular Design

Page 2: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Modular Design – Reusable Code

• Object-oriented programming (OOP) is a programming style that represents the concept of "objects" that have data fields (attributes that describe the object) and associated procedures known as methods.

• Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs.

Page 3: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• C++, Objective-C, Smalltalk, Delphi, Java, Javascript, C#, Perl, Python, Ruby and PHP are examples of object-oriented programming languages.

• Visual Basic .Net and newer versions are now true OOP. (Well almost…)

Page 4: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Class modules can immensely improve your productivity, help you solve many common and intricate programming problems, and even permit you to perform tasks that would be extremely difficult, if not impossible, otherwise.

• You can still use Visual Basic classes to better organize your code into truly reusable modules and design your applications entirely using concepts derived from the Object-Oriented Design discipline.

Page 5: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Most important, objects are the base on which almost every feature of Visual Basic is implemented. For example, without objects you can’t do serious database programming and you can’t deliver Internet applications. In short, you can do little or nothing without a firm grasp on what objects are and how you can take advantage of them.

Page 6: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Class vs. Object

• A class is a portion of the program (a source code file, in Visual Basic) that defines the properties, methods, and events of one or more objects that will be created during execution.

• An object is an entity created at run time, which requires memory and possibly other system resources, and is then destroyed when it’s no longer needed or when the application ends.

• In a sense, classes are design time–only entities, while objects are run time–only entities.

Page 7: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Users will never see a class; rather, they’ll probably see and interact with objects created from your classes, such as invoices, customer data, or circles on the screen.

• As a programmer, your point of view is reversed because the most concrete thing you’ll have in front of you while you’re writing the application is the class, in the form of a class module in the Visual Basic environment.

Page 8: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)
Page 9: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Encapsulation

• Encapsulation is probably the feature that programmers appreciate most in object-oriented programming. In a nutshell, an object is the sole owner of its own data. All data is stored inside a memory area that can’t be directly accessed by another portion of the application, and all assignment and retrieval operations are performed through methods and properties provided by the object itself. This simple concept has at least two far-reaching consequences:

Page 10: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• You can check all the values assigned to object properties before they’re actually stored in memory and immediately reject all invalid ones.

• You’re free to change the internal implementation of the data stored in an object without changing the way the rest of the program interacts with the object. This means that you can later modify and improve the internal workings of a class without changing a single line of code elsewhere in the application.

Page 11: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• The goal that every programmer should pursue is code reusability, which you achieve by creating classes that are easily maintained and reused in other applications. This is a key factor in reducing the development time and cost.

• When you start writing a new class, you should always ask yourself:

Page 12: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Is there any chance that this class can be useful in other applications?

• How can I make this class as independent as possible from the particular software I’m developing right now?

• In most cases, this means adding a few additional properties or additional arguments to methods.

Page 13: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• The concept of self-containment is also strictly related to code reuse and encapsulation.

• If you want to create a class module that’s easily reusable, you absolutely must not allow that class to depend on any entity outside it, such as a global variable. This would break encapsulation.

Page 14: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Polymorphism

• Polymorphism is the ability of different classes to expose similar (or identical) interfaces to the outside.

• The most evident kind of polymorphism in Visual Basic is forms and controls. TextBox and PictureBox controls are completely different objects, but they have some properties and methods in common, such as Left, BackColor, and Move.

Page 15: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• This similarity simplifies your job as a programmer because you don’t have to remember hundreds of different names and syntax formats.

• More important, it lets you manage a group of controls using a single variable (typed as Control, Variant, or Object) and create generic procedures that act on all the controls on a form and therefore noticeably reduce the amount of code you have to write.

Page 16: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

The Class Initialize event

• As you start building classes, you’ll soon notice how often you want to assign a well-defined value to a property at the time of the creation of the object itself, without having to specify it in the caller code.

• For example, if you’re dealing with an Employee object you can reasonably expect that in most cases its Citizenship property is “Canadian" (or whatever nationality applies where you live).

Page 17: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Similarly, in most cases the AddressFrom property in a hypothetical Invoice object will probably match the address of the company you’re working for.

• In all cases, you’d like for these default values to be assigned when you create an object, rather than your having to assign them manually in the code that uses the class.

Page 18: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Visual Basic offers a neat way to achieve this goal. In fact, all you have to do is write some statements in the Class_Initialize event of the class module.

‘ The Private member variable Private m_Citizenship As String

Private Sub Class_Initialize() m_Citizenship = "American“

End Sub

Page 19: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Overloading a Method

• Have you ever wanted to create a group of functions that each did essentially the same thing, with only their parameters changed?

• You can accomplish this by using optional parameters, or by changing the name of the function.

• Both techniques work, but they are not very elegant.

Page 20: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• In an OOP language such as Microsoft® Visual Basic®, you are allowed to create methods in a class that have the same name but different argument lists.

• Visual Basic can figure out which method to call during compile based on the parameter types that you pass. This technique is called overloading a method. (The "OOP" term for this is "polymorphism")

Page 21: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• The benefit of using the same name is that the user interface is kept consistent; only the inputs are different.

• The functionality within the method changes for each new overloaded method.

Page 22: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Reasons to Overload a Method

• Overloading a method allows you to keep your interface consistent, and allows you to logically call the same method regardless of the types of data you are passing in.

• You will find that using the same name will help you remember what a procedure does, as opposed to having to come up with new names or a naming convention, to help you keep things straight.

Page 23: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Method Overriding

• Overrides can only be used in a derived class (sometimes called a child class) that inherits from a base class (sometimes called a parent class).

• And Overrides is the hammer; it lets you entirely replace a method (or a property) from a base class.

Page 24: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Because it's so comprehensive and powerful, you have to have permission from the base class to use Overrides.

• But well designed code libraries provide it. (Your code libraries are all well designed, right?)

• VB.NET gives you even more control by allowing a base class to specifically require or deny a derived class to override using the MustOverride and NotOverridable keywords in the base class. But both of these are used in fairly specific cases.

Page 25: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Inheritance

• Inheritance is the ability, offered by many OOP languages, to derive a new class (the derived or inherited class) from another class (the base class).

• The derived class automatically inherits the properties and methods of the base class.

Page 26: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• For example, you could define a generic Shape class with properties such as Color and Position and then use it as a base for more specific classes (for example, Rectangle, Circle, and so on) that inherit all those generic properties. You could then add specific members, such as Width and Height for the Rectangle class and Radius for the Circle class.

Page 27: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Inheritance Basics

• The Inherits statement is used to declare a new class, called a derived class (child), based on an existing class, known as a base class (parent).

• Derived classes inherit, and can extend, the properties, methods, events, fields, and constants defined in the base class.

• The following describes some of the rules for inheritance, and the modifiers you can use to change the way classes inherit or are inherited:

Page 28: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• By default, all classes are inheritable unless marked with the NotInheritable keyword.

• Classes can inherit from other classes in your project or from classes in other assemblies that your project references.

• Unlike languages that allow multiple inheritance, Visual Basic allows only single inheritance in classes; that is, derived classes can have only one base class.

Page 29: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Although multiple inheritance is not allowed in classes, classes can implement multiple interfaces, which can effectively accomplish the same ends.

• To prevent exposing restricted items in a base class, the access type of a derived class must be equal to or more restrictive than its base class. For example, a Public class cannot inherit a Friend or a Private class, and a Friend class cannot inherit a Private class.

Page 30: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Inheritance Modifiers

• Visual Basic introduces the following class-level statements and modifiers to support inheritance:

• Inherits statement — Specifies the base class.

• NotInheritable modifier — Prevents programmers from using the class as a base class.

• MustInherit modifier — Specifies that the class is intended for use as a base class only.

Page 31: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

Overriding Properties and Methods in Derived Classes

• By default, a derived class inherits properties and methods from its base class.

• If an inherited property or method has to behave differently in the derived class it can be overridden.

• That is, you can define a new implementation of the method in the derived class. The following modifiers are used to control how properties and methods are overridden:

Page 32: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Overridable — Allows a property or method in a class to be overridden in a derived class.

• Overrides — Overrides an Overridable property or method defined in the base class.

• NotOverridable — Prevents a property or method from being overridden in an inheriting class. By default, Public methods are NotOverridable.

Page 33: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• MustOverride — Requires that a derived class override the property or method.

• When the MustOverride keyword is used, the method definition consists of just the Sub, Function, or Property statement.

• No other statements are allowed, and specifically there is no End Sub or End Function statement.

• MustOverride methods must be declared in MustInherit classes.

Page 34: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Revisit your DeckoCard Library. Using the information in this lesson. Attempt to make your class reuseable, more robust and follow the concepts of OOP.

• Remember Encapsulation, Polymorphism and Inheritance. Try to implement them into your code design.

Page 35: Computer Science 4U Unit 1 - Weeblyfon10.weebly.com/uploads/1/3/4/7/13472506/ics4_1_5...Class vs. Object •A class is a portion of the program (a source code file, in Visual Basic)

• Within your code, add comments that show your changes to the original library.

• Once complete, send a copy of your source code.

• Save your work as john_s_1_5_DeckoCards2 (where john_s is replaced by your name) under the appropriate folder for your class in the DropOff folder on the X: drive