Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining Your Own Classes Part 1

Similar presentations


Presentation on theme: "Defining Your Own Classes Part 1"— Presentation transcript:

1 Defining Your Own Classes Part 1
SE-1010 Dr. Mark L. Hornick

2 CS-1010 11/19/2018 You’ve been using classes defined by others, like String and WinPlotter No pre-written class will meet all of your needs. You need to be able to define our own classes customized for your applications. Larger programs are typically composed of many classes defined by you, the developer The sample application programs we have written so far included only one class, the main class of the program. And the main class contained only one method, the main method. When we review the sample programs developed so far, we note two key characteristics: The programs included only one class, the main class of the program. The main class contained only one method, the main method. From this main method, we used objects and classes from the standard packages such as javax.swing and java.util . Such organization works only for small programs. In order to manage complexity of large programs, it is best to define instantiable classes. SE-1010 Dr. Mark L. Hornick Dr. Mark L. Hornick

3 You’ve actually written your own class already
CS-1010 11/19/2018 You’ve actually written your own class already Every Java application has a main class containing a main() method Remember attributes? A class definition may include zero or more data attributes of any datatype: Primitives, like byte, short, int, long, float, double… …or Scanner, String, etc. The sample application programs we have written so far included only one class, the main class of the program. And the main class contained only one method, the main method. When we review the sample programs developed so far, we note two key characteristics: The programs included only one class, the main class of the program. The main class contained only one method, the main method. From this main method, we used objects and classes from the standard packages such as javax.swing and java.util . Such organization works only for small programs. In order to manage complexity of large programs, it is best to define instantiable classes. Data attributes are also called data members SE-1010 Dr. Mark L. Hornick Dr. Mark L. Hornick

4 A class example: BankAccount
class BankAccount { // class variable; shared by all instances private static double interestRate; // instance variable; diff. value for each object private double balance; … } Hey! What is static all about? What does public/private mean here? SE-1010 Dr. Mark L. Hornick

5 Creating instances of BankAccounts
In our main class’s main() method: class ClassDemoApp{ public static void main(String[] args) { BankAccount myCheckingAcct = new BankAccount(); BankAccount mySavingsAcct = new BankAccount(); … } SE-1010 Dr. Mark L. Hornick

6 Class Data is shared by all objects; Instance Data is unique to each object
All BankAccount objects share the same value for interestRate, which is the same for every account Can be stored as a common class data value Static means data is shared among all instances of a class However, balance is not shared -- it is unique to each BankAccount instance Should be stored as a instance data value (not static) SE-1010 Dr. Mark L. Hornick

7 We use static variables because duplication of identical data is inefficient
If interestRate is not static, then all BankAccount objects duplicate the same information (interestRate= 5.0). SE-1010 Dr. Mark L. Hornick

8 There are several types of data attributes
Class variables The static modifier designates a data attribute/member to be a class variable i.e. the same variable is shared by all instances of the class Class constants The static final modifiers designate a data member to be a class constant The value cannot change after initialization Instance variables Otherwise, the data member is an instance variable Every object of a class has it’s own copy of an instance variable – values are not shared SE-1010 Dr. Mark L. Hornick

9 Public/Private: Information Hiding and Visibility Modifiers
The modifiers public, private, and protected designate the accessibility of data members (and methods). If a class data member is declared private… Only methods within the class can access the data It cannot be accessed by an “outside” class; it is hidden or invisible to the outside SE-1010 Dr. Mark L. Hornick

10 CS-1020 11/19/2018 However, any data members accessible from within an instance are also accessible from other instances of the same class Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick

11 What about protected? What if I forget to use an access modifier?
If a class component (data member or method) is declared protected… Only methods in the same class or in a derived class can access the component Derived class: wait until SE1021 It cannot be accessed by an “outside” class If a class component is missing an access declaration, it can be accessed from any class in the same package. SE-1010 Dr. Mark L. Hornick

12 Access modifiers – UML Syntax
The + and - signs designate public and private attributes or methods The # sign designates a protected attribute or method A method or attribute that is underlined indicates that it is static SE-1010 Dr. Mark L. Hornick

13 How do you use access modifiers?
Public methods of a class determine the behavior of its instances, as perceived by a user of the class. Internal details are implemented by private methods and private data members. Declaring the data members private ensures the integrity of the class. No “outside” code can modify an object’s data directly SE-1010 Dr. Mark L. Hornick

14 Are there exceptions? Class constants may be declared public because:
A constant is “read only” by nature A constant is a clean way to make characteristics of the instances known to users of the class. Public class data members are accessed by the syntax <class name>.<class data members> Example: Color.BLUE SE-1010 Dr. Mark L. Hornick

15 So how do you access private data from the “outside”?
You have to provide public methods that you can call from the “outside” Public methods that let you “read” a value are called Accessors because they let you access private data public float getCurrentBalance() { return currentValue; } Aka “getters” because they begin with the verb “get” Public methods that let you “write” a value are called Mutators because they let you change private data public void setCurrentBalance( float value ) { currentValue = value; } Aka “setters” because they begin with the verb “set” SE-1010 Dr. Mark L. Hornick

16 Methods are used to contain Java instructions that perform a specific function
A way of grouping related Java instructions together A technique for organizing your program into a logical structure A way to keep methods from getting too long (doing too much) The words subroutine and function are alternate, older (deprecated) terms for method that were used in the days before Object-oriented programming. SE-1010 Dr. Mark L. Hornick

17 We already know a little about calling other class’s methods:
WinPlotter plotter = new WinPlotter(); plotter.moveTo(100, 100); String string1 = “abcdefg”; String uppercaseStr1; uppercaseStr1 = string1.toUpperCase(); String and WinPlotter are class identifiers. plotter, string1 and uppercaseStr1 are all object identifiers. moveTo is a methods of the WinPlotter class; toUpperCase is a method of the String class Actual arguments are passed to a method within the parentheses of a method invocation. Literal values (like “hello”) or identifiers (like string1, that reference values) can be passed to a method within the parenthesis. A method may not take any arguments at all, like in toUpper(). If a method returns a value as a result of being called, that value can be “captured” by assigning it to an object identifier. SE-1010 Dr. Mark L. Hornick

18 This is the Java syntax for defining our own methods within our class:
We need to give our method a name, which should be a verb or verb phrase, since a method implies an action. The first letter is lowercase. If our method expects actual arguments to be supplied when it is called, we have to specify each argument’s specific datatype. We also specify identifiers that the method will use locally (within the method) to represent the values supplied when the method is called. These identifiers are called the formal arguments. public class BankAccount{ private double balance; // other attributes not shown because of space limitations public void deposit( double amount) { balance += amount; } } SE-1010 Dr. Mark L. Hornick

19 When you call a method that declares formal arguments, you pass actual arguments to the method that indicates what value the formal argument should have for that call. public class MyApp { public static void main(String args[]) { BankAccount myAccount = new BankAccount(); myAccount.deposit(100.0); } The datatype of the actual argument must be compatible with the datatype of the matching formal argument public class BankAccount{ private double balance; // other attributes not shown because of space limitations public void deposit( double amount) { balance += amount; } } SE-1010 Dr. Mark L. Hornick

20 CS-1010 11/19/2018 A method (like an Accessor) that returns a value is called a value-returning method, or non-void method A value-returning method must include a return statement in the following format: public double getCurrentBalance() { … return balance; } SE-1010 Dr. Mark L. Hornick Dr. Mark L. Hornick


Download ppt "Defining Your Own Classes Part 1"

Similar presentations


Ads by Google