COP3804 - INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)

Slides:



Advertisements
Similar presentations
Basic -2 Classes and Objects. Classes and Objects A class is a complex data TYPE An object is an instance of a class. Example: Class: Person Objects:
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COP INTERMEDIATE JAVA Review of COP2250 Concepts.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
COP INTERMEDIATE JAVA Review of COP2250 Concepts.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
COP INTERMEDIATE JAVA Recursion. The term “recursion” refers to the fact that the same computation recurs, or occurs repeatedly, as a problem is.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Topics Instance variables, set and get methods Encapsulation
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.
Class Fundamentals BCIS 3680 Enterprise Programming.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
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.
Topic: Classes and Objects
Creating Your Own Classes
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 8 Classes User-Defined Classes and ADTs
Outline Anatomy of a Class Encapsulation Anatomy of a Method
JAVA CLASSES.
Classes and Objects CGS3416 Spring 2019.
Chapter 7 Objects and Classes
Presentation transcript:

COP INTERMEDIATE JAVA Designing Classes

Class Template or blueprint for creating objects. Their definition includes the list of properties (fields) and behavior (methods) that the objects created from it will have. Class Declaration Syntax: accessSpecifier class ClassName { variables (instance or class) constants constructors methods } accessSpecifier may be public (visible from any class) or not specified, in which case it’s visible from within the package.

Object Software bundle of related state (fields) and behavior (methods). They get all the properties and behavior that were defined in the class from which they were created. They are created using constructors. Syntax to create new objects: new ClassName(parameters)

Instance Variable Declaration Instance variables, also known as non-static fields, store values that are unique to each instance of a class. Declaration syntax: accessModifier dataType variableName Example: private String letterGrade; Access modifiers determine what classes can access the field: public: it can be accessed from all classes. protected: it can be accessed within the same package and sub-classes. No modifier: it can be accessed within the same package. private: it can only be accessed within its own class. They are usually declared private. Then, public methods may be provided to set and get the value of these fields.

Data Encapsulation The process of hiding the internal state of objects by making its fields private and requiring all interaction to be performed through the object's public methods. This way allows for the methods to perform data validation before changing a field.

Static Variable Declaration Static variables are also called class variables. There is only one copy of this variable to be shared among all objects of the class. They are declared using the static keyword. Declaration syntax: accessModifier static dataType variableName Example: public static double interestRate = 5;

Instance vs. Static Variables public class BankAccount { private double balance; private int accountNumber; private static int lastAssignedNumber = 1000; }

Instance vs. Static Variables

Class Constants Values that do not change throughout program execution. Identified with the reserved word final. They may be declared as public since they cannot be modified. It makes sense to make them static so that there is a single copy for all objects. Declaration syntax: accessModifier static final dataType CONSTANT_NAME Example: public static final int VALUE_OF_A = 4;

Constructors Used to create objects from the class blueprint and return their address in memory. Their declaration looks like a method declaration but they use the name of the class and have no return type. Their main objective is to initialize the fields of an object. Syntax: new ClassName(parameters) Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

Constructors If a class declaration does not include any constructor, Java automatically provides a default, no-argument constructor. The default constructor sets all numeric fields to 0, boolean fields to false, and reference fields to null. Constructors can be overloaded, meaning, there could be multiple constructors as long as their signature is different. note: signature refers to the number and data type of the parameters

Methods Methods can manipulate the internal data of an object (its fields). Syntax of a method implementation: accessSpecifier returnType methodName(parameters) { // body of the method } Methods can be accessors, used to access the object’s fields without changing their value, or mutators, used to change the value of an object’s fields.

Syntax of a Method Implementation accessSpecifier returnType methodName(parameter list) { // body of the method } Examples: public String getLetterGrade() { return letterGrade; } private String toProperCase(String str) { … }

Accessor vs. Mutator Methods Accessor methods are those class methods that return the value of the fields without changing them. Mutator methods are those class methods that change the value of fields. It is common practice to make all the class fields private and provide methods to get their values and methods to set their values. Sometimes, accessor methods are called “getters” and mutator methods are called “setters”.

Parameter vs. Argument Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.

Call by value vs. call by reference Parameter variables come into existence when the method starts getting executed and they cease to exist when the method finishes. As the method starts, the parameter variable is set to the same value as the corresponding argument. If the parameter variable gets modified inside the method, that has no effect on the argument because they are separate variables. If the argument is a reference to an object, then mutator methods may be used inside the method to modify the state of the object.

Commonly Included Methods Many classes provide an implementation for the following methods: toString - it returns a String representing the state of an object (the data stored in the object’s fields). It gets called implicitly when an object is passed as an argument to print or println, or when using the concatenation operator. Providing an implementation for this method overrides the one defined in the Object class. equals – compares the contents of two objects of the same class: the object calling the method and the object being passed as an argument. Providing an implementation for this method overrides the one defined in the Object class. copy – it creates a new object and sets the fields to the same values as the ones in the object calling the method.

Method and Constructor Overloading Some classes provide several constructors with different parameter lists. Also, several methods with the same name may be provided, as long as they have a different parameter list. This concept is called overloading and it makes classes more flexible in the sense that they give the user of the class more options on how to call the overloaded methods or constructors.

Shallow Copy vs. Deep Copy

Packages Used to organize related classes and interfaces. Classes that are declared in other packages need to be imported. Syntax to import a class: import packageName.ClassName;

Garbage Collection The Java platform allows you to create objects without having to worry about destroying them when you no longer need them. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

References Horstmann, Cay. Big Java 4th ed. New York, USA: John Wiley & Sons, Inc., Oracle. The Java Tutorials, Web. 25 Aug Gaddis, Tony, and Godfrey Muganda. Starting out with Java: from Control Structures through Data Structures 2 nd ed. Boston, USA: Addison-Wesley, 2012