Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program.

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
OBJECT-ORIENTED PROGRAMMING CONCEPTS (Review). What is an Object? What is an Object? Objects have states and behaviors. Example: A dog has states - color,
Inheritance, part 2: Subclassing COMP 401, Fall 2014 Lecture 8 9/11/2014.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism = slide covered in class – others for reference.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Objects and Classes The objectives of this chapter are: To discuss important aspects of the software development process To define objects and classes.
Classes and Objects in Java. What Is an Object?. An object is a software bundle of related state and behavior. Software objects are often used to model.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Object Oriented Paradigm Programming Paradigms En Mohd Norafizal A.Aziz.
Object Oriented Software Development
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 4 Objects and Classes.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Introduction to Programming Writing Java Beginning Java Programs.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
The Java Programming Language
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Introduction to Programming Writing Java Beginning Java Programs.
Announcements Final Exam:TBD. public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE;
AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance A Review of Objects, Classes, and Subclasses.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
What Is a Package? A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar.
A Java program consists of a set of class definitions, optionally grouped into packages. Each class encapsulates state and behavior appropriate to whatever.
By : Robert Apeldorn. What is OOP?  Object-oriented programming is a programming paradigm that uses “objects” to design applications and computer programs.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Object Oriented Programming Object and Classes Lecture 3 MBY.
Object and Classes อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 3.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Objects as a programming concept
3 Introduction to Classes and Objects.
Classes and OOP.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
03/10/14 Chapter 9 Inheritance.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Classes Variables That Are Not of a Built-in Type Are Objects
Introduction to Object-Oriented Programming
Dr. R Z Khan Handout-3 Classes
Day 11 The Last Week!.
Presentation transcript:

Java Objects and Classes

3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program creates many objects, which as you know, interact by invoking methods.  Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network.  Once an object has completed the work for which it was created, its resources are recycled for use by other objects.  An object can create other objects.  An object can call another object’s (and its own) methods  An object has data fields, which hold values that can change while the program is running.

 A class is a piece of the program’s source code that describes a particular type of objects.  A class is a template or blueprint that describes the behavior/ state of the object.  An object is called an instance of a class. A program can create and use more than one object (instance) of the same class. 3-3

 A blueprint for objects of a particular type  Defines the structure (number, types) of the attributes  Defines available behaviors of its objects 3-4 Attributes Behaviors

3-5 Attributes: String model Color color int numPassengers double amountOfGas Behaviors: Add/remove a passenger Get the tank filled Report when out of gas Attributes: model = "Mustang" color = Color.YELLOW numPassengers = 5 amountOfGas = 16.5 Behaviors:

3-6  A piece of the program’s source code  Written by a programmer  An entity in a running program  Created when the program is running (by the main method or a constructor or another method)

3-7  Specifies the structure (the number and types) of its objects’ attributes — the same for all of its objects  Specifies the possible behaviors of its objects  Holds specific values of attributes; these values can change while the program is running  Behaves appropriately when called upon

3-8  Each class is stored in a separate file  The name of the file must be the same as the name of the class, with the extension.java public class Car {... } Car.java By convention, the name of a class (and its source file) always starts with a capital letter. (In Java, all names are case-sensitive.)

3-9  Java programs are usually not written from scratch.  There are hundreds of library classes for all occasions.  Library classes are organized into packages. For example: java.util — miscellaneous utility classes java.awt — windowing and graphics toolkit javax.swing — GUI development package

Objects and Classes Programmers implement classes Classes are templates or blueprints for Objects Data and methods are defined within Classes Classes must provide an implementation such that objects created from those classes behave as those defined in the Object model. An Object is the instantiation of a class An object is an Instance of a class The process of creating an object is called instantiation The attributes of an object are called instance variables The methods of an object are called instance methods In Java, Objects are created using the new keyword: Employee e1 = new Employee();

Defining Classes A class definition must have the following: The keyword "class" followed by the name of the class The class body Before the keyword "class" is the optional modifier "public" If a class is public, it must be defined within a file which is the same name as the class with a ".java" extension. i.e. Classname.java eg. HelloWorld.java, Account.java, Ledger.java, Transaction.java most classes are declared public The class body contains: Zero or more instance variables Zero or more methods

Example Class Definition public class Employee { String name; String dept; int salary; Date startingDate; // more variable definitions... public int getSalary() //accessor method { return salary; } public int computeHourlyRate() //mutator method { // calculate hourly rate from salary } //more method definitions } in Employee.java: Instance Variables: Methods:

public class Bicycle { // the Bicycle class has three fields public int cadence; public int gear; public int speed; // the Bicycle class has one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; }

// the Bicycle class has four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } }