Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.

Slides:



Advertisements
Similar presentations
Web Application Development Slides Credit Umair Javed LUMS.
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Lecture 9 Concepts of Programming Languages
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CIT241 Prerequisite Knowledge ◦ Variables ◦ Operators ◦ C++ Syntax ◦ Program Structure ◦ Classes  Basic Structure of a class  Concept of Data Hiding.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
OOP Languages: Java vs C++
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.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Chapter 4 Objects and Classes.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
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.
Object-Oriented Programming Chapter Chapter
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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.
ISBN Object-Oriented Programming Chapter Chapter
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
CSCI 171 Presentation 15 Introduction to Object–Oriented Programming (OOP) in C++
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java Primer 1: Types, Classes and Operators
Class: Special Topics Copy Constructors Static members Friends this
Creating Your OwnClasses
Lecture 9 Concepts of Programming Languages
CLASS DEFINITION (FIELDS)
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Assessment – Java Basics: Part 1
Applying OO Concepts Using Java
Basics of OOP A class is the blueprint of an object.
JAVA CLASSES.
Object-Oriented Programming
CIS 199 Final Review.
Dr. R Z Khan Handout-3 Classes
Chapter 7 Objects and Classes
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Java Objects and Classes

Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes

Definitions n Object-oriented Programming –involves 3 main concepts –Encapsulation –Inheritance –Polymorphism n Class vs Object (instance of class)

More definitions n State - current set of values of the object n Methods - operate on objects; may change state; state may affect behavior n Inheritance in Java –a class ‘extends’ another class –has all the properties and methods of class extended and new methods and data fields that apply to new class

Using Existing Classes n Example: Math –only encapsulates methods, no data –do NOT construct a instance of class Math! –Call methods by using class name Math.sqrt (x);

Using Existing Classes n Example: Date –construct them specifying initial state using new, then apply methods Date birthday = new Date(); n Difference between object and object variable!!! –birthday refers to an object variable

Date deadline; //object variable String s = deadline.toString(); //NO! Need between deadline =new Date(); or deadline = birthday

Semantics Date deadline = new Date(); n Expression new Date() makes an object of type Date and its value is a reference to that newly created object. The reference is then stored in the deadline object variable. Can also set deadline = null; but don’t call any methods for it!

Java and C++ Differences JAVA Date birthday; Date birthday = new Date(); n Objects live on heaps, access bad reference, get error n Auto garbage collection n Clone to get a copy C++ Date * birthday; Date * birthday = new Date(); n Access bad pointer, get another memory location n Destructors necessary n Effort for copy and assignment

Mutator and Accessor Methods n Mutator methods change state of object n Accessor methods do NOT. –In C++ should use const suffix for these, most of you probably don’t –no distinction in Java n Convention: –mutators prefix method name with set –accessors use prefix get

Building your own classes n A complete Java program requires one class with a main method. n No other classes have a main method

Simplest form class NameOfClass { constructor1 constructor2 Methods Fields }//no ;

Example: ComplexNumber.java n In main code: ComplexNumber c = new ComplexNumber (); ComplexNumber b = new ComplexNumber (4.5,5.5); ComplexNumber [] cnums = new ComplexNumber[3]; cnums[0] = new ComplexNumber (5.4, 3.2); cnums[1] = new ComplexNumber (); cnums[2] = new ComplexNumber (3.0, 4.1);

Using Multiple Source Files n Put ComplexNumber class in ComplexNumber.java file n Put ManipCN class (main class) in ManipCN.java file n Make ComplexNumber class public! n To compile: javac ManipCN.java

Notes: n Constructor has same name as class n classes can have more than one constructor n A constructor may take zero, one or more parameters n A constructor has no return value n A constructor is called with the new operator. //different from C++

Fields n Final instance fields –must be set before end of constructor and is never changed again private final String name; n Static fields (class fields) –only one such field per class, shared among all instances of the class private static int nextId = 1;