Download presentation
Presentation is loading. Please wait.
1
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Basic Classes Phil Tayco Slide version 1.0 Created Sep 18, 2017
2
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; }
3
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.
4
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?
5
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”
6
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
7
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)
8
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
9
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
10
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
11
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)
12
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
13
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
14
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
15
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…?
16
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(…”)
17
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?
18
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
19
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?
20
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?
21
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?
22
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
23
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
24
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…
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.