Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.

Slides:



Advertisements
Similar presentations
AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Inheritance Inheritance Reserved word protected Reserved word super
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
OOP Languages: Java vs C++
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Abstraction, Inheritance, and Polymorphism in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
An Introduction to Software Development Using JAVA
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 (part 2) Khalid Siddiqui.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
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.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
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.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Classes and Objects Including inheritance. OOP — Object-Oriented Programming In OOP system, the class is a fundamental unit. An OOP program models a.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
Coming up: Inheritance
Creating a Java Application and Applet
Chapter 4 Defining Classes I Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Object Oriented Programming Object and Classes Lecture 3 MBY.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Console Input and Output JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
OOP: Encapsulation &Abstraction
Java Classes, Objects, and Events: A Preview
Java Methods Objects and Classes Start on Slide 30 for day 2
Java Methods Objects and Classes Start on Slide 30 for day 2
Object-Oriented Programming
Unit 3 - Introduction to Inheritance - Example
Presentation transcript:

Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.

3-2 Objectives: See an example of a small program written in OOP style and discuss the types of objects used in it Learn about the general structure of a class, its fields, constructors, and methods Get a feel for how objects are created and how to call their methods Learn a little about inheritance in OOP

3-3 OOP An OO program models the application as a world of interacting objects. An object can create other objects. An object can call another object’s (and its own) methods (that is, “send messages”). An object has data fields, which hold values that can change while the program is running.

3-4 Objects Can model real-world objects Can represent GUI (Graphical User Interface) components Can represent software entities (events, files, images, etc.) Can represent abstract concepts (for example, rules of a game, a particular type of dance, etc.)

3-5 Objects in the Dance Studio Program Dancer Foot Dance floor Control panel Go / Stop button Dance selection pulldown list Positioning button Dance Studio window Band Dance group Waltz, etc. Dance step

3-6 Classes and Objects A class is a piece of the program’s source code that describes a particular type of objects. OO programmers write class definitions. 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-7 Class Object A blueprint for objects of a particular type Defines the structure (number, types) of the attributes Defines available behaviors of its objects Attributes Behaviors

3-8 Class: Car Object: a car 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 = 0 amountOfGas = 16.5 Behaviors:

3-9 Class vs. Object 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-10 Class vs. Object 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-11 CRC Card A preliminary description of a class at the initial stage of program design Collaborators Class Responsibilities

3-12 Classes and Source Files 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-13 Libraries 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

3-14 import Full library class names include the package name. For example: java.awt.Color javax.swing.JButton import statements at the top of the source file let you refer to library classes by their short names: import javax.swing.JButton;... JButton go = new JButton("Go"); Fully-qualified name

3-15 import (cont’d) You can import names for all the classes in a package by using a wildcard.*: import java.awt.*; import java.awt.event.*; import javax.swing.*; java.lang is imported automatically into all classes; defines System, Math, Object, String, and other commonly used classes. Imports all classes from awt, awt.event, and swing packages

3-16 public class SomeClass Fields Constructors Methods } Attributes / variables that define the object’s state; can hold numbers, characters, strings, other objects Procedures for constructing a new object of this class and initializing its fields Actions that an object of this class can take (behaviors) { Class header SomeClass.java import... import statements

3-17 public class Foot { private Image picture; private CoordinateSystem coordinates; public Foot (int x, int y, Image pic) { picture = pic; coordinates = new CoordinateSystem (x, y, pic); } public void moveForward (int distance) { coordinates.shift (distance, 0); } public void moveSideways (int distance) { coordinates.shift (0, distance); }... } Fields Constructor Methods

3-18 Fields A.k.a. instance variables Constitute “private memory” of an object Each field has a data type (int, double, String, Image, Foot, etc.) Each field has a name given by the programmer

3-19 private [static] [final] datatype name ; Fields (cont’d) Usually private May be present: means the field is a constant int, double, etc., or an object: String, Image, Foot You name it! May be present: means the field is shared by all objects in the class private Foot leftFoot ;

3-20 Constructors Short procedures for creating objects of a class Always have the same name as the class Initialize the object’s fields May take parameters A class may have several constructors that differ in the number and/or types of their parameters

3-21 Constructors (cont’d) public class Foot { private Image picture; private CoordinateSystem coordinates; public Foot (int x, int y, Image pic) { picture = pic; coordinates = new CoordinateSystem(x, y, pic); }... } The name of a constructor is always the same as the name of the class A constructor can take parameters Initializes fields

3-22 Constructors (cont’d) public class Foot {... public Foot (int x, int y, Image pic) {... }... } // FootTest.java... Image leftShoe =...;... Foot leftFoot = new Foot (5, 20, leftShoe);... An object is created with the new operator The number, order, and types of parameters must match Constructor

3-23 Constructors (cont’d) JButton go = new JButton("Go");

3-24 Methods Call them for a particular object: leftFoot.moveForward(20); amy.nextStep( ); ben.nextStep( ); go.setText("Stop");

3-25 Methods (cont’d) The number and types of parameters (a.k.a. arguments) passed to a method must match method’s parameters: g.drawString ("Welcome", 120, 50); public void drawString ( String msg, int x, int y ) {... }

3-26 Methods (cont’d) A method can return a value to the caller The keyword void in the method’s header indicates that the method does not return any value public void moveSideways(int distance) {... }

3-27 Encapsulation and Information Hiding A class interacts with other classes only through constructors and public methods Other classes do not need to know the mechanics (implementation details) of a class to use it effectively Encapsulation facilitates team work and program maintenance (making changes to the code)

3-28 Methods (cont’d) Constructors and methods can call other public and private methods of the same class. Constructors and methods can call only public methods of another class. Class X private field private method Class Y public method

3-29 Inheritance In OOP a programmer can create a new class by extending an existing class Superclass (Base class) Subclass (Derived class) subclass extends superclass

3-30 A Subclass... inherits fields and methods of its superclass can add new fields and methods can redefine (override) a method of the superclass must provide its own constructors, but calls superclass’s constructors does not have direct access to its superclass’s private fields

3-31 public class Pacer extends Walker { public Pacer (int x, int y, Image leftPic, Image rightPic) { super (x, y, leftPic, rightPic); } public void turnAround () { Foot lf = getLeftFoot (); Foot rf = getRightFoot (); lf.turn (180); rf.turn (180); lf.moveSideways (-PIXELS_PER_INCH * 8); rf.moveSideways (PIXELS_PER_INCH * 8); } A new method Calls Walker’s constructor using super Constructor Calls Walker’s accessor methods

3-32 public class Walker {... public int distanceTraveled() { return stepsCount * stepLength; }... } Overrides Walker’s distanceTraveled method public class Slowpoke extends Walker {... public int distanceTraveled() { return super.distanceTraveled ( ) / 10; }... } Calls superclass’s distanceTraveled method

3-33 Review: Name a few objects used in Dance Studio. Name a few library classes that we used in Dance Studio. What are import statements used for? What is a field? A constructor? A method? Which operator is used to construct an object?

3-34 Review (cont’d): What is the difference between private and public methods? Why are fields usually private? What is inheritance? Can a subclass add new fields? New methods? How does a subclass call its superclass’s constructors?