Phil Tayco Slide version 1.0 Created Sep 18, 2017

Slides:



Advertisements
Similar presentations
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Lists Introduction to Computing Science and Programming I.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Writing Classes (Chapter 4)
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Introduction to Object-Oriented Programming Lesson 2.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Class Fundamentals BCIS 3680 Enterprise Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Programming Logic and Design Seventh Edition
Phil Tayco Slide version 1.0 Created Sep 10, 2017
Classes (Part 1) Lecture 3
Classes and Objects.
Programming Logic and Design Seventh Edition
Static data members Constructors and Destructors
Java Programming: Guided Learning with Early Objects
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Phil Tayco Slide version 1.0 Created Nov 6, 2017
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Classes.
Creating Your OwnClasses
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Classes and Objects 2nd Lecture
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Classes and Objects: Encapsulation
Comparing Objects Phil Tayco San Jose City College Slide version 1.1
Composition Phil Tayco San Jose City College Slide version 1.2
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Phil Tayco Slide version 1.0 Created Oct 16, 2017
Object Oriented Programming COP3330 / CGS5409
Introduction to Classes
Defining Your Own Classes Part 1
Classes and Objects Encapsulation
Stacks.
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Polymorphism Phil Tayco San Jose City College Slide version 1.1
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Classes, Encapsulation, Methods and Constructors (Continued)
Java Programming Arrays
Arrays in Java What, why and how Copyright Curt Hill.
Java Programming Function Introduction
Java Programming Control Structures Part 1
Stacks.
Classes and Objects Phil Tayco San Jose City College Slide version 1.1
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CIS16 Application Development and Programming using Visual Basic.net
Inheritance Phil Tayco San Jose City College Slide version 1.2
Object Comparisons and Arrays
Object-Oriented Programming
Defining Classes and Methods
Defining Classes and Methods
Java Programming Function Introduction
CMSC 202 Encapsulation Version 9/10.
Lecture 8 Object Oriented Programming (OOP)
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
(4 – 2) Introduction to Classes in C++
Creating and Using Classes
CSG2H3 Object Oriented Programming
Phil Tayco San Jose City College Slide version 1.2 Updated Sep 9, 2019
Presentation transcript:

Phil Tayco Slide version 1.0 Created Sep 18, 2017 Basic Classes Phil Tayco Slide version 1.0 Created Sep 18, 2017

Accessing Properties When a class is designed with the data members public, a binding occurs between the code creating the object of that class and the class’ design Class example: public class Student { public String firstName; public String lastName; public int age; public int id; public double gpa; }

Accessing Properties Using that class, objects in other programs set property values directly: Student example = new Student(); example.firstName = “John”; example.id = 123; This makes the object and class code somewhat dependent on each other Changes in code on the use of the object are ok as they won’t affect the class code design – whether or not you use a property, the class code is unaware of its use More importantly, the class code shouldn’t be aware how it’s used! The class is providing capabilities to object creators.

Accessing Properties This relationship can be referred to as “high coupling” or “weak encapsulation” Weak encapsulation means that the property information is not hidden because it is available for all to use (they are public) What are some problems with this? If the code changes on the object side, we already saw that doesn’t affect the class code design What about the other way? If we change any part of the design, does that affect the object code?

Accessing Properties Absolutely! public class Student { public String first_name; public String lastName; public int age; public String id; public double gpa; } These simple code changes affects the example object example.firstName has to change to example.first_name example.id = 123 has to change to example.id = “123”

Accessing Properties In small amounts of code, this can be manageable, but as programs get larger and object use increases, any change in class code design has more downstream effects Property usage is also a highly performed activity – programs are fundamentally designed to manipulate the variable values and make logical decisions from them It is also hard to predict what complex programs actually need as they evolve (why do we have different versions of Windows? iOS? Microsoft Office?) Classes are also supposed to be stand-alone. New programs using the class may have different needs than as the class originally was designed As requirements change, data types used will evolve

Strong Encapsulation Because class properties have such high usage and evolution, another approach is to hide that information from other programs public class Student { private String firstName; private String lastName; private int age; private int id; private double gpa; } This now means any object created as type Student cannot directly access the properties (example.id = 123 would result in a compile error)

Strong Encapsulation How are property values manipulated if they are marked as private? In order to see how that is typically done, we must first discuss another aspect to all class designs All classes have properties that define “what objects of that class type will have” In addition, we can define “behaviors” that define “what objects of that class type can do” These behaviors are also referred to as “methods” and they define actions

Class Methods We’ve already seen code that define actions such as “calculateArea” or “println” These actions are defined in the form of a function. Recall that a function is defined with 3 parts: function name, return type and input parameters public double calculateArea(double length, double width) { return length * width; } This function is defined with the name “calculateArea”, a return type of “double” and has 2 input parameters that are each “double” data types

Class Methods We call the definition of a function a “function signature” and all signatures have these 3 elements Within the function contains the code for performing the overall action it is intended to define We learned that such code is also designed to be stand-alone and hidden. This means whomever calls the function with their code, they don’t need to know what the code actually does (nor should they!) – they only care that it performs as designed We can take advantage of this by defining functions in classes designed to handle property value usage

