+ All Categories
Home > Documents > IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36...

IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36...

Date post: 16-Jul-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
36
METHODS (CONSTRUCTORS)
Transcript
Page 1: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

METHODS (CONSTRUCTORS)

Page 2: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

26

What is Constructor?

• Is a special type of subroutine called to

create an object

• Prepares the new object for use

• Is a public method

• Member method/function

• Name must be the class name

Constructors

Page 3: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

27

1. Is a public method

2. Name must be the class

name

1 2

Constructors

Page 4: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

28 Constructors

Page 5: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Multiple constructors

• You can create multiple constructors, each must accept different parameters. – Student(){ }

– Student(int age, String Name) { }

– Student (Student stuob) { }

• If you don't write any constructor, the compiler will (in effect) write one for you:

– classname() {}

– Student()

• If you include any constructors in a class, the compiler will not create a default constructor!

29 Constructors

Page 6: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example-Multiple constructors

30 Constructors

Page 7: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example-Multiple constructors

31 Constructors

Page 8: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

32

Multiple Constructors cont.…

• One constructor can call another.

• Use "this", not the classname:

A call to this must be the

first statement in the constructor!

Constructors

Page 9: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Summary

• Is a special type of public method called to create an object

• can create multiple constructors, each must accept

different parameters.

– Student(){ }

– Student(int age, String Name) { }

– Student (Student stuob) { }

• One constructor can call another.

33 Constructors

Page 10: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

• Create a Java Class “Student” with following

constructors

– Student();

– Student(Name, Age, IndexNo)

• Create a class name “Length” with following

constructors

– Length()

– Length(Feets, Inches )

34

Page 11: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

• Complete the class ‘Date’ with following methods

– Date() // 0,0,0

– Date(day, month, year);

– Date(Date d)

– Input()

– print() // 23.5.2015

– printLong() // 23rd may 2015

7/11/2015 Budditha Hettige ([email protected]) 35

Page 12: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Java Programming: OOP 36

Destructors

• No!

• There is a finalize() method that is called when

an object is destroyed.

– you don't have control over when the object is

destroyed (it might never be destroyed).

– The JVM garbage collector takes care of

destroying objects automatically (you have limited

control over this process).

Page 13: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

toString Method

• Returns a string representation of the object

• Example

7/11/2015 Budditha Hettige ([email protected]) 37

Page 14: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

• Create a Java class Name with following variables and methods

– 3 private variables to represent first name, middle name and last name

– Relevant constructors

– toString method to print full name

– A method name “namewithins() that return name with initials. example (S. K. SILVA)

– Implement main method to demonstrate name class capabilities.

7/11/2015 Budditha Hettige ([email protected]) 38

Page 15: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

• Complete the all previously created class with the

following methods

– 3 Constructors

• Example Date(), Date(int,int,int), Date(date)

– toString method

– Input method

– Print method

• Update main method of the Application class to

demonstrate each class capabilities

7/11/2015 Budditha Hettige ([email protected]) 39

Page 16: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Encapsulation

40

Page 17: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Java Programming: OOP 41

Encapsulation

• Information Hiding.

• Don't need to know how some component is

implemented to use it.

• Implementation can change without effecting any

calling code.

• "protects us from ourselves"

Page 18: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Encapsulation

• Is the technique of making the fields in a class private and providing access to the fields via public methods

• Is also referred to as data hiding

• Benefit

– is the ability to modify our implemented code without breaking the code of others who use our code

• Encapsulation gives maintainability, flexibility and extensibility to our code

42

Page 19: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Public access

43

public class Test

{

public float money;

public String TV;

public void work ()

{

}

Page 20: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Private access

44

public class Test

{

private float money;

private String TV;

public void work ()

{

}

}

Page 21: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

GET Methods

45

public int getData() {

return data;

}

Page 22: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

SET Methods

46

public void setData(int nd)

{ data = nd; }

Page 23: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

public class Encap

{

private String name;

private String idNum;

private int age;

public int getAge()

{

return age;

}

public String getName()

{

return name;

}

47

public String getIdNum()

{

return idNum;

}

public void setAge( int newAge)

{

age = newAge;

}

public void setName(String newName)

{

name = newName;

}

public void setIdNum( String newId)

{

idNum = newId;

}

}

Page 24: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

public class RunEncap

{

public static void main(String args[])

{

EncapTest encap = new EncapTest();

encap.setName("James");

encap.setAge(20);

encap.setIdNum("12343ms");

System.out.println("Name : " + encap.getName()+ " Age : "+ encap.getAge());

}

}

48

Page 25: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Benefits of Encapsulation:

• The fields of a class can be made read-only or write-

only.

• A class can have total control over what is stored in

its fields.

• The users of a class do not know how the class

stores its data. A class can change the data type of a

field, and users of the class do not need to change

any of their code.

49

Page 26: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

50

Composition

Page 27: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Composition…?

• Way to combine simple objects or data types

into more complex ones

51

Java Primitive data types

– byte

– short

– int

– long

– float

– double

– boolean

– char

Complex Data types/Objects

– Name

– Date

– Student

– Account

– Course

– Results

Page 28: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Composition 52

Composition

• is a way to combine simple objects or data types

into more complex ones

• A composition relation exists between two classes if

one classes’ field(s) consist of another class

• Called as has-a relationship

Page 29: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

HAS-A Relationship

• Relationships are mainly based on the usage

• Determines whether a certain class HAS-A certain

thing

53

Page 30: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example (Date)

• Create a Date class to represent day, month,

and year for a date.

54

Date

Day

Month

Year

Page 31: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example (Student)

• Create a class Student with student name, date of

birth and group

55

Student has-a Date of birth

class field consist of another class

Page 32: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

• Complete the following class

7/11/2015 Budditha Hettige ([email protected]) 56

Page 33: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

7/11/2015 Budditha Hettige ([email protected]) 57

• Complete the following class

Page 34: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Required Methods for a each class

• Constructor(s)

• Set and Get methods

• toString Method

• Input method (from keyboard)

• Print method(s)

• Other required methods

7/11/2015 Budditha Hettige ([email protected]) 58

Compulsory Methods

Page 35: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Example

1. Create a class name customer including following data fields and methods

– Data fields • Name • Full name • Address • NIC No • Date of Birth • Sex • Email Address

– Methods • Get Methods … name, NIC etc. • Set methods … • Constructors, • PrintCustomer

59

Page 36: IT203 OOP Introduction to OOP SJP ART - WordPress.com · 3/2/2015  · Java Programming: OOP 36 Destructors • No! • There is a finalize() method that is called when an object

Exercise

• Complete the following classes with all relavent methods

– Student

– Employee

– Person

– Vehicle

– Book

• Use Composition as required

• Update main method of the Application class to demonstrate each class capabilities

7/11/2015 Budditha Hettige ([email protected]) 60


Recommended