Title: IT1202Fundamentals Of Programming Object Oriented Concepts and Techniques
1IT1202-Fundamentals Of ProgrammingObject
Oriented Concepts and Techniques
2Introduction to Object Oriented Programming
- The idea of Object Oriented
- Programming is
- organize your programs in a way
- that mirrors the way objects are
- organized in the real world
-
3Thinking in terms of Objects
- This is a way to look at a program as a set of
objects that work together in predefined ways to
accomplish tasks - Using OOP, your overall program is made up of
different components called objects
4An Object ?
- An object is a self-contained element of a
computer program that represents a related group
of features and is designed to accomplish
specific tasks - Also known as instances
- Each object has a specific role in a program, and
all objects can work with other objects in
defined ways
5Object
- An Employee object (say employee1)
- 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
6A Class
- Definition A template used to create
- multiple objects with
similar - features.
- Classes embody all features (attributes
behaviour) of a particular set of objects - When writing a program in an OO language, you do
not need to define individual objects - Instead, define classes of objects (template)
7A Class
- eg A Tree that describes the features of all
trees - Has leaves roots
- Grows
- Creates chlorophyll
- The Tree class serves as an abstract model for
the concept of a tree
8Class.
- Some objects that
- will be in tree class
9Class..
WashingMachine
Attributes
brandName modelName serialNumber capacity
Objects
A Class
addClothes() addDetergent() removeClothes()
methods
10Attributes and Behavior
- Every class written in java is made up of two
components - Attributes and Behavior
- Attributes
- Individual things that differentiate one class
of objects from another - Determine the appearance, state and other
qualities of that class - eg Color orange, raw amber, lemon yellow, maize
11Attributes and Behavior.
- Behavior of a Class of Objects
- Behavior is the way that a class of objects can
act to change their attributes
12Behavior.
- Examples for behavior
- Get angry
- Calm down
- Eat an ice cream
- Behavior for a class of objects is done by
- using methods
13Example of a class (template for an object)
- public class Employee
- private String name
- private int age
- private float salary
- public void setName(String tName)
- Name tName
-
- public void setSalary (float tSalary)
- salary tSalary
- public float getSalary ()
- return salary
Attributes
Methods
14Creating a Class..
- Open the text editor to create Java programs.
- Start with class definition
- eg
- class Jabberwock
-
- Then create instance variables
- eg
- String color
- boolean hungry
15Creating a Class.
- After that the programmer can add behavior to the
class by adding methods - eg
- void feedJabberwock()
- if (hungry true)
- System.out.println(yum an ice
cream) - hungry false
- else
- System.out.println(No, thanks I
have already eaten) -
16Creating Objects
- 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 new
operator
17Creating objects..
- Example
- Employee newEmp new Employee()
Constructor
Variable containing an Object reference
Class Name
18Methods
- Methods are group of related statements in a
class of objects that act on themselves and on
other classes of objects - Used to accomplish specific tasks
- Objects communicate with each other using methods
19Defining methods
Can be any primitive type or a class Name or void
(no return statement)
Optional
modifier returnType MethodName (parameter List)
statement(s)
20Methods..
- 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 - Methods are two types - instance methods and
class methods
21Methods..
1.public void CreatePoint( ) 2.public void
CreatePoint(int x , int y)
22Methods..
An 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)
23What is a constructor?
- Constructor is also a method
- It is a special method
- Method name same as the class name
- No return type
- It allocates memory for the instance object
- called automatically when an instance is
created/constructed - If not defined still and object will be created
- If defined variables can be initialized in your
own way
24this and super keywords
- this keyword
- this keyword is used to refer to the current
object
- It can be used to
- Refer to the current objects member variables
- Refer to current objects methods
- Pass a reference to a method of the current
object - Return a reference from the current object
25this keyword
Refers to the member variable x in this object
tthis.x this.myMethod(this) return this
Calls the myMethod defined in this class and
pass it to this Object
Return this Object
26super keyword
- super keyword is used to refer to the
- Super or parent class
Invokes super class Constructor
super( ) super(x,y) super.f( )
Invokes super class Constructor with x and y
arguments
Calls the super class function called f( )
27Object Oriented Concepts
- Three basic concepts underlining Object Oriented
Programming - Data Abstraction with Encapsulation
- Inheritance
- Polymorphism
28Encapsulation
- Enables you to hide, inside the object, both the
data fields and the methods that act on that data
29Encapsulation..
- Object CAR
- Attributes
- speed,direction
- Number of people it can carry
- Methods
- Steer() ,
- PressGasPedal(),
- PressBreak()
- Loadpeople()
30Encapsulation..
- Encapsulation is achieved by using Modifiers
- Modifiers are Language Keywords that modify the
definition of a Class, Method or a Variable - Access to variables and methods in Java classes
is accomplished through Access Modifiers - Access modifiers define varying levels of access
between class members and the outside world
(other objects)
31Encapsulation..
- Access modifiers are declared immediately before
the type of a member variable or the return type
of a method - modifier returnType MethodName (parameter List)
- There are four access modifiers
- Default (package access)
- Public
- Protected
- Private
32Inheritance
- One class of objects inheriting the data and
behavior from another class of objects
33Inheritance
Class Car direction
position speed
Steer() PressGasPedal()
PressBreak()
Data members/ Attributes
Methods
34Inheritance
Suppose you want to create a new car which has a
special passing gear
Class PassingCar inherits from Car
Pass()
Pass() is a method that implements the new
functionality
35Inheritance
- Through inheritance you can express the
differences among related classes - You share the functions and member variables that
implement the common features - Inheritance also helps you to reuse existing code
from one or more classes by simply deriving a new
class from them
36Inheritance
- Inheritance is the creation of one class by
extending another class so that instances of the
new class automatically inherit the fields and
methods of its parent class - Inheritance promotes the class Reusability
- It is because an existing class can be sub
classed through inheritance and any modifications
necessary could be applied to the sub class
(Overriding)
37Inheritance
- In Java the extends Keyword is used to achieve
Inheritance
public class threeDPoint extends twoDPoint
sub class
super class
Java does not Support Multiple Inheritance
38Inheritance
- A class that cannot be instantiated is known as
an Abstract Class - An Abstract Class
- is defined to be extended to sub classes
- uses abstract Keyword in the Definition
39Inheritance
abstract class Shape abstract Point Center(
) abstract double Diameter( ) abstract double
Area( )
Can be used to create any shape Such as Circle
or Square
40Polymorphism
- Combines Greek Words
- Poly , Morphism
- Poly Many
- Morphism Forms
- The quality of having more than one form
41Polymorphism
- In the context of Object Oriented Programming,
polymorphism refers to the fact - A single operation can have different behavior in
different objects
42Polymorphism
- It allows different forms of the same service to
be defined - There are two common ways of implementing
Polymorphism. Overloading, Overriding - Overloading - Using the same method name with
different parameter type lists within the same
class - Overriding - Using different implementations of
the same method in sub classes
43Lets consider some examples which are
illustrating some of the concepts of object
Orientation
44Lets simulate a box in Java
Depth
Height
Width
45Lets simulate a box in Java
Introduce the class
class Box
46Lets simulate a box in Java
Introduce instance variable
class Box double width double height double
depth
47Lets simulate a box in Java
Save and compile
class Box double width double height double
depth
48Lets simulate a box in Java
Some important notes
- In the class the main() method is not written
- Class is just a template
- During designing one creates a class
- During Run time objects are created
49Lets simulate a box in Java
Using the class Box
- Create a class which is having the main() method
(our program) - Declare a variable of the class Box
- Use them in the program
50Lets simulate a box in Java
Using the class Box
class BoxDemo public static void main(String
args)
51Lets simulate a box in Java
Declare a variable of Box with values
class BoxDemo public static void main(String
args)
Box myBox new
Box() myBox.width3 myBox.height5 myBox.depth
4
52Lets simulate a box in Java
Using the class Box
class BoxDemo public static void main(String
args)
Box myBox new
Box() myBox.width3 myBox.height5 myBox.depth
4
myBox
53Lets simulate a box in Java
Lets calculate the volume of the myBox
class BoxDemo public static void main(String
args)
Box myBox new Box() myBox.width3 myB
ox.height5 myBox.depth4 System.out.println(myB
ox.width myBox.heightmyBox.depth)
54Lets simulate a box in Java
new keyword
- Box myBox new Box()
- Java does three things when new is used
- Allocate memory for the object
- Initialize that objects instance variables,
either to initial values or to a default(0 for
numbers,null for objects, false for Booleans
,\0 for characters) - Calls the constructor method of the class which
might be one of several methods
55Lets simulate a box in Java
Initialization
- myBox.width3
- myBox.height5
- myBox.depth4
- When the variables are initialized of the myBox
instance object - width 3, height 5, depth 4
56Lets simulate a box in Java
The output
- System.out.println(myBox.width
myBox.heightmyBox.depth) - When this statement is executed the volume of my
box will be calculated and will output to the
screen
57Lets simulate a box in Java
When the program executed
58Lets simulate a box in Java
Methods
- Methods written to manipulate data
- Methods written to allocate memory(constructors)
59Lets simulate a box in Java
Add a method to the class Box
- To calculate the volume lets create a method
- Name of the method is calcVol()
- No parameters are used
- No returning values
-
60Lets simulate a box in Java
Add a method to the class Box
How do we write a method? Return type
methodName(Parameter list) Body of the method
61Lets simulate a box in Java
Add a method to the class Box
void CalcVol( ) System.out.println( width
height depth)
resultant value will be out put to the screen
62Lets simulate a box in Java
Add a method to the class Box
class Box double width double height double
depth void CalcVol() System.out.println( width
height depth)
63Lets simulate a box in Java
Add a method to the class Box
- Save the file Box.java
- Compile the file
- But you can not execute the file
- because this is a class file( no main())
64Lets simulate a box in Java
Add a method to the class Box
- How do we use methods(call methods)
- We have to call methods from our program
- Our program name is BoxDemo
- When we call the method we have to refer to the
method using the instance name and dot operator
65Lets simulate a box in Java
Use a method in the BoxDemo
66Lets simulate a box in Java
What is a constructor?
- Constructor is also a method
- It is a special method
- Method name same as the class name
- No return type
- It allocates memory for the instance object
- Called automatically when an instance is
created/constructed - If not defined an object will be created
- If defined, variables can be initialized
according to your requirements
67Lets simulate a box in Java
Add a constructor to the class Box
class Box double width double heightdouble
depth void CalcVol() System.out.println( width
height depth ) Box() Width
4 Height 2 Depth 3
68Lets simulate a box in Java
Then our program?
69Lets simulate a box in Java
Add a constructor to the class Box
How do we create our own box with our own
dimension ?
70Lets simulate a box in Java
constructor to the class Box
class Box double width double heightdouble
depth void CalcVol() System.out.println( width
height depth) Box(double w, double h,
double d) Width w Height h
Depth d
71Lets simulate a box in Java
Then our program?
72Lets simulate a box in Java
Inheritance
Box
Box
There are two boxes having different colours
73Lets simulate a box in Java
Inheritance
Box
Box
Other than dimensions the colour is changed
74Lets simulate a box in Java
Template of the class Box
class Box double width double heightdouble
depth double CalcVol() return width height
depth Box(double w, double h, double d)
Width w Height h Depth d
75Lets simulate a box in Java
Template of the class ColorBox
class ColorBox extends Box int
colour ColorBox(double w, double h, double d,
int c) Super(w,h,d) Color c
76Lets simulate a box in Java
Inheritance
height width depth
w , h , d
colour
Box
c
77Lets simulate a box in Java
Template of the class ColorBox
class ColorBox extends Box int
colour ColorBox(double w, double h, double d,
int c) Super(w,h,d) Color c
78Lets simulate a box in Java
Then our program?
class BoxDemo public static void main(String
args) ColorBox myBox new ColorBox(2,4,5,255)
System.out.println(myBox.CalcVol())
79Polymorphism
80Lets simulate a box in Java
Template of the class Box
class Box double width double heightdouble
depth double CalcVol() return width height
depth Box() Box(double w, double h, double
d) Width w Height h Depth
d
81Some URLs
- Definition of a class
- http//java.sun.com/docs/books/tutorial/java/conce
pts/class.html - Creating objects
- http//java.sun.com/docs/books/tutorial/java/data/
objectcreation.html - Defining methods
- http//www.cs.brown.edu/courses/cs015/2002/Referen
ceGuides/JavaRefGuide/definemethod.html