The class definition is not a program by itself. It can be used by other programs in order to create objects and use them.

Similar documents
Java OOP (SE Tutorials: Learning the Java Language Trail : Object-Oriented Programming Concepts Lesson )

ENCAPSULATION AND POLYMORPHISM

* To change this license header, choose License Headers in Project Properties.

Chapter 7. Inheritance

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Java is an Object Oriented Language. As a language that has the Object Oriented feature Java supports the following fundamental concepts:

Classes and Objects. CGS 3416 Spring 2018

UCLA PIC 20A Java Programming

Introduction and Review of Java: Part 2

Chapter 4. Defining Classes I

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Encapsulation in C++

CSCI 201L Midterm Written Summer % of course grade

drawobject Circle draw

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

SampleApp.java. Page 1

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Friend Functions and Friend Classes

Object-Oriented Programming Paradigm

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Graphical User Interface

Chapter No. 2 Class modeling CO:-Sketch Class,object models using fundamental relationships Contents 2.1 Object and Class Concepts (12M) Objects,

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

The main form for the application is shown in the following Form Designer image:

Notes on Chapter Three

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Allenhouse Institute of Technology (UPTU Code : 505) OOT Notes By Hammad Lari for B.Tech CSE V th Sem

CHAPTER 7 OBJECTS AND CLASSES

CSCI 201L Midterm Written SOLUTION Summer % of course grade

For example, when we first started Java programming we described the HelloWorld class:

Chapter 15: Object Oriented Programming

Object-oriented. Service Innovation

BBM 102 Introduction to Programming II Spring 2017

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Object-Oriented Programming

Classes. Classes as Code Libraries. Classes as Data Structures

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Chapter 4 Defining Classes I

Object oriented programming Concepts

CMP 326 Midterm Fall 2015

Lecture 06: Classes and Objects

GUI Forms and Events, Part II

Principles of Object Oriented Programming. Lecture 4

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Software Architecture (Lesson 2) Object-Oriented Paradigm (1)

CS 302: Introduction to Programming in Java. Lecture 15

Java Swing Introduction

CST141 Thinking in Objects Page 1

Answer on question #61311, Programming & Computer Science / Java

Super-Classes and sub-classes

Encapsulation in Java

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Working with Objects. Overview. This chapter covers. ! Overview! Properties and Fields! Initialization! Constructors! Assignment

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

Classes Basic Overview

CSC 1214: Object-Oriented Programming

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Lecture 5: Methods CS2301

Object Oriented Programming

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

CS 251 Intermediate Programming Methods and Classes

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

CS 251 Intermediate Programming Methods and More

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

ECOM 2324 COMPUTER PROGRAMMING II

Programming II (CS300)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Chapter 8 Objects and Classes

Programming Languages (Outsource: 2-2)

SINGLE EVENT HANDLING

I.1 Introduction Matisse GUI designer I.2 GroupLayout Basics Sequential and Parallel Arrangements sequential horizontal orientation

Overview. Lab 5 Methods and Parameters

CHAPTER 7 OBJECTS AND CLASSES

Basic Object-Oriented Concepts. 5-Oct-17

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

The return Statement

Object Oriented Programming

Object-Oriented Programming in Java. Topic : Objects and Classes (cont) Object Oriented Design

Object Oriented Programming in C#

Programming II (CS300)

JAVA: A Primer. By: Amrita Rajagopal

1.00 Lecture 8. Using An Existing Class, cont.

CS 2530 INTERMEDIATE COMPUTING

Programming II (CS300)

MIDTERM REVIEW. midterminformation.htm

Object-Oriented Programming (Java)

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Some miscellaneous concepts

Classes and Objects. COMP1400/INFS1609 Week 8. Monday, 10 September 12

Chapter 5: Classes and Objects in Depth. Introduction to methods

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Transcription:

