1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.

Slides:



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

Classes and Objects in Java
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Tutorial 6 February 4th/5th, 2015
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Introduction to Java Programming, 4E Y. Daniel Liang.
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.
Internet Software Development Classes and Inheritance Paul J Krause.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
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 Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0.
Topic: Classes and Objects
Creating Your Own Classes
Static data members Constructors and Destructors
Final and Abstract Classes
Object Oriented Programming
Classes and Objects in Java
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects in Java
Classes and Objects in Java
Chapter 9 Objects and Classes
Classes Lecture 7 from Chapter /1/11.
Constructors, GUI’s(Using Swing) and ActionListner
Classes, Objects and Methods
Final and Abstract Classes
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Chapter 7 Objects and Classes
Presentation transcript:

1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java

2 Contents Introduce to classes and objects in Java. Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes.

3 Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Circle centre radius circumference() area()

4 Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. The basic syntax for a class definition: Bare bone class – no fields, no methods public class Circle { // my circle class } class ClassName [extends SuperClassName] { [fields declaration] [methods declaration] }

5 Adding Fields: Class Circle with fields Add fields The fields (data) are also called the instance varaibles. public class Circle { public double x, y; // centre coordinate public double r; // radius of the circle }

6 Adding Methods A class with only data fields has no life. Objects created by such a class cannot respond to any messages. Methods are declared inside the body of the class but immediately after the declaration of data fields. The general form of a method declaration is: type MethodName (parameter-list) { Method-body; }

7 Adding Methods to Class Circle public class Circle { public double x, y; // centre of the circle public double r; // radius of circle //Methods to return circumference and area public double circumference() { return 2*3.14*r; } public double area() { return 3.14 * r * r; } Method Body

8 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;

9 Class of Circle cont. aCircle, bCircle simply refers to a Circle object, not an object itself. aCircle Points to nothing (Null Reference) bCircle Points to nothing (Null Reference) null

10 Creating objects of a class Objects are created dynamically using the new keyword. aCircle and bCircle refer to Circle objects bCircle = new Circle() ; aCircle = new Circle() ;

11 Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle;

12 Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; P aCircle Q bCircle Before Assignment P aCircle Q bCircle Before Assignment

13 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. Q

14 Accessing Object/Circle Data Similar to C syntax for accessing data defined in a structure. Circle aCircle = new Circle(); aCircle.x = 2.0 // initialize center and radius aCircle.y = 2.0 aCircle.r = 1.0 ObjectName.VariableName ObjectName.MethodName(parameter-list)

15 Executing Methods in Object/Circle Using Object Methods: Circle aCircle = new Circle(); double area; aCircle.r = 1.0; area = aCircle.area(); sent ‘message’ to aCircle

16 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.r = 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); } java MyMain Radius=5.0 Area=78.5 Radius=5.0 Circumference =

17 Refer to the Earlier Circle Program // 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.r = 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); } java MyMain Radius=5.0 Area=78.5 Radius=5.0 Circumference =

18 Better way of Initialising or Access Data Members x, y, r When there too many items to update/access and also to develop a readable code, generally it is done by defining specific method for each purpose. To initialise/Update a value: aCircle.setX( 10 ) To access a value: aCircle.getX() These methods are informally called as Accessors or Setters/Getters Methods.

19 Accessors – “Getters/Setters” public class Circle { public double x,y,r; //Methods to return circumference and area public double getX() { return x;} public double getY() { return y;} public double getR() { return r;} public double setX(double x_in) { x = x_in;} public double serY(double y_in) { y = y_in;} public double setR(double r_in) { r = r_in;} }

20 How does this code looks ? More readable ? // 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.setX(10); aCircle.setY(20); aCircle.setR(5); double area = aCircle.area(); // invoking method double circumf = aCircle.circumference(); System.out.println("Radius="+aCircle.getR()+" Area="+area); System.out.println("Radius="+aCircle.getR()+" Circumference ="+circumf); } java MyMain Radius=5.0 Area=78.5 Radius=5.0 Circumference =

21 Object Initialisation When objects are created, the initial value of data fields is unknown unless its users explicitly do so. For example, ObjectName.DataField1 = 0; // OR ObjectName.SetDataField1(0); In many cases, it makes sense if this initialisation can be carried out by default without the users explicitly initialising them. For example, if you create an object of the class called “Counter”, it is natural to assume that the counter record-keeping field is initialised to zero unless otherwise specified differently. class Counter { int CounterIndex; … } Counter counter1 = new Counter(); What is the value of “counter1.CounterIndex” ? In Java, this can be achieved though a mechanism called constructors.

22 What is a Constructor? Constructor is a special method that gets invoked “automatically” at the time of object creation. Constructor is normally used for initializing objects with default values unless different values are supplied. Constructor has the same name as the class name. Constructor cannot return values. A class can have more than one constructor as long as they have different signature (i.e., different input arguments syntax).

23 Defining a Constructor Like any other method Invoking: There is NO explicit invocation statement needed: When the object creation statement is executed, the constructor method will be executed automatically. public class ClassName { // Data Fields… // Constructor public ClassName() { // Method Body Statements initialising Data Fields } //Methods to manipulate data fields }

24 Defining a Constructor: Example public class Counter { int CounterIndex; // Constructor public Counter() { CounterIndex = 0; } //Methods to update or access counter public void increase() { CounterIndex = CounterIndex + 1; } public void decrease() { CounterIndex = CounterIndex - 1; } int getCounterIndex() { return CounterIndex; }

25 Trace counter value at each statement and What is the output ? class MyClass { public static void main(String args[]) { Counter counter1 = new Counter(); counter1.increase(); int a = counter1.getCounterIndex(); counter1.increase(); int b = counter1.getCounterIndex(); if ( a > b ) counter1.increase(); else counter1.decrease(); System.out.println(counter1.getCounterIndex()); }