Class Methods public class Student { private String firstName; public void setFirstName(String f) firstName = f; } In this example, the method signature is “setFirstName”. It takes a String as an input parameter and returns nothing (return type is void) Within the function, “f” is a local String variable initialized by the input value where the function was called The goal of this method is to set the firstName property to a given value (stored in f)

Class Methods Typical questions arise from this approach How is this function called? Where and how is “firstName” used? Remember that these functions live within the context of a class A class is used when an object is instantiated of its type Once an object is created, anything defined as public within the class design is usable and will apply to that specific object

Class Methods Student me = new Student(); me.setFirstName(“John”); In this example, an object “me” is created of type Student The only thing defined as public in the Student class is the “setFirstName” method When the method is used, it will be in the context of a created object – this means in this case, the setFirstName method will be applied to the “me” object Since the method is a public function, we use it like a function call, sending in a String and not expecting a return value because of the way the method signature is defined

Class Methods When the method is called, “John” comes in as an input value that will be assigned to the “f” variable in the setFirstName function public void setFirstName(String f) { firstName = f; } Now the important part: the only line of code in the method is “firstName = f;”. Where is firstName defined? Since it is not defined within the function, we have to look outside the function but still within the code module the function is defined

Class Methods That code module is the class itself! public class Student { private String firstName; public void setFirstName(String f) firstName = f; } That code module is the class itself! This means firstName is accessible to all functions defined within the class Now note that firstName is treated like a variable – we call it a class property, but it is essentially a variable By itself, firstName is definition. It is not a physical variable, until when…?

Class Methods …when an instance of the class is created! “firstName” doesn’t become a physical element in memory until “Student me = new Student();” was created Once that object was instantiated, me.firstName = “John”; could be done to assign “John” to the firstName property of the “me” instance But remember, we made firstName private, so that line of code is not allowed setFirstName is a method that is public, so we will use that By calling the function setFirstName(“John”): “John” is used in the function (through f) f is assigned to the variable firstName firstName represents the property of the me object (because we called it with “me.setFirstName(…”)

Class Methods This approach can be used with all class properties public void setGpa(double g) { gpa = g; } Some questions to note: “gpa” property name is part of the function name “setGpa” – what would the function name be for the id or lastName properties? The input parameter type is “double” which matches the gpa property data type definition. What would the input parameter types be for id and lastName? Why is the return type void? Could the return type be something else? If so, what could it signify?

Class Methods This approach promotes code reuse as well. If we create multiple objects, the same code is used within the context of each object: Student one = new Student(); Student two = new Student(); one.setFirstName(“Susan”); two.setFirstName(“Alex”); One and two are physically two separate objects with their own property values Both are using the same code instructions to set their individual firstName properties respectively These particular methods are frequently used and commonly referred to as “setters” or “mutators” – they are functions designed to change object property values

Class Methods Could you create set methods that change more than one property at a time? Yes! public void setName(String f, String l) { firstName = f; lastName = l; } What are pros/cons to doing so? Remember, any function created doesn’t always have to be used… Mutators are needed for changing object property values. What about when we need to view the data?

Accessing Data Previously, we could look at an object’s property value at any time directly: System.out.println(me.firstName); However, the firstName property is now private We need another way to retrieve that value Put another way, we need a “method” that is designed to “return” the firstName value to us This leads to designing another function. Based on the information here, what would the method signature for seeing the object’s firstName value?

Accessing Data Remember the 3 parts to the function signature: name, input parameter types and return type public String getFirstName() { return firstName; } The name of this function (which also states its purpose) is appropriately entitled “getFirstName” The return type is a String meaning that when the function is called, we will get what represents the firstName of the Student object as a String Why are there no input parameters?

Accessing Data Just like with the set methods, these get methods are designed to be used for each class property Because the value coming back is through a return value of a function, the get methods are designed to return one property value The return type in this example is a String which matches the definition of firstName in the class. This usually is the case, but remember: The class is encapsulating the definition of the firstName property. This means we can change the name and the type at any time (what would we have to change if we did that?) There are usually no input parameters with get methods because their job is to return existing (and known) information. No new information from the object method call is needed to perform this action

Setters and Getters Like setters/mutators, these functions are commonly referred to as “getters” or “accessors” You typically see a get and a set function for each property (a class with 4 properties will usually have 4 accessors and 4 mutators) Their function signatures usually follow a pattern based on the property name and its data type Some classes may not want to have certain accessors/mutators defined – this will depend on the class designer’s intent of use Any number of setter functions can occur. Typically you see one get function per class property

Exercise 2 Modify your class from exercise 1 as follows: Add one new property to the class Modify one of the existing properties to a different name or data type Make all properties private Create a set/get method for each property Modify the main program from exercise 1 such that the objects you created use these set/get methods When finished, ask yourself how would your code change if you changed the name of a class property? What if you changed the data type of a class property? What if you removed a data type? A key learning from this is the amount of code changes occur when class code is updated…