Title: Software Technolgy - I
1Software Technolgy - I
Classes Objects Methods
2What is an Object?
An object is a software bundle of related
variables and methods. Software objects are
often used to model real-world objects you find
in everyday life.
3What is a Class?
A class is a blueprint or prototype that defines
the variables and the methods common to all
objects of a certain kind.
class Person String name int age
void setName(String _name) name _name
.... ....
4A Closer Look
class Person String name int age
void setName(String _name) name _name
void setAge(int _age) age
_age void showInfo()
System.out.println(name " is " age " years
old.")
Variables (define state)
Methods (define behavior)
5Defining Methods
return-type methodName(parameter-list)
statements
int factorial(int number) int temp number
// A local variable int value 1 // Store the
result while(temp gt 1) value
temp temp temp - 1 return value
A Simple method
6Exercise
- Define a method that will accept an integer (say
n) and return thefollowing sum1 2 3 4
... n - You have written a part of a class which
describes a Person. ThePerson class has two data
items name and age. Write a methodcalled
showInfo that simply prints tha name and age of
an instance(object) of a Person class.Hints
The method returns nothing. Hence the return
type is "void". The method accepts nothing.
Hence it is simply showInfo().
7(No Transcript)
8(No Transcript)
9(No Transcript)
10Creating and Accessing Members
- "new" operator is used to instantiate (create) an
object from a class. - Dot operator (".") is used to access members.
public static void main(String args) Person
person1 new Person() person1.setInfo("Nilantha
", 1) person1.showInfo() Person person2
new Person() person2.name "Kannan" person2.a
ge 2 person2.showInfo() // end main
Declare and instantiate
Pass messages (accessing methods)
Direct access of data members
11Constructors
A constructor is a special method whose name is
the same name of the class. JVM automatically
calls an appropriate constructor while
it instantiate an object from a class. No return
type is specified for the Constructors.
class Person String name byte
age Person(String name, byte age)
this.name name this.age
age ...... // rest methods
A constructor that initialize the name and age
12Calling Constructors
class PersonTest public static void
main(String args) Person person1 new
Person("Kirihonda", 15) person1.showInfo()
If you atleast define one constructor for a class
of your own, you cannot simply call the default
constructor. For example you cannot say person2
new Person() in the above example. Check the
next slide for a remidy.
13Overloading Methods
Overloading allows to use the same method name
for different behavior.
class Person String name byte age void
setInfo(String name, byte age) this.name
name this.age age void setInfo(String
name) this.name name ...
Two overloaded methods Method signatures are
different
14Method Overloading Exercise
- Complete the above Person class with an
additional setInfo methodthat will only set the
age of a person. - Write two oveloaded constructors for the Person
class 1. Just the default constructor (for you
to instantiate objects without any
information). 2. A constructor which sets the
name and age.
15UML Class Diagram
16Using the Person Class
public static void main(String args) Person
person1 new Person() // Default
constructor person1.setInfo("Nilantha",
1) person1.showInfo() person1.setInfo(2) per
son1.showInfo() Person person2 new
Person("Kannan", 3) person2.showInfo() person
2.setInfo("Kamal") person2.showInfo() // end
main
17Static Members
If you define a variable/method to be static and
create several objects,JVM will only allocate
memory for a single copy of the
variable/methodand share this allocated memory
among all the objects. You can access static
variables/methods without creating objects
fromthe class. Static variables are mostly used
to define constants. Example static final
float PI 3.14f Methods are declared static if
the method belongs to a class but does
not manipulate non-static data (variables) of the
class. Example static int factorial(int n)
18A Class with Static Members
class Maths static final float PI
3.14f static int factorial(int n) if(n
1 n 0) // fatorial of 0 and 1 is
1 return 1 else if (n lt 0) // No
factorial for negative numbers return 0 //
just return 0 to indicate an error else
return n factorial(n - 1)
Static methods does not access instance
variables/methods.
19Using the Maths Class
public static void main(String args)
System.out.println("Value of PI is "
Maths.PI) int number 5 int factorial
Maths.factorial(number) System.out.println("Fact
orial of " number " is " factorial)
We did not used "new Maths()" here!
20Exercise
class A static int i class B public
static void main(String args) A first new
A() first.i 2 A second new
A() second.i 5 System.out.println(first.
i)
What will be the output you get?
21Instance/Class Members
class Circle static final float PI
3.14f float radius Circle(float _radius)
radius _radius float getArea()
return PI radius radius static
float getArea(float _radius) return PI
_radius _radius
Class variables/methods
Instance variables/methods
22Exercise
Go through the Bill Program given to you and draw
the class diagrams. Identify the instance/class
variables and methods. Compile the program and
modify it as you wish. Have some fun!