Basics of OOP A class is the blueprint of an object.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
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)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
OOP Languages: Java vs C++
Writing Classes (Chapter 4)
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
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.
ECE122 Feb. 22, Any question on Vehicle sample code?
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
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.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
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.
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.
CLASSES AND OBJECTS Object-Oriented Basics. Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected)
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.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Classes and Objects Introduced
Lecture 3: Introduction to Object and Classes
Lecture 7 D&D Chapter 7 & 8 Composite Classes and enums Date.
Interface, Subclass, and Abstract Class Review
Intro To Classes Review
Creating Your OwnClasses
CompSci 230 Software Construction
CS 302 Week 11 Jim Williams, PhD.
Lecture 9 Concepts of Programming Languages
CS Week 13 Jim Williams, PhD.
CLASS DEFINITION (> 1 CONSTRUCTOR)
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Object Oriented Programming in java
Today’s topics UML Diagramming review of terms
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object Oriented Programming Review
Java Basics II Minseok Kwon
Class Diagram.
Web Design & Development Lecture 4
Dr. R Z Khan Handout-3 Classes
OO Programming Concepts
2.1 Introduction to Object-Oriented Programming
Classes and Objects Object Creation
Chapter 7 Objects and Classes
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Basics of OOP A class is the blueprint of an object. Example: A blueprint for a house establishes the design specifications: layout of the rooms dimensions, etc. A Java class is used to model (establish) the data attributes and behavior of an object.

Example: How do we model a circle drawn on a canvas?

Constructors A constructor is used to create (instantiate) an object: Circle c1 = new Circle(); Constructors must have the same name as the class. A class can have multiple constructors. This is called overloading the constructor. The compiler picks the constructor that matches the constructor arguments. c1 is a Circle object created by the constructor of Circle.

public Circle(int x, int y) { radius = 1; this. x = x; this public Circle(int x, int y) { radius = 1; this.x = x; this.y = y; fillColor = “#000000”; strokeColor = “#000000”; } public Circle() { x = 1; y = 1; Explicit Constructor Overloaded (same name) The compiler recognizes the which constructor to use based on the parameter list. Default Constructor

What happens when there are no constructors? A default constructor with no arguments is automatically generated. All data members are initialized to default values. Default values are 0, false, and null for object instances.

Object References An object is a reference pointer to a memory location. Circle c1 = new Circle(); radius x c1 y fillColor strokeColor

Example of Object References Consider the following code: Circle c1 = new Circle(4, 7); Circle c2 = new Circle(3, 8); c1 = c2;   The data of c2 is NOT copied to c1. The address of c2 is copied to c1. Garbage collection mode will locate and reclaim any memory occupied by lost objects.

Circle c1 = new Circle(4, 7); Circle c3 = c2; c1 = c2;

public , private, static access public and private are used to allow (public) or hinder(private) access to data members and methods of a class. static means an element defined by a class belongs to the class, not to an object of the class.   Examples of a static: Math.PI Math.abs() Math.cos() Math.random()

Example of a static Usage Static constants can be either public or private. Static variables are usually private. public class Play { public static final int CUPCAKE = 999999; public static String breakfast() { return "Breakfast is the a morning meal."; }

public class Play { public static final int CUPCAKE = 999999; public static String breakfast() { return "Breakfast is a morning meal."; } Poor Programming: Not accessed in a static way. This won’t cause an error, but it is poor programming. public class TestPlay { public static void main(String[] args) { Play play1 = new Play(); System.out.println(play1.CUPCAKE); System.out.println(play1.breakfast());   System.out.println(Play.CUPCAKE); System.out.println(Play.breakfast()); } Good Programming: Accessed in a static way. This is the correct way to call a static method/value.

Dependency and Aggregation (composition) in Classes The classes in a software application can have various relationships to each other. Three of the more common relationships are dependency, composition (also called aggregation), and inheritance.   Dependency means that a class uses or relies on another class. For example, the word jumble application relied on the random number generator to randomly select a word to play. Aggregation (Composition) describes how an object is composed. Some objects are made up of other objects. For example, a car is made up of an engine, wheels, chassis, etc; Composition is often referred to as a “has a” or “has many” relationship because the objects of the composite “have” objects of the composed class as members.

Deck of Cards Example Notes: An ArrayList is used to store the collection of cards. Collections can be used to shuffle the list of cards.

Setters and Getters Setters and Getters and mutator and accessor methods. Mutator methods are used to change or set the data of an object: card1.setRank(2); Accessor methods do not change the data, but rather access (get) the data of an object: card1.getRand ();

UML Diagram UML is a Unified Modeling Language. Class attributes and methods are indicated in a UML diagram. A UML diagram divides the class structure into compartments for data members, constructors, and methods.

Practice Create the Deck and Card class. Create a test class that performs the following: Create a deck of cards. Deal cards until there are no more cards left in the deck. Repopulate and shuffle the deck. Deal until there are no more cards left in the deck.