470 likes | 495 Views
Introduction to Classes and Objects. Ch 4. Object-Oriented Programming. Object Abstraction of a read-world item (e.g., car) Has attributes (e.g., size, shape, color) and behaviors (e.g., accelerates, brakes, turns) Class Blueprint for instantiating (creating) objects
E N D
Object-Oriented Programming • Object • Abstraction of a read-world item (e.g., car) • Has attributes (e.g., size, shape, color) and behaviors (e.g., accelerates, brakes, turns) • Class • Blueprint for instantiating (creating) objects • Many objects may belong to the same class • User-defined, non-built-in type
The OOP Trilogy • Three important OO principles • Encapsulation • Inheritance • Polymorphism
Encapsulation • Attributes & behaviors encapsulated (wrapped) into objects • Information hiding • Implementation details hidden within objects • You can drive a car without knowing details of how engine, transmission really work • Modularization!
Method • Implements a behavior of object • E.g., car accelerates, brakes, turns • Describes the mechanisms that actually perform its tasks • Hides from its user the complex tasks that it performs • Method call by its user tells method to perform its task
Instance Variable & Property • Together, represent an attribute of object • E.g., size, shape, color of a car • Instance variable • Actually stores an attribute • May not be directly accessible (information hiding) • Property • Provides indirect, controlled (get and set) access to an attribute
Class Declaration • Instance variable • Each object has its own copy • Stores a value • E.g., name • Instance method • Can be called on an object to perform a task • voidindicates method does not return a value • E.g., SetName, GetName
Access modifier • Private • Considered hidden in the class (information hiding) • Accessible in the class only • Cannot be accessed outside the class • Public • Can be accessed outside the class by a user • Available to the public
UML Class Diagram • Three compartments of each class • Name, Attributes (Properties), Behaviors (Methods) • Plus sign indicates public
Instantiating an Object Account myAccount = new Account(); • Instantiating an object referred to as myAccountof class Account • myAccountholds a reference (memory address) of the object
Calling Method on an Object myAccount.SetName(theName); • Calling the SetName method of object myAccount • Telling object to set the name variable
In the Memory name object (anonymous) Jane Green myAccount reference
Property • Provides indirect, controlled access to an attribute, e.g., Name for name • Can be used just like a variable • get accessor method • return an attribute value • set accessor method • Set (update) an attribute with a new value • value is implicitly declared input parameter
Auto-Implemented Properties • Simpler syntax for defining a private instance variable and a public property accessing the instance variable • Cannot be used if the get or set accessor is not trivial
Constructor • Initializes an object of a class • Automatically called when object is instantiated (keyword new) • Has the same name as the class • Has no return type and is not void
Test the Class with Constructor and Auto-implemented Property
Access Modifiers • private variable, property, method • Only accessible in the class • Considered hidden (information hiding) • public variable, property, method • Accessible both inside and outside the class • The interface presented to the outside world
Encapsulation • Information hiding • Implementation details hidden within objects • Instance variables made private • Indirect access to variables through public properties
Advantages of Information Hiding • Create a more robust class • Less likely to malfunction, easier to maintain • Prevent data contamination • Data validation in set accessor of property • E.g., account balance cannot be negative • Freedom of changing internal design • As long as public properties are preserved • E.g., storing account balance in an integer (in number of cents)
Illustrating Information Hiding(Example by Instructor) classAccount { // instance variable changed from decimal to int privateintbalanceInCents; // Balance property is still decimal publicdecimal Balance { get { return (decimal)balanceInCents/100; } privateset { if (value > 0.0m) { balanceInCents = (int)(value * 100); } } }
Value Type vs. Reference Type • Value Type • Simple type • Variable contains a value of that type • bool,char, int, double, decimal, etc • Reference Type • Non-simple type • Class for instantiating objects • Variable contains reference (address) of an object, which must be instantiated with new • Otherwise, default value is null
GradeBook Class publicclassGradeBook { // property to get and set the course name publicstringCourseName {get; set;} // display a welcome message to the GradeBook user publicvoidDisplayMessage() { // use property CourseName to get the // name of the course that this GradeBook represents Console.WriteLine( "Welcome to the grade book for\n{0}!", CourseName ); // display property CourseName } // end method DisplayMessage } // end class GradeBook
Test Value/Reference Types(Example by Instructor) int courseNumber1, courseNumber2, courseNumber3; GradeBook book1, book2, book3; book1 = newGradeBook(); book2 = book1; book3 = newGradeBook(); courseNumber1 = 101; courseNumber2 = 102; courseNumber3 = 103; book1.CourseName = "CS101 Instruction to C#"; book2.CourseName = "CS102 Data Structures in C#"; book3.CourseName = "CS103 Algorithms in C#"; Console.WriteLine($"Course number 1: {courseNumber1}"); Console.WriteLine($"Course number 2: {courseNumber2}"); Console.WriteLine($"Course number 3: {courseNumber3}"); book1.DisplayMessage(); book2.DisplayMessage(); book3.DisplayMessage();
Result Course number 1: 101 Course number 2: 102 Course number 3: 103 Welcome to the grade book for CS102 Data Structures in C#! Welcome to the grade book for CS102 Data Structures in C#! Welcome to the grade book for CS103 Algorithms in C#!
In the Memory (1) int courseNumber1, courseNumber2, courseNumber3; GradeBook book1, book2, book3; null book3 null 3 references book2 null book1 0 courseNumber3 3 values 0 courseNumber2 0 courseNumber1
In the Memory (2) book1 = new GradeBook(); book2 = book1; book3 = new GradeBook(); CourseName (anonymous) null 2 objects CourseName (anonymous) null book3 3 references book2 book1 0 courseNumber3 3 values 0 courseNumber2 0 courseNumber1
In the Memory (3) courseNumber1 = 101; courseNumber2 = 102; courseNumber3 = 103; book1.CourseName = "CS101 Instruction to C#"; book2.CourseName = "CS102 Data Structures in C#"; book3.CourseName = "CS103 Algorithms in C#"; CourseName (anonymous) CS102 2 objects CourseName (anonymous) CS103 book3 3 references book2 book1 103 courseNumber3 3 values 102 courseNumber2 101 courseNumber1
Summary • Class is template for instantiating objects • Methods • Instance variables and properties • Access modifiers • private, public • Encapsulation • Information hiding • Value type vs. reference type