Creating and Using Classes

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Writing Classes (Chapter 4)
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Object-Oriented Programming in C++
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
An Introduction to the Unified Modeling Language
Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Object Oriented Software Development
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Introduction to Object-oriented Programming
Classes (Part 1) Lecture 3
More Sophisticated Behavior
IS 350 Application Structure
Introduction to Classes and Objects
Object-Oriented Analysis and Design
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
About the Presentations
An Introduction to Inheritance
The Object Oriented Approach to Design
Lecture 14 - Abstract Classes
Introduction to Classes and Objects
Object Oriented Analysis and Design
Inheritance Basics Programming with Inheritance
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes Part 1
Lecture 22 Inheritance Richard Gesick.
Classes & Objects: Examples
Slides by Steve Armstrong LeTourneau University Longview, TX
Computer Programming with JAVA
Lecture Set 11 Creating and Using Classes
Defining Classes and Methods
Classes, Objects, Methods and Strings
Fundaments of Game Design
JAVA CLASSES.
Object Oriented Programming in java
Class Diagram.
Final and Abstract Classes
2.1 Introduction to Object-Oriented Programming
Chapter 9 Introduction To Classes
Creating and Using Classes
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Creating and Using Classes Lecture Set 11 Creating and Using Classes Part C – Advanced Topics on Classes - Communication Across Classes, Class Design

Objectives Understanding access rights for different kinds of class variables Variable Accessibility (review) Designing classes (using UML) Informal UML for Class design 11/11/2018 12:14 PM

Access Rights for Class Variables Public variables can be shared between forms (classes) and other assemblies Private variables can only be used by the class containing the declaration Protected variables can only be used by the class containing the declaration AND any class the is derived It is generally NOT a good idea to declare public variables in a class as it defeats the purposes of encapsulation/information hiding 11/11/2018 12:14 PM

User-Defined Classes (review) User-defined classes, in addition to those supported by the .NET Framework class library classes, can be created and reused by the programmer Class features A class has a constructor Classes typically have methods Classes typically have private fields Classes typically have constants Classes often contain definitions of internal data types Classes may have events Classes may have static methods and attributes 11/11/2018 12:14 PM

Introduction to Class Design Encapsulation refers to the coupling of data and the processes that operate on that data Data should only be modified by functions or methods in the class that contains the data Classes have an interface and an implementation The interface consists of the exposed methods, and events The interface is used by other developers The implementation forms the hidden part of the class The implementation is hidden from the developers using the class Classes should be designed to model problem domain data entities or software entities required to solve a problem 11/11/2018 12:14 PM

Introduction to UML Class Diagrams A class can be modeled using a UML class diagram A UML class diagram has three compartments divided by horizontal lines The top compartment contains the class name The middle compartment contains the attributes (properties) The lower compartment contains the operations (methods) 11/11/2018 12:14 PM

UML Class Diagram 11/11/2018 12:14 PM

UML Class Diagram (Analysis) Plus and minus signs denote whether the attribute or operation is public (+) or private (-) Arguments appear in the declaration in indicates an input argument The data type of a method's return value also appears following a full colon (:) 11/11/2018 12:14 PM

UML Class Diagrams (Associations) Associations depict a relationship between two classes Classes can have many associations A person can have bank accounts and other types of accounts, for example A bank can have accounts for many persons Does this remind you of anything from your database course? It should 11/11/2018 12:14 PM

UML Class Diagram with Associations 11/11/2018 12:14 PM

UML Process (Behavior) Diagram (Example) 11/11/2018 12:14 PM

Informal UML Diagrams I find UML Class Diagrams to be too confining for the purposes of this course I prefer instead that we use more detailed versions of UML For lack of a better name, I will call these Unboxed Class Diagrams (UCDs) They are also called Activity Diagrams UCDs (ADs) are similar to UML in that they contain the same descriptive components (and others) Class Name and statement of purpose (contract) Attributes (fields, properties, constants); Exceptions Methods (incl. constructors and non-public methods) New data types; shared methods and data 11/11/2018 12:14 PM

Informal (UML) Class Diagrams Examples of more information UML class diagrams are shown on the next page Note that even given the less formal representations of Class diagrams, UML association diagrams can still be drawn, and all the other features of UML used Examples of informal UML diagrams are shown on the next few slides  11/11/2018 12:14 PM

Informal Class Descriptions Not every class description will contain entries for each of the features Class Name and statement of purpose (contract) Attributes (fields, constants) Exceptions Methods (including constructors and non-public methods) New data types A very important aspect of these informal class diagrams is that they encourage more complete thinking about method arguments and return values These descriptions will be iteratively enhanced as your software development work moves on to more detailed steps 11/11/2018 12:14 PM

More on Constructors Every C# class has a constructor If you do not provide one, the compiler gives you a default constructor You can, AND SHOULD, write your own constructor(s) even if one of them has no arguments and does nothing In C# a constructor is a special public function with the same name as the class A constructor is called automatically when a class instance is created 11/11/2018 12:14 PM

Constructors (continued, with an example) Constructors can accept 0, 1, or many arguments The argument list determines which constructor is called when overloaded constructors are used A constructor without arguments public class BankAccount { private DateTime HiddenDateCreated; public BankAccount() HiddenDateCreated = DateTime.Now; } // end constructor } // end class 11/11/2018 12:14 PM

Constructor (Example, continued) A constructor with arguments public class BankAccount { private DateTime HiddenDateCreated; private int HiddenAccountNumber; public BankAccount(int accountNum) HiddenDateCreated = DateTime.Now; HiddenAccountNumber = accountNum; } // end Constructor } // end class 11/11/2018 12:14 PM

Calling a Constructor (VB code) 11/11/2018 12:14 PM

Creating Methods Classes have a name, attributes, and operations Operations are called methods Methods are implemented as public functions Note that Private functions are part of the implementation of the class and are not considered methods that are part of the class interface 11/11/2018 12:14 PM

Creating Methods (Example) Create a method named MakeWithdrawal to decrease the account balance public class BankAccount { private double HiddenAccountBalance; public double MakeWithdrawal(double amount) HiddenAccountBalance -= amount; } // end MakeWithdrawal } // end Class 11/11/2018 12:14 PM