Uploaded by M Sharjeel

LECTURE 10

advertisement
COMPUTER PROGRAMMING
60011101-4
LECTURE 10
Object Oriented Programming
Lecture by: Dr Mehnaz Khan
Object-oriented programming (OOP) is a way
to organize a program as a set of interacting
objects.
In OOP, a program is written as a collection of
classes and the objects created from those classes.
Classes
Class is the basic unit of programming.
A Java program is a collection of classes.
A class is a template/ blueprint from which objects of same type are created.
It is a logical entity ( has no memory space).
■ A class is a collection of data fields (variables) and methods (procedure or
function) that operate on that data.
•
class class-name
Example: class circle {
double radius;
{
data fields;
methods;
double getarea()
{
return radius * radius * 3.14
}
}
3
Objects
Objects are the instances/variables of type class.
An object is a real world entity e.g., a person, table, book, pen etc
Each object has a class which creates it and defines its data and methods.
Objects are created using new keyword.
circle a; //declare an object
a = new circle(); // instantiate an object
or, we can write both statements together as,
circle a = new circle();
this creates an object ‘a’ of type circle
We can create many objects of the same class as,
circle b = new circle();
circle c = new circle(); and so on.
a, b and c are the objects of the same class circle.
4
5
6
• An
object can use the data values and methods defined in the
class.
circle a = new circle();
To use data values:
Objectname.variablename
a.radius = 2.0;
To use methods:
Objectname.methodname
a.getarea();
class circle {
double radius;
double getarea(){
return radius * radius * 3.14
}
When we declare an object
circle a;
a
Points to nothing (Null Reference)
null
After instantiating the object
a = new circle();
a
a
b
circle a = new circle();
circle b = new circle();
Now, if we write a = b;
a
b
This object does not have
a reference and cannot be
used in future. So, it is deleted
from memory.
Constructors
•
Constructor is a special type of method written inside a
class and is used to initialize the objects of that class.
•
It doesn’t have a return type.
•
Constructor has same name as the class.
•
Constructors may or may not have arguments/parameters.
No-arg constructors have no arguments/parameters.
Parameterized constructors have arguments/
parameters.
class circle {
double radius;
circle()
Constructors have
{
no-arg
radius = 3;
same name as class
constructor
}
circle( double r)
{
parameterized
radius = r;
constructor
}
double getarea()
{
return radius * radius * 3.14
}
Constructors are called when the objects of the class are created.
circle a = new circle( ); // this will call no-arg constructor
circle b = new circle(2); // this will call parameterized constructor
Program to create a class and its objects
Access Modifiers in Java
•
Access modifiers in Java are used to specify the
scope/accessibility (‫ )إمكانية الوصول‬of a data value,
method, constructor or a class.
•
Access modifiers specify who can access the class
and its data and methods.
•
4 types of access modifiers: private, public,
default, and protected.
1. private - can be used only inside
class. (Visible to class)
2. public - can be used anywhere.
(Visible to world)
3. default - Visible in a package
4. protected - Visible in a package and
all subclasses.
‫أقل تقيي ًدا‬
‫أكثر تقيي ًدا‬
1.private access modifier
•
The private access modifier is the most restrictive
modifier.
•
Methods, data values and constructors that are
declared as private cannot be accessed outside the
class. They are visible only inside the class.
•
Classes cannot be declared as private, only the data
and methods and constructors of a class can be
private.
Example 1: private data accessed in same class
variable ‘data’ is declared as private
in class Test
variable data is accessed in the same class
Example 2: private data cannot be accessed by other class
class A in which variable ‘data’
and method ‘msg’
are declared as private
class Test in which variable
‘data’ and method ‘msg’ are
accessed
This program will give an error because ‘data’
and ‘msg’ are private in class A.
So, they cannot be accessed in class Test.
They can be accessed only inside class A.
How to access private data of a class from other class
If we want to access the private data values of a class in
another class then we have to use ‘get’ and ‘set’ methods
and declare them as public.
•
set(parameter) - it is used to set the value of the private variable
get - it is used to access the value of the private variable
How to access private data of a class from other class
class A in which variable ‘data’
is declared as private
public set method
public get method
class Test in which variable
‘data’ is accessed using ‘set’
and ‘get’ methods
Both ‘set’ and ‘get’
methods should be public
2.public access modifier
•
The public access modifier is the least restrictive
modifier.
•
Classes, Methods, data values and constructors that
are declared as public can be accessed every where
outside the class.
class A in which variable ‘data’
is declared as public
class Test in which variable
‘data’ is accessed
‘data’ is declared as public so it can be accessed from any
class
3.protected access modifier
•
The protected access modifier allows access within
a package.
•
Methods, data values and constructors that are
declared as protected can be accessed from any
class in a package.
•
Classes cannot be declared as protected. Only
methods, data values and constructors can be
declared as protected.
class A in which variable ‘data’
is declared as protected
class Test in which variable
‘data’ is accessed
‘data’ is declared as protected so it can be accessed from any
class within the same package
4.default access modifier
•
The default access modifier means we do not need
to declare an access modifier. If there is no access
modifier it is taken as default.
•
Methods, data values and constructors that are
declared without any access modifier can be
accessed from any class in a package.
this keyword in Java
•
The this keyword refers to the current object in a
method or constructor.
•
this keyword is used to avoid confusion when the
parameter name and data values name is same.
public class MyClass {
int x;
data value name and parameter name is same ‘x’
public MyClass(int x) {
this.x = x;
If you do not write keyword ‘this’
the output would be "0" instead of "5".
}
public static void main(String[] args) {
MyClass obj = new MyClass(5);
System.out.println("Value of x = " + obj.x);
}
}
Program for using this keyword
If you do not write keyword ‘this’
the output would be "0" instead of "5".
Download