Data Classes and Object-Oriented Programming Data classes can be motivated by the need to create data structures that have grouped together a number of variables of simpler type (ints, Strings, arrays) in order to represent the structure of a complex real-world object. Object-oriented languages go further in associating with the data structures the operations that can represent what behaviors we want to model. Here is a quote from the Sun Java tutorial: Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming. In Java, the data class is used to provide the definition of how to represent the state of an object in terms of a group of variables and to provide methods that define how the behaviors of the object will occur. Each class can be considered as a template and that objects are created by creating an instance of the template. Each instance can have the data that represents one object of the class. Note that there are classes not used as data classes, but primarily for computation. For example, a class may have a main method that is used to start the computation. It is expected that there will only be one instance of this method and it is declared as static. Class Definitions The class definition is not a program by itself. It can be used by other programs in order to create objects and use them. Diagram of a class definition: public class <classname> { <fields> <constructor functions> <methods> The fields are declarations of variables that represent the class. These variables can have any valid type. The constructor function is used to create an instance of the class, and may also initialize the field variables. Calling this function allocates the instance. The other methods can operate on the fields in order to implement whatever operations need to be done with this class.

Here is one implementation of an example representing the employees of a company: class Employee { // fields String empname; double salary; String hiredate; // constructor function creates and instance and initializes fields public Employee (String startname, double startsalary, String starthiredate){ empname = startname ; salary = startsalary; hiredate = starthiredate; // other methods to provide functionality void raisesalary(double rate) { salary = salary * (salary * rate); double getsalary() { return salary; String gethiredate() { return hiredate; void setempname(string newname) { empname = newname; void printemployee() { System.out.println("Employee Name:"+empName+ " salary:"+salary+" hire date:"+hiredate); Note that the methods of the class just refer directly to the field variables as they would for any other variables in any type of class.

Using a Class Since a class definition gives a template of a number of fields and methods, a program that wants to use objects of the class would create one or more instances of the class. To create an instance, declare a variable that has the <classname> as its type, and uses the new keyword with the constructor method and any parameters that the constructor method needs to do initialization. <ClassName> <varname> = new <ClassName> (<param1>,..., <paramn>) Example: Employee emp1 = new Employee ( Carl Worker, 35000.0, Jan2005 ); This statement will allocate memory for an instance of the Employee class that will contain a memory allocation for each of the field variables and initialize the employee name to Carl Worker, the salary to 35000.0, and the hire date to Jan2005:. Once a class instance has been created in a variable, then the dot notation is used to use the fields and methods in the class. <varname>. <fieldname> <varname>. <methodname> Here is an example program that creates two instances of the Employee class and uses the fields and methods. class EmployeeDemo { public static void main(string[] args) { // Create two different Bicycle objects Employee emp1 = new Employee ( Carl Worker, 35000.0, Jan2005 ); Employee emp2 = new Employee ( Barbara Boss, 70000.0, Jan1998 ); // Invoke methods on those objects emp1.raisesalary(.10); emp1.printemployee(); emp2.raisesalary(.03);; emp2.setempname ( Barbara Harris ); emp2.printemployee();

Other examples of using classes In Java, all of the built-in programs and data come from classes and are described in the Java API (Application Programming Interface). Some examples that we have used so far include the GUI components that we use to make a form design. But in these cases, NetBeans helps by generating some of the code for us. So when we drag components onto the form, NetBeans generates code that creates instances of the classes representing those components. buttongroup1 = new javax.swing.buttongroup(); jlabel1 = new javax.swing.jlabel(); jlabel2 = new javax.swing.jlabel(); jbutton1 = new javax.swing.jbutton(); jradiobutton1 = new javax.swing.jradiobutton(); jradiobutton2 = new javax.swing.jradiobutton(); jradiobutton3 = new javax.swing.jradiobutton(); jtextfield1 = new javax.swing.jtextfield(); jtextfield2 = new javax.swing.jtextfield(); NetBeans also automatically generates code to change the size and appearance of the components and other things like: buttongroup1.add(jradiobutton1); Then when we write the code for the button actionperformed method, we can also use methods for the components. For example, we can get and set the text appearing in a label or textfield: String pername = jtextfield1.gettext(); jlabel1.settext ( Name + pername); There are some methods that can be used without creating an instance of the class. These methods are declared to be static methods, and they don t use any one instance of the class. For example, when we convert a String to an int, the method parseint is static and is used just by giving the name of the class, Integer: Integer.parseInt(String x) And the Math class provides a number of mathematical functions as static methods, for example, Math.pow(

Data Encapsulation When designing a program, it is important to design a class for each type of data object and to use methods to code every operation on objects of the class. This accomplishes two important principles: Modularity: Conceptually all the details of using the class are defined by what methods to call, making each piece of the program separated into its own class. Data Hiding: Other parts of the program should not use the fields directly; only use them through the methods. This means that the class can be given a different implementation and the programs using it will not have to be changed. As a result of these principles, it is recommended that most fields of a class be made private, unless it really is a field that should be publicly accessible. This means that these fields cannot be accessed directly from another class using the dot notation. For example, in the Employee class: // fields private String empname; private double salary; private String hiredate; Other programs can no longer use emp1.salary, for example. If other programs need to get the value of the field, an accessor get method is given. An accessor method already given is public double getsalary() { return salary; And if other programs need to assign the value of the field, a set method is given. For example, public void setempname(string newname) { empname = newname;