CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

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:
Fields, Constructors, Methods
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
Written by: Dr. JJ Shepherd
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Road Map Introduction to object oriented programming. Classes
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
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.
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.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
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
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
ECE122 Feb. 22, Any question on Vehicle sample code?
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
Classes, Interfaces and Packages
Written by: Dr. JJ Shepherd
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
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.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
GC211 Data structure Lecture 3 Sara Alhajjam.
JAVA MULTIPLE CHOICE QUESTION.
Haidong Xue Summer 2011, at GSU
Java Primer 1: Types, Classes and Operators
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Chapter 4: Writing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
JAVA CLASSES.
Chap 2. Identifiers, Keywords, and Types
Chapter 4 Constructors Section 4.4
Chapter 7 Objects and Classes
Chapter 5 Classes.
Presentation transcript:

CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1

Existing Data Types Declare a variable: 2 LeMoyne-Owen College

Create your own data type 3 LeMoyne-Owen College

Class & Objects Class – blueprint e.g., Course Object – house e.g., JAVAI, JAVAII, Data Structures,…etc. Characteristic of an object Instructor, number of seats,…etc. Behavior Change instructor, increase number of seats,…etc. LeMoyne-Owen College 4

5

Class & Objects (cont.) Characteristic – Instance Variable A storage – not take any action, no (). private – only methods within the same class can view or change it. Data is protected. – encapsulation. data type – to know how large of storage to be reserved. variable name – no space (one word only) e.g., numOfStudents. LeMoyne-Owen College 6

Class & Objects (cont.) Behavior – Method or Constructor Take an action – () is needed. public – client (main method) can call it. Interface between client and methods. return data type – what type information the method return to caller? void - no return information. parameter – information needed from caller. - Can be only used within the block of the method, it located. - data type is needed. LeMoyne-Owen College 7

Constructor A special method Take an action : Constructor’s job: Initialize objects; i.e. initialize instance variables. Be called when a new object of the class is declared. Has the same name as the class name. No return data type. Number of parameters usually, not always, is same as number of instance variables. public LeMoyne-Owen College 8

Declare a class 9 LeMoyne-Owen College

10 LeMoyne-Owen College

11 LeMoyne-Owen College

12 LeMoyne-Owen College

Default constructor If a programmer did not set any constructor, the Java compiler has a default constructor with no parameters. Set zero for primitive number types. Set false for boolean values Set null for references (e.g. String type) 13 LeMoyne-Owen College

Example 14 LeMoyne-Owen College

What if a constructor is set? 15 LeMoyne-Owen College

16 LeMoyne-Owen College

What if a constructor is set? (cont.) 17 LeMoyne-Owen College

What if a constructor is set?(Cont.) A constructor is created in a class. Default constructor is turned off. The no-argument constructor is required if a client creates an object with no initial values. Constructor Overload 18 LeMoyne-Owen College

19 Constructor Overload LeMoyne-Owen College

20 LeMoyne-Owen College

21 LeMoyne-Owen College

22 LeMoyne-Owen College

23 LeMoyne-Owen College

Compile and run javac *.java java CourseTest 24 LeMoyne-Owen College

Compare two objects 25 LeMoyne-Owen College