METHODS
(CONSTRUCTORS)
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
Example
27
1. Is a public method
2. Name must be the class
name
1 2
Constructors
Example
28
Constructors
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
Example-Multiple constructors
30
Constructors
Example-Multiple constructors
31
Constructors
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
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
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
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 (budditha@yahoo.com) 35
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).
toString Method
• Returns a string representation of the object
• Example
7/11/2015 Budditha Hettige (budditha@yahoo.com) 37
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 (budditha@yahoo.com) 38
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 (budditha@yahoo.com) 39
Encapsulation
40
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"
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
Public access
43
public class Test
{
public float money;
public String TV;
public void work ()
{
}
Private access
44
public class Test
{
private float money;
private String TV;
public void work ()
{
}
}
GET Methods
45
public int getData()
{
return data;
}
SET Methods
46
public void setData(int nd)
{
data = nd;
}
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;
}
}
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
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
50
Composition
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
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
HAS-A Relationship
• Relationships are mainly based on the usage
• Determines whether a certain class HAS-A certain
thing
53
Example (Date)
• Create a Date class to represent day, month,
and year for a date.
54
Date
Day
Month
Year
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
Example
• Complete the following class
7/11/2015 Budditha Hettige (budditha@yahoo.com) 56
Example
7/11/2015 Budditha Hettige (budditha@yahoo.com) 57
• Complete the following class
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 (budditha@yahoo.com) 58
Compulsory Methods
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
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 (budditha@yahoo.com) 60

02-advance-methods-on-class jdpgs code .pdf

  • 1.
  • 2.
    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
  • 3.
    Example 27 1. Is apublic method 2. Name must be the class name 1 2 Constructors
  • 4.
  • 5.
    Multiple constructors • Youcan 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
  • 6.
  • 7.
  • 8.
    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
  • 9.
    Summary • Is aspecial 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
  • 10.
    Example • Create aJava Class “Student” with following constructors – Student(); – Student(Name, Age, IndexNo) • Create a class name “Length” with following constructors – Length() – Length(Feets, Inches ) 34
  • 11.
    Example • Complete theclass ‘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 (budditha@yahoo.com) 35
  • 12.
    Java Programming: OOP36 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).
  • 13.
    toString Method • Returnsa string representation of the object • Example 7/11/2015 Budditha Hettige (budditha@yahoo.com) 37
  • 14.
    Example • Create aJava 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 (budditha@yahoo.com) 38
  • 15.
    Example • Complete theall 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 (budditha@yahoo.com) 39
  • 16.
  • 17.
    Java Programming: OOP41 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"
  • 18.
    Encapsulation • Is thetechnique 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
  • 19.
    Public access 43 public classTest { public float money; public String TV; public void work () { }
  • 20.
    Private access 44 public classTest { private float money; private String TV; public void work () { } }
  • 21.
    GET Methods 45 public intgetData() { return data; }
  • 22.
    SET Methods 46 public voidsetData(int nd) { data = nd; }
  • 23.
    Example public class Encap { privateString 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; } }
  • 24.
    Example public class RunEncap { publicstatic 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
  • 25.
    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
  • 26.
  • 27.
    Composition…? • Way tocombine 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
  • 28.
    Composition 52 Composition • isa 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
  • 29.
    HAS-A Relationship • Relationshipsare mainly based on the usage • Determines whether a certain class HAS-A certain thing 53
  • 30.
    Example (Date) • Createa Date class to represent day, month, and year for a date. 54 Date Day Month Year
  • 31.
    Example (Student) • Createa class Student with student name, date of birth and group 55 Student has-a Date of birth class field consist of another class
  • 32.
    Example • Complete thefollowing class 7/11/2015 Budditha Hettige (budditha@yahoo.com) 56
  • 33.
    Example 7/11/2015 Budditha Hettige(budditha@yahoo.com) 57 • Complete the following class
  • 34.
    Required Methods fora 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 (budditha@yahoo.com) 58 Compulsory Methods
  • 35.
    Example 1. Create aclass 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
  • 36.
    Exercise • Complete thefollowing 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 (budditha@yahoo.com) 60