Classes and Objects - Part I. What is an Object? An Object has two primary components: state – properties of the object behavior – operations the object.

Slides:



Advertisements
Similar presentations
Classes And Objects II. Recall the LightSwitch Class class LightSwitch { boolean on = true; boolean isOn() { return on; } void switch() { on = !on; }
Advertisements

BBIT 212/ CISY 111 Object Oriented Programming (OOP) Objects and classes, object interactions in Java
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
Chapter 2 Review Questions
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
Introduction to Programming with Java, for Beginners Arrays of Objects.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Understanding class definitions Looking inside classes.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private.
ECE122 Feb. 22, Any question on Vehicle sample code?
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
10-Nov-15 Java Object Oriented Programming What is it?
Day Class Definitions and Methods Local & global variables, parameters & arguments,
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Understanding class definitions
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Arithmetic, Class Variables and Class Methods Week 11
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Class Fundamentals BCIS 3680 Enterprise Programming.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
MIT AITI 2004 Lecture 7 Classes and Objects - Part I.
GC211 Data structure Lecture 3 Sara Alhajjam.
Objects as a programming concept
Some Eclipse shortcuts
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
Creating Your OwnClasses
suggested reading: Java Ch. 6
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Understanding class definitions
Chapter 3 Introduction to Classes, Objects Methods and Strings
An Introduction to Java – Part II
Creating Objects in a Few Simple Steps
Interfaces.
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)
Recap Week 2 and 3.
Assessment – Java Basics: Part 1
Understanding class definitions
Chapter 9 Objects and Classes Part 01
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Encapsulation.
Presentation transcript:

Classes and Objects - Part I

What is an Object? An Object has two primary components: state – properties of the object behavior – operations the object can perform Examples objectstatebehavior dogbreed, isHungryeat, bark grade bookgradesmean, median lighton/offswitch

Objects in Java A class defines a new type of Object To create a Object type to represent a light switch... class LightSwitch { // state and behavior here }

Fields An Object's state is stored in variables called fields Fields are declared (and optionally initialized) inside the braces of the class Light switch example with a field... class LightSwitch { boolean on = true; }

Methods An Object's behavior is defined by its methods Methods, like fields, are written inside the braces of the class Methods can access the fields (the state) of their object and can change them

Light Switch Example class LightSwitch { boolean on = true; // field boolean isOn() { // returns return on; // the state } void switch() { // changes on = !on; // the state }

Constructing Objects We use the new keyword to construct a new instance of an Object We can assign this instance to a variable with the same type of the Object Note: classes define new datatypes ! LightSwitch ls = new LightSwitch();

ls.on; ls.isOn(); ls.switch(); Using Fields and Methods To access the field of an instance of an Object use instance.field To access the method of an instance use instance.method(arguments)

Example Using Light Switch What does this main method print out? LightSwitch ls = new LightSwitch(); System.out.println(ls.on); ls.switch(); System.out.println(ls.isOn()); true false

Person Example class Person { String name = “Jamal”; int age = 26; String getName() { return name; } void setName(String n) { name = n; } int getAge() { return age; } void setAge(int a) { age = a; } boolean smellsBad(){return true;} }

Constructing Person Objects To create an instance of the Person class with a name of "George" and an age of 22 Can we create a Person that has the name George and the age 22 from the moment it is created? Answer: Yes! Person george = new Person(); george.setName("George"); george.setAge(22);

Constructors Constructors are special methods used to construct an instance of a class They have no return type They have the same name as the class of the Object they are constructing They initialize the state of the Object Call the constructor by preceding it with the new keyword

Person Constructor Now we can construct George as follows: class Person { String name; int age; Person(String n, int a) { name = n; age = a; } //... } Person george = new Person("George", 22);

Default Constructor When you do not write a constructor in a class, it implicitly has a constructor with no arguments and an empty body Result: every class has a constructor class LightSwitch { // Leaving out the constructor // is the same as... LightSwitch() {} }

Multiple Constructors A class can have multiple constructors class LightSwitch { boolean on; LightSwitch() { on = true; } LightSwitch(boolean o) { on = o; }

This Keyword Instance can refer to itself with the keyword this class LightSwitch { boolean on; LightSwitch() { this.on = true; //(same as on=true;) } LightSwitch(boolean on) { this.on = on; }

Cascading Constructors A constructor can call another constructor with this(arguments) class LightSwitch { boolean on; LightSwitch() { this(true); } LightSwitch(boolean on) { this.on = on; }

Classes Recap Classes have fields to store the state of the objects in the class and methods to provide the operations the objects can perform. We construct instances of a class with the keyword new followed by a call a constructor method of the class. We can assign an instance to a variable with a datatype named the same as the class. If you do not provide a constructor, the class will have one with no arguments and no statements by default.

Apple Example class Apple { String color; double price; Apple(String color, double price) { this.color = color; this.price = price; } Apple(double price) { this("green", price); } String getColor() { return color; } double getPrice() { return price; } void setPrice(double p) { price = p; } }

Apple Quiz What will these lines print out? Apple a = new Apple("red", 100.0); System.out.println(a.getColor()); System.out.println(a.getPrice()); a.setPrice(50.5); System.out.println(a.getPrice()); Apple b = new Apple(74.6); System.out.println(b.getColor()); System.out.println(b.getPrice()); b.setPrice(a.getPrice()); System.out.println(b.getPrice()); red green