Chapter 4 Objects and Classes.

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

CS0007: Introduction to Computer Programming 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,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Object Oriented Paradigm Programming Paradigms En Mohd Norafizal A.Aziz.
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.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Object Oriented Programming Concepts Fatih University Ceng-104-A Introduction to Object Oriented Programming Harun Reşit Zafer This is a slide version.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Java. Features of Java Architecture Neutral and Portable o Write once, run anywhere o Java Virtual Machine (JVM) Object Oriented o Classes encapsulate.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
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.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Programming Concepts. Object ► An object is a software bundle of related state and behavior. ► Software objects are often used to model.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
By : Robert Apeldorn. What is OOP?  Object-oriented programming is a programming paradigm that uses “objects” to design applications and computer programs.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Chapter 11 An introduction to object-oriented design.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Introduction to Object-oriented Programming
GC211 Data structure Lecture 3 Sara Alhajjam.
What is an Object Objects are key to understanding object-oriented technology. An object can be considered a "thing" that can perform a set of related.
OOP: Encapsulation &Abstraction
Objects as a programming concept
Classes and OOP.
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
03/10/14 Chapter 9 Inheritance.
Creating Your OwnClasses
OBJECT ORIENTED CONCEPTS
EEC 484 Computer Networks Java Tutorial #1 Wenbing Zhao
البرمجة الكينونية بلغة جافا 1294
Object-Oriented Programming
Object-Oriented Programming
OO Programming Concepts
Chapter 7 Objects and Classes
CSG2H3 Object Oriented Programming
Presentation transcript:

Chapter 4 Objects and Classes

Assignment 2 Solution In class exercise

Object Oriented Programming OOP Programs made up of objects Functionality exposed to users Implementation hidden from users Objects can be used from library or custom created Using object and their methods can reduce program complexity

Classes The template or “blueprint” from which objects are made. Construct an object from class means to create an “instance” of that class Encapsulation – aka info hiding. The data inside an object is known as its instance fields Function that operate on the data are called methods

Class cont’ Notion that classes are cookie cutters and objects are the cookies created from classes The user interacts with the methods of class – you should never deal with how the methods are implemented

Classes cont’ Java has the ability to “extend” a class. A new class has all of the properties and methods of the class you extend You can write new methods and add properties that apply to your new class. This is also referred to as “inheritance”

Where to start?? The top down approach doesn’t apply You don’t start with the void main(String args[]) When thinking about creating your own classes (use nouns for analyzing the problem space) Use verbs to describe methods on the nouns

Using Predefined Classes Take, for example, the Math class. You have seen that you can use methods of the Math class, such as Math.random(), without needing to know how they are implemented— all you need to know is the name and parameters (if any). That is the point of encapsulation and will certainly be true of all classes. But the Math class only encapsulates functionality; it neither needs nor hides data. Because there is no data, you do not need to worry about making objects and initializing their instance fields—there aren’t any Arrays class is another example of this

Object Variables To work with objects, you first construct them and specify their initial state. Then you apply methods to the objects. You use constructors to construct new instances. A constructor is a special method whose purpose is to construct and initialize objects. Constructors always have the same name as the class name. To construct an object, you combine the constructor with the new operator. new Date(); There is an important difference between objects and object variables. Date deadline; // deadline doesn't refer to any object The above defines an object variable, deadline, that can refer to objects of type Date

Object Variables Cont’ The return value of the new operator is also a reference. A statement such as Date deadline = new Date(); Has two parts. The expression new Date() makes an object of type Date, and its value is a reference to that newly created object. That reference is then stored in the deadline variable.

Lets create an Object!!! Bicycle Class Nouns? Verbs? Bike components Bike (behaviors)

Bicycle Class Bike properties State Methods Gear, speed, and cadence Current gear, speed, and cadence Methods SpeedUp, ChangeGear, Brake, ChangeCadence

Bike Class Java Code public class Bicycle { //members (properties of Bike class) int cadence = 0; int speed = 0; int gear = 1;

Bike Class Java Code cont’ void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; void speedUp(int increment) { speed = speed + increment; void applyBrakes(int decrement) { speed = speed - decrement; void printStates() { System.out.println("cadence:"+cadence+" speed:” +speed+" gear:"+gear); } //end of class

Bike Class Java Code cont’ class BicycleDemo { public static void main(String[] args) { // Create Bicycle objects Bicycle bike1 = new Bicycle(); // Invoke methods on those objects bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates(); }//end main() }//end BicycleDemo

Extending Bicycle class MountainBike extends Bicycle { } // new fields and methods defining a mountain bike would go here }

Relationships Between Classes

Constructors

Constructors Cont’

this

Accessor and Mutator and Private Are types of functions used in your classes Accessor fucntions (get) Return values of instance fields (class properties) Mutator functions (set) Change the value of the instance fields (class properties The private keyword makes sure that the only methods that can access these instance fields No outside method can read or write to these fields.

Instance Fields

Final Instance Fields

Static Fields and Methods

Initializing Fields

Method Parameters

Packages Java allows you to group classes in a collection called a package. Packages are convenient for organizing your work separating your work from code libraries provided by others The main reason for using packages is to guarantee the uniqueness of class names. You wouldn’t want two copies of Bicycle class

Packages Cont.’

Static Imports

Static Imports Cont.’

Extending A Class

Dynamic Binding

super Whenever you want to access a instance field of your super class inside of your sub class you have to use the keyword super See eclipse for example

Final Classes

Final Methods

Assignments 3 & 4 Due Dec 4th – posted to class website