Sunteți pe pagina 1din 0
1 Introduction Introduction to to Programming Programming ( ( Using Using Java) Java) Nandika Nandika

1

IntroductionIntroduction toto ProgrammingProgramming

((UsingUsing Java)Java)

NandikaNandika KasunKasun

UniversityUniversity ofof ColomboColombo SchoolSchool ofof ComputingComputing (UCSC)(UCSC) UniversityUniversity ofof ColomboColombo SriSri LankaLanka

2 Definition Definition of of a(n) a(n) Object/Class Object/Class • What is an Object :

2

DefinitionDefinition ofof a(n)a(n) Object/ClassObject/Class

Definition of of a(n) a(n) Object/Class Object/Class • What is an Object : – A ‘thing’
Definition of of a(n) a(n) Object/Class Object/Class • What is an Object : – A ‘thing’

• What is an Object :

– A ‘thing’ may have a physical presence such as a ‘table’ ,‘chair’ or an abstract concept such as ‘a job’ . – An object is an abstract representation of a ‘thing’ in the real world. – We simulate a problem domain in the real- world through objects.

Objects Objects 3 – An object has a unique identity, attributes (What it knows or

ObjectsObjects

3

– An object has a unique identity, attributes (What it knows or data about it), and behavior (What it can do).

EgEg::

An Employee object (say employee) will have the following attributes (what it knows):

name, age, salary It will have the following behavior (what it can do):

set salary, get salary, set name, set age

Classes Classes 4 • What is a Class? – Objects are the representation of things

ClassesClasses

4

What is a Class?

– Objects are the representation of things in the real world

Classes are templates for objects, ie. They contain all the common properties and behavior of a group of objects.

– All objects with the same characteristics and behaviors belongs to the same class.

characteristics and behaviors belongs to the same class. UCSC 2003. All rights reserved. No part of
characteristics and behaviors belongs to the same class. UCSC 2003. All rights reserved. No part of
characteristics and behaviors belongs to the same class. UCSC 2003. All rights reserved. No part of
characteristics and behaviors belongs to the same class. UCSC 2003. All rights reserved. No part of
characteristics and behaviors belongs to the same class. UCSC 2003. All rights reserved. No part of

UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.

Kasun@Kasun@cmbcmb.ac ac.lklk

5 Java Java Application Application • A Java application is a collection of one or

5

JavaJava ApplicationApplication

• A Java application is a collection of one or more Java classes.

• At least one of the classes is public and contains a method named main() except in case of applets

contains a method named main() except in case of applets UCSC 2003. All rights reserved. No
Objects Objects and and Classes Classes public class Employee { private String name; private int

ObjectsObjects andand ClassesClasses

public class Employee { private String name; private int age; private float salary;

String name; private int age; private float salary; Attributes 6 Methods public void setName(String tName) {

Attributes

private int age; private float salary; Attributes 6 Methods public void setName(String tName) { Name =

6

Methods public void setName(String tName) { Name = tName; } public void setSalary (float tSalary)
Methods
public void setName(String tName) {
Name = tName; }
public void setSalary (float tSalary) {
salary = tSalary; }
public float getSalary () {
return salary; }
}
An example
class Declaration

UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.

Kasun@Kasun@cmbcmb.ac ac.lklk

7 Creating Creating Objects Objects • Objects are created by instantiating classes. • To use

7

CreatingCreating ObjectsObjects

• Objects are created by instantiating classes.

• To use a class in a program, you must first create an instance of it.

• Objects of a class can be created using the newnew operator.

Example

Employee newEmp = new Employee();

Class Name

newEmp = new Employee(); C l a s s N a m e Constructor Variable containing
newEmp = new Employee(); C l a s s N a m e Constructor Variable containing

Constructor

Variable containing an Object reference

8 Creating Creating Objects Objects Object References following declaration will create an Object reference Employee

8

CreatingCreating ObjectsObjects

Object References following declaration will create an Object reference

following declaration will create an Object reference Employee newEmp2 ; The newEmp2 variable will have value

Employee newEmp2 ;

The newEmp2 variable will have value null

You can create multiple References to the same object

Employee newEmp; newEmp = new Employee(); newEmp2 = newEmp;

newEmp; newEmp = new Employee(); newEmp2 = newEmp; newEmp2 newemp2 newEmp newemp UCSC 2003. All rights

newEmp2

newEmp; newEmp = new Employee(); newEmp2 = newEmp; newEmp2 newemp2 newEmp newemp UCSC 2003. All rights
newEmp; newEmp = new Employee(); newEmp2 = newEmp; newEmp2 newemp2 newEmp newemp UCSC 2003. All rights
newEmp; newEmp = new Employee(); newEmp2 = newEmp; newEmp2 newemp2 newEmp newemp UCSC 2003. All rights
newEmp; newEmp = new Employee(); newEmp2 = newEmp; newEmp2 newemp2 newEmp newemp UCSC 2003. All rights
newEmp; newEmp = new Employee(); newEmp2 = newEmp; newEmp2 newemp2 newEmp newemp UCSC 2003. All rights
newEmp; newEmp = new Employee(); newEmp2 = newEmp; newEmp2 newemp2 newEmp newemp UCSC 2003. All rights
newEmp; newEmp = new Employee(); newEmp2 = newEmp; newEmp2 newemp2 newEmp newemp UCSC 2003. All rights
newEmp; newEmp = new Employee(); newEmp2 = newEmp; newEmp2 newemp2 newEmp newemp UCSC 2003. All rights

newemp2

newEmp

= new Employee(); newEmp2 = newEmp; newEmp2 newemp2 newEmp newemp UCSC 2003. All rights reserved. No

newemp

UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.

Kasun@Kasun@cmbcmb.ac ac.lklk

9 Destroying Destroying Objects Objects • Java’s garbage collection is automatic • There is no

9

DestroyingDestroying ObjectsObjects

• Java’s garbage collection is automatic

• There is no great need for an explicit destructor. finalize method plays the role of a destructor.

• This method is useful to disconnect network connections as well as close any open databases

network connections as well as close any open databases UCSC 2003. All rights reserved. No part
10 Objects Objects and and Classes Classes A basic method definition is as follows: Optional

10

ObjectsObjects andand ClassesClasses

A basic method definition is as follows:

Optional

Classes A basic method definition is as follows: Optional Can be any primitive type or a
Classes A basic method definition is as follows: Optional Can be any primitive type or a

Can be any primitive type or a class Name or void (no return statement)

modifier returnType MethodName (parameter List) { statement(s);

}

Usually the modifier for methods is public and for attributes it is private

11 Objects Objects and and Classes Classes • The return Type, Method Name , and

11

ObjectsObjects andand ClassesClasses

• The return Type, Method Name, and the parameter list defines the Signature of the method • It is possible to define two or more methods with the same name within the same class (Method Overloading) with different signatures

public void CreatePoint( ) public void CreatePoint(int x , y)

void CreatePoint( ) public void CreatePoint(int x , y) UCSC 2003. All rights reserved. No part
12 Objects Objects and and Classes Classes Accessing methods a instance method can be accessed

12

ObjectsObjects andand ClassesClasses

Accessing methods a instance method can be accessed using the dot(.) operator as shown below.

ObjectName.method();

A class method can be accessed by using the class name followed by a period and then the method name

Integer.parseInt(“25”);

13 Objects Objects and and Classes Classes Passing parameters – all parameters (except Object types

13

ObjectsObjects andand ClassesClasses

Passing parameters

– all parameters (except Object types such as instances of classes and Arrays etc.) are passed by value.

– i.e. any modifications done within the method does not affect the original variable.

within the method does not affect the original variable. –if you require to modify the original
within the method does not affect the original variable. –if you require to modify the original
within the method does not affect the original variable. –if you require to modify the original

–if you require to modify the original variables, and needs to pass those to a method

•Declare those as instance variables in a class and pass an object of that class to the method

Constructors Constructors Constructor Methods – These methods are used to initialize objects – they have

ConstructorsConstructors

Constructor Methods

– These methods are used to initialize objects

– they have the same name as the class and have no return type

– these methods are called automatically when the new operator is used to allocate memory for an object.

new operator is used to allocate memory for an object. 14 UCSC 2003. All rights reserved.

14

Constructors Constructors • Overloading Constructors 15 – A class can have multiple Constructors (Overloaded

ConstructorsConstructors

Overloading Constructors

15

– A class can have multiple Constructors (Overloaded Constructors)

– All carries the same name

– They have either different number of arguments or different types of arguments

number of arguments or different types of arguments UCSC 2003. All rights reserved. No part of

UCSC 2003. All rights reserved. No part of this material may be reproduced and sold.

Kasun@Kasun@cmbcmb.ac ac.lklk

16 Constructors Constructors Example for Constructors public class twoDPoint { float x,y; twoDPoint ( )

16

ConstructorsConstructors

Example for Constructors public class twoDPoint { float x,y; twoDPoint ( ) { x= y = 0;

}

}

No argument Constructor

twoDPoint ( ) { x= y = 0; } } No argument Constructor UCSC 2003. All
twoDPoint ( ) { x= y = 0; } } No argument Constructor UCSC 2003. All
Constructors Constructors 17 Example for Constructors public class twoDPoint { float x,y; twoDPoint ( )

ConstructorsConstructors

17

Example for Constructors

public class twoDPoint { float x,y; twoDPoint ( ) { x= y = 0;

class twoDPoint { float x,y; twoDPoint ( ) { x= y = 0; } No argument

}

No argument Constructor

}

twoDPoint ( float a , float b) { x=a;

y=b;

Constructor } twoDPoint ( float a , float b) { x=a; y=b; } Overloaded constructor UCSC

}

Overloaded

constructor

This This Keyword Keyword this keyword – this keyword is used to refer to the

ThisThis KeywordKeyword

this keyword

This This Keyword Keyword this keyword – this keyword is used to refer to the current

this keyword is used to refer to the current object.

– this keyword is used to refer to the current object. It can be used to

It can be used to :

•refer to the current objects member variables

•refer to current objects methods

18

•pass a reference to the current object to a method

•return a reference current object

19 This This Keyword Keyword • this keyword Refers to the member variable x in

19

ThisThis KeywordKeyword

this keyword

Refers to the member variable x in this object

t=this.x; this.myMethod(this); return this;

this object t=this.x; this.myMethod(this); return this; Call the myMethod defined in this class and pass its
this object t=this.x; this.myMethod(this); return this; Call the myMethod defined in this class and pass its
this object t=this.x; this.myMethod(this); return this; Call the myMethod defined in this class and pass its

Call the myMethod defined in this class and pass its this Object

Return this Object

• super keyword Super Super Keyword Keyword 20 – This keyword is used to refer

super keyword

SuperSuper KeywordKeyword

20

– This keyword is used to refer to the Super or parent class

super( );

Invokes super class Constructor

or parent class super( ); Invokes super class Constructor Invokes super class Constructor with x and

Invokes super class Constructor with x and y arguments

Invokes super class Constructor with x and y arguments super(x,y); super.f( ); Calls the super class

super(x,y);

super.f( );

Calls the super class function called f( )

super.f( ); Calls the super class function called f( ) UCSC 2003. All rights reserved. No
Summary Summary 21 This week we learned Objects and Classes Exercise: Exercise: Implement Implement Java

SummarySummary

21

This week we learned Objects and Classes

Exercise:Exercise:

ImplementImplement JavaJava objectsobjects forfor representrepresent aa Chair,Chair, aa CarCar andand aa Student.Student. YouYou shouldshould definedefine attributesattributes andand methodsmethods ofof thesethese objects.objects.