Objects and Classes Object-oriented programming (OOP) revolves around – Defining classes – Creating instances of classes (objects) and interacting with.

Slides:



Advertisements
Similar presentations
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Advertisements

Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 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.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
Chapter 9 Objects and Classes
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Writing Classes (Chapter 4)
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
CS 112 Programming 2 Lecture 02 Objects and Classes (1)
OOP Basics Classes & Methods (c) IDMS/SQL News
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Classes and Objects The main advantage of OOP is that you can “grow” the language add our own classes and their behavior how they function, what they can.
Java Primer 1: Types, Classes and Operators
Corresponds with Chapter 7
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Java Programming Language
OO Programming Concepts
Classes and Objects Object Creation
Chapter 7 Objects and Classes
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Objects and Classes Object-oriented programming (OOP) revolves around – Defining classes – Creating instances of classes (objects) and interacting with the objects through message passing This paradigm differs from traditional procedural programming where you have – Procedures/functions that call other procedures/functions because interaction with an object can only be done through the class’ interface – This promotes such concepts as information hiding and encapsulation – OOP also permits inheritance so that we can extend a class into a more specific class without rewriting the original class from scratch

States and Behaviors The state of an object is the collection of instance data and their current values The behavior of an object is how it responds to messages, which is dictated by the methods that are implemented in the class All objects of the same class will share the same methods and the same instance data – but each object has its own state because it will have its own unique storage in memory with (possibly) differing values for its instance data Objects share the same interface – you access each object of a class in the same way – but objects differ as you use them because of their differing states

Defining Classes: UML UML is the Unified Modeling Language – It is a representation used to illustrate the components of a class without necessarily seeing the details – This representation is provided as a box segmented into 3 areas: the class name, the instance data (type and name) and the methods’ names (including their return type if any and their parameters) – We can also express objects of a class in UML by filling in their state – Below, we see UML for a Circle class (shown in the next slides)

Circle Class Defined public class Circle { private double radius; public Circle( ) { radius=1.0; } public Circle(double newRadius) { radius=newRadius; } public double getRadius() { return radius; } public void setRadius(double newRadius) { radius = newRadius; } public double getArea() { return radius*radius*Math.PI; } public double getPerimeter() { return 2*radius*Math.PI; }

A Class Explained As our classes are to model real world things (whether physical objects, abstract objects or concepts) – We need to model them through instance data for the circle, we have radius – A constructor is a method that will be used to initialize any instance data – We need to define an interface – how other programs will interact with the object – We will usually want to protect the instance data from external access, so to access and effect them, we need methods accessor methods will return values of instance data (like getArea, getPerimeter) mutator methods will (or can) alter instance data (like setRadius)

Using a Class Once defined, we use the class by – Declaring variables of that type of class Circle circle1, circle2, mycircle; – Instantiate the variables into objects (instantiation calls upon the class’ constructor) circle1=new Circle( ); // use the first constructor mycircle=new Circle(12); // use the second constructor – Pass messages to those objects perimeter=mycircle.getPerimeter( ); mycircle.setRadius(10); We place these instructions in another class – Often a “user class” which is defined in a class with a main method – A class with a main method is the only type of class that can be directly executed in Java

Visibility Modifiers There are four modifiers that control who can access the various components of a class (methods, instance data): public, private, protected, default – To support information hiding, we generally want to make all instance data private (accessible only from within the class) so that another object cannot alter this object’s data constants can be public because they cannot be altered – To provide an interface, those methods that can be called from outside the class will be made public other methods that are called only from within the class are also made private – Protected items will be discussed when we look at inheritance – The default modifier means that you do not list any of public, private or protected – this modifier means that items are treated as private unless the classes are defined in the same package (library)

Example y and m2 are default, available to C1 and C2 alike but not to C3

Example: TV Class public class TV { int channel = 1; // Default channel int volumeLevel = 1; // Default volume boolean on = false; // Default TV is off public TV( ) { } public void turnOn( ) { on = true; } public void turnOff( ) { on = false; } public void setChannel(int newChannel) { if (on && newChannel >= 1 && newChannel <= 120) channel = newChannel; }

public void setVolume(int newVolumeLevel) { if (on && newVolumeLevel >= 1 && newVolumeLevel <= 7) volumeLevel = newVolumeLevel; } public void channelUp( ) { if (on && channel < 120) channel++; } public void channelDown( ) { if (on && channel > 1) channel--; } public void volumeUp( ) { if (on && volumeLevel < 7) volumeLevel++; } public void volumeDown( ) { if (on && volumeLevel > 1) volumeLevel--; }

TestTV public class TestTV { public static void main(String[] args) { TV tv1 = new TV(); tv1.turnOn(); tv1.setChannel(30); tv1.setVolume(3); TV tv2 = new TV(); tv2.turnOn(); tv2.channelUp(); tv2.volumeUp(); System.out.println("tv1's channel is " + tv1.channel + " and volume level is " + tv1.volumeLevel); System.out.println("tv2's channel is " + tv2.channel + " and volume level is " + tv2.volumeLevel); }

Constructors All classes require a constructor – if you do not define one, a default constructor will be provided by the Java compiler – this default constructor is a no-arg constructor (no parameters) – you may (and probably should) overload your constructor so that you can define a constructor for each possible set of parameters that you might expect to the constructor recall Circle had two, one with no parameter and one with a double as a parameter The constructor is called when you do a new Class(…) operation Constructors must share the same name as the class and must be public to be of use Constructors have no return type (not even void)

Accessors and Mutators Since we hide our instance data (make them private), how do we access and change the data in an object? – Accessor methods are usually simple methods that return a single instance datum its name will usually be getVariableName such as getX or getRadius – Mutator methods are usually simple methods that change a single instance datum as in setRadius however, we can also make mutators that change multiple data as needed – We usually want to ensure that any data change make sense – for setRadius, we might make sure the parameter passed is positive (a negative or 0 radius doesn’t make sense) public void setRadius(double newRadius){ if(newRadius > 0) radius = newRadius; }

Additional Examples Here we see the Random and Date classes using UML

Reference Variables All variables in Java are of one of two types – A primitive – int, char, double, float, boolean, etc – A reference variable – this serves as a means to reference an object in most programming languages, we call the reference a pointer because it stores a memory location of the item being referenced instead of the actual item You declare reference variables like any other type The reference variable is the size of an address You point the reference variable by assigning it to the location of an object – if the object doesn’t exist yet, use new Class(…) – if an object of this type exists, you can point this variable at it using var=existingObject;

Reference Variables vs Primitive Data

Reference vs What is Being Referred With a primitive datum, the datum is stored in an easily accessible area of memory (the stack) where you access the datum by variable name With an object, the datum is stored in the heap – The heap stores nameless items – You can only access these items through a pointer (reference variable) – Usually the reference variable itself is stored in the stack, pointing to an area of the heap Heap memory when no longer of use must be deallocated so that the heap can reuse it – In some languages like C/C++, you have to explicitly deallocate heap memory use a special instruction – In Java, if you reassign the reference to point elsewhere, the Java garbage collection will deallocate that heap memory

Stack vs Heap Memory For every method, you are given a location in the run time stack to store each local variable and parameter Objects are referenced by reference variables, stored on the stack Objects are physically stored in heap memory, created whenever you use new and deallocated whenever the reference variable is reassigned

Passing Objects as Parameters We can pass primitive data or reference variables as parameters to methods – foo(var); If var is a primitive, we are passing a copy of var – changing the value in the method does not impact var If var is a reference, we are passing a copy of the object’s address in memory, not a copy of the object – changing var (via message passing) in the method changes the object itself

Example public class ExampleClass { private int x; public ExampleClass(int value) {x = value;} public void inc( ){x++;} public int getValue( ) {return x;} } public class IncrementClassUser { public static void main (String[] args){ IncrementClass foo=new IncrementClass(0); int x=0; increment(foo); increment(x); System.out.println( foo.getValue( )); System.out.println(x); } public static void increment (int value) { value++; } public static void increment (IncrementClass foo) { foo.inc( ); } Output is 1 0 See how foo changes but x does not

The null Value Reference variables that are not currently pointing at an object in the heap will have the value null You can alter the reference variable’s value by instantiating a new object – Using var=new Class(…); – Or by assigning the variable to point at an already existing object as in var=var2; for this to work, var2 must be the same class of object as var (we refine this idea later when we learn about inheritance and polymorphism) Passing a message to an object whose value will yield a NullPointerException – We can avoid this by testing the variable first –if(var!=null) var.someMessage( );

Static The term static is meant to convey that all instances of a class share this item We can use static to modify a variable, a constant and/or a method – if an instance data is static, then that variable is shared among all objects of the class in such a case, the instance datum is thought of as a class variable (or a class-wide variable) – we only do this if we intend that no single object has its own unique variable but instead the one variable is shared across all objects of the class – if a method is static, then the method cannot access instance data of the class (but it can access class variables) we might use this to access any static variables We generally only use static when we are defining methods and variables in a class with main

Example public class StaticExample { private int x; private static int y; public StaticExample() { x=y=0; } public StaticExample(int x) { this.x=x; y=0; } public void change(int x) { this.x=x; y++; } public void inc() { x++; y++; } public static int getY() { return y; } public class StaticUser { public static void main(String[] args){ StaticExample s1= new StaticExample(); StaticExample s2= new StaticExample(5); StaticExample s3= new StaticExample(10); s1.inc(); s2.change(15); s3.change(22); s3.inc(); System.out.println( StaticExample.getY()); } Outputs 4

Another Example The CircleWithStaticMembers class is defined on p – We add a static int variable numberOfObjects this variable is a class-wide variable, incremented whenever the constructor is called – And a static method getNumberOfObjects to return this class-wide variable – Notice that since we do not have an explicit deconstructor, we do not know when to decrement numberOfObjects

Summary of Static A non-static method – can invoke another non-static method – can access a non-static instance datum – can invoke a static method – can access a static instance datum (classwide variable) A static method – can invoke a static method – can access a static instance datum – cannot invoke a non-static method – cannot access a non-static instance datum Typically you will define a static method to operate on parameters passed to the method, for instance – public static int compareCircle(Circle c1, Circle c2) {…} // code will compare two circles and return an int indicating which is larger, for instance, -1, 0 or 1 for c1 c2 respectively

Arrays of Objects Arrays are objects and so need to be instantiated –int[ ] x = new int[10]; Notice how this instantiation differs from an ordinary object where we pass parameters as in –Car c = new Car(“ferrari”, 2013); But an array of objects requires two levels of instantiation –Car[ ] cars = new Car[100]; –cars[0] = new Car(“ferrari”, 2013); –cars[1] = new Car(“prius”, 2010, “used”); – cars[2] through cars[99] are currently equal to null and so passing one a message (e.g., cars[2].getYear( ); ) would cause a NullPointerException

Java as a Compiled Language High level programming languages are either compiled or interpreted – An interpreted language runs in an environment maintained by an interpreter – The interpreter takes each program instruction, one at a time, converts it to machine language and executes it – In this way, you can execute instructions from a command line interface to test each instruction out Compiled languages are translated into machine language all at once requiring that the program be complete (or complete enough to permit compilation) – Java is a compiled language and yet the Java compiler does not compile a Java program into machine language

The Java Compiler and the JVM The Java compiler compiles your code into an intermediate form called byte code – Byte code is machine independent but consists of primitive operations more like machine or assembly language instructions To execute your byte code, you need a Java Virtual Machine which translates the byte code into executable code – The JVM runs in any web browser and also in the JRE (Java Runtime Environment) Although interpreted languages will run more slowly then compiled languages because of the need for interpreting code at run time, Java is somewhat of an exception – Because the JVM is only doing minimal interpretation as byte code is already at a low level The advantage of this approach is that byte code is platform independent allowing Java programs to be compiled once but can then run nearly anywhere

What the Java Compiler Does Obviously the Java compiler compiles your Java code – It will convert a Java class into a.class file Consider the TV and TestTV classes from earlier – Compiling TV.java creates TV.class – Compiling TestTV.java creates TestTV.class you cannot directly run a.class file as if it were a.exe file instead you run it via the JRE – Note that we will see later how to define classes inside of other classes (nested or inner classes) an inner class is compiled into its own.class file separate from the outer class e.g., public class Foo {… private class Bar {…} } will create Foo.class and Foo$Bar.class As with any compiled language, the compiler will also locate compiler errors – We call these syntax errors – The Java compiler will not be able to compile a Java class with syntax errors and so you must fix all syntax errors before successfully compiling the class