Defining Your Own Classes Part 1

Slides:



Advertisements
Similar presentations
DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
©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.
Road Map Introduction to object oriented programming. Classes
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
UML Review – class diagrams SE 2030 Dr. Rob Hasker 1 Based on slides written by Dr. Mark L. Hornick Used with permission.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
OOP with Java, David J. Barnes/Eric Jul Defining Classes1 Object State and Complexity Objects maintain a state. State is represented by a set of attributes.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Topics Instance variables, set and get methods Encapsulation
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
UML Review – class diagrams SE-2030 Dr. Mark L. Hornick 1.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Chapter 7 User-Defined Methods.
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Java Primer 1: Types, Classes and Operators
University of Central Florida COP 3330 Object Oriented Programming
Lecture 14 Writing Classes part 2 Richard Gesick.
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.
Chapter 4 Defining Instantiable Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Defining Your Own Classes
Encapsulation and Constructors
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Programs and Classes A program is made up from classes
JAVA CLASSES.
Object Oriented Programming in java
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Java Basics Data Types in Java.
CSG2H3 Object Oriented Programming
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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