INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

Classes and Objects in Java
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Tutorial 6 February 4th/5th, 2015
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
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
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)
Introduction to Java Programming, 4E Y. Daniel Liang.
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,
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Internet Software Development Classes and Inheritance Paul J Krause.
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.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Classes and Objects in Java Basics of Classes in Java.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Objects and Classes Mostafa Abdallah
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
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.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object Oriented Programming Object and Classes Lecture 3 MBY.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
DR. NERMIN HAMZA Java: Lecture 2 Java life cycle 2 Java programs normally undergo four phases  Edit  Programmer writes program (and stores program.
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.
Topic: Classes and Objects
Classes and Objects Introduced
Classes and Objects in Java
Classes and Objects in Java
Classes and Objects in Java
Chapter 9 Objects and Classes Part 01
OO Programming Concepts
Chapter 7 Objects and Classes
Presentation transcript:

INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines the “state” and “behaviour” of each object made from the class. A class essentially serves as a template for an object and behaves like a basic data type, e.g., “int”. We are, in essence, defining a type, along with all the functions and properties we want associated with the type we’re defining. It is therefore important to understand: how the properties (fields) and methods are defined in a class how they are used to build a Java program 2

CLASSES Contains definition for all Objects of the same type defines the data types and names of properties defines methods that define behavior the “template” for all Object instances of the same type

CLASSES The basic syntax for a class definition: Bare bone class – no fields, no methods 5 public class Circle { // my circle class } class ClassName { [fields declaration] [methods declaration] }

CLASSES 6

ADDING PROPERTIES (FIELDS): Add fields 7 public class Circle { double radius; // radius of the circle double x, y; // center coordinate double circ; // circumference }

CONSTRUCTORS public class Circle { double radius; Circle() {#What happens when the code says new radius = 1.0; } Circle(double newRadius) { radius = newRadius; } Notice: no return value – these are not methods! These are constructors. They make a Circle object. 8 Constructors are invoked when we first make an object (a variable) of the type of the Class.

ADDING METHODS A class with only data fields has no life. Objects created from such a class cannot respond to anything. Methods are declared inside the body of the class and after the declaration of data fields and the constructors. The general form of a method declaration is: (just like a function, only it belongs to a certain class) 9 type MethodName (parameter-list) { Method-body; }

ADDING METHODS TO CLASS CIRCLE 10 public class Circle { double radius; // radius of circle double x, y; // center of the circle // …Constructors go here //Methods to return circumference and area public double circumference() { return (2*Math.PI*radius); } public double area() { return (Math.PI * Math.pow(radius,2)); } Method Body

ALL TOGETHER 11 public class Circle { double radius; Circle() { radius = 1.0; } Circle(double d) { radius = d; } public double getArea() { return(Math.pow(radius,2.0) * Math.PI); } public static void main(String[] args) { Circle c = new Circle(3.2); System.out.println(c.getArea()); } Method Body

DATA ABSTRACTION Declare the Circle class, have created a new data type – Data Abstraction Can define variables (objects) of that type: Circle aCircle; Circle bCircle; 12

CREATING CIRCLE OBJECTS CONT. Circle aCircle; Circle bCircle; aCircle, bCircle simply refers to a Circle object, not an object itself. (e.g., this is going to be a Circle, but it isn’t yet.) 13 aCircle Points to nothing (Null Reference) bCircle Points to nothing (Null Reference) null

CREATING OBJECTS OF A CLASS Objects are created using the new keyword. aCircle and bCircle refer to Circle objects 14 bCircle = new Circle() ; aCircle = new Circle() ;

15 CREATING OBJECTS OF A CLASS aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; P aCircle Q bCircle Before Assignment P aCircle Q bCircle After Assignment

AUTOMATIC GARBAGE COLLECTION The object does not have a reference and cannot be used in future. The object becomes a candidate for automatic garbage collection. Java automatically collects garbage periodically and releases the memory used to be used in the future. 16 Q P aCircle Q bCircle

ACCESSING OBJECT/CIRCLE DATA 17 Circle aCircle = new Circle(); aCircle.x = 2.0; // initialize center and radius aCircle.y = 2.0; aCircle.radius = 1.0; ObjectName.VariableName ObjectName.MethodName(parameter-list)

EXECUTING METHODS IN OBJECT/CIRCLE Using Object Methods: 18 Circle aCircle = new Circle(); aCircle.r = 1.0; System.out.println(aCircle.area()); Gets Circle’s method and runs it for this particular circle

USING CIRCLE CLASS // Circle.java: Contains both Circle class and its user class //Add Circle class code here class MyMain { public static void main(String[] args) { Circle aCircle; // creating reference aCircle = new Circle(); // creating object aCircle.x = 10; // assigning value to data field aCircle.y = 20; aCircle.radius = 5; double area = aCircle.area(); // invoking method double circumf = aCircle.circumference(); System.out.println("Radius="+aCircle.r+" Area="+area); System.out.println("Radius="+aCircle.r+" Circumference ="+circumf); } 19 Radius=5.0 Area=78.5 Radius=5.0 Circumference =31.40

ENCAPSULATION : MAKING STUFF PUBLIC AND PRIVATE An object instance owns its state and behavior Java provides access modifiers to define what code can access an object's state and behavior public all code can access the tagged state or behavior private only instances of the enclosing class may access this tagged state or behavior Protected (hold that thought for later…)

EXAMPLE OF PUBLIC VS PRIVATE: public class Circle { private double rad; private double circ; private double area; public Circle() { rad = 1.0; setValues(); } public Circle(double r) { rad = r; setValues(); } private void setValues(){ circ = Math.PI * 2 * rad; area = Math.PI * Math.pow(rad, 2); } public void setrad(double r) { rad = r; setValues(); } public String toString(){ String s = "Radius: "+rad+" Cicumference: "+circ+" Area: "+area; return s; }

WHAT IF? public class Circle { public double rad; private double circ; private double area; public Circle() { rad = 1.0; setValues(); } public Circle(double r) { rad = r; setValues(); } private void setValues(){ circ = Math.PI * 2 * rad; area = Math.PI * Math.pow(rad, 2); } public void setrad(double r) { rad = r; setValues(); } public String toString(){ String s = "Radius: "+rad+" Cicumference: "+circ+" Area: "+area; return s; } public class Hello2 { public static void main (String[] args) { Circle c = new Circle(3.2); System.out.println(c); c.rad = 100; System.out.println(c); } >> Radius: 3.2 Cicumference: 20.1 Area: >> Radius: Cicumference: 20.1 Area: 32.16

THE RULE: All Fields should be PRIVATE!!!! Unless there’s a serious concrete reason why you didn’t make it private Whenever possible, your methods should be private Always go for the most protected state you can make things You’re in control, and you want to control what other people who might be using your code have access to. You want to keep everything private unless it is something that will be used by other people/code that is using your code.