OOP Chapter 3
OOP Chapter 3
1
Introduction
2
OOP Concepts
• Object • Encapsulation
• Class • Abstraction
• Polymorphism • Method
• Inheritance • Message
3
Object
• A programming entity that contains state (data) and
behavior (methods).
– State: A set of values (internal data) stored in an
object.
– Behavior: A set of actions an object can perform.
• Example 1: Dogs
– States: name, color, breed
– Behaviors: bark, run, and wag tail
• Example 2: Cars
– States: color, model, speed, direction
– Behaviors: accelerate, change gears
4
What is an Object?
• An Object has two primary components:
– state – properties of the object
– behavior – operations the object can perform
• Examples
6
Classes and Objects
• A Java program consists of one or more classes.
• A class is an abstract description of objects.
• Here is an example class:
class Dog {
...
description of a dog goes here
...
}
• Here are some objects of that class:
7
Class and object image
8
Creating objects of a class
• Object – block of memory that contains space to store
all instance variables.
• Objects are created dynamically using the new keyword.
It’s called instantiating an object.
• Example:
class_name object_name;
object_name=new class_name();
or
class_name object_name=new class_name();
Object’s created:
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
9
Constructor in Java
10
Types of Constructors
• Default constructor (No-argument constructor)
• A constructor that have no parameter
• Default values to the object like 0, null etc.
depending on the type.
<class_name>()
{
}
• Parameterized constructor
• A constructor that have parameters
• Used to provide different values to the distinct
objects.
<class_name>(parameter list)
{
} 11
Example
class Student {
int id;
String name;
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]) {
Student s1=new Student();
Student s2=new Student();
s1.display();
s2.display();
}
}
class_name object_name=new class_name();
12
Example:
class Student }
{ public static void main(St
int id; ring args[])
String name; {
Student(int i,String n){ Student s1 = new Studen
id = i; t(111,“Abay");
name = n;
} Student s2 = new Studen
t(222,“Gebissa");
void display(){
s1.display();
System.out.println(id+" "+na s2.display();
me); }
}
13
Constructor Overloading
• Constructor overloading is a technique in Java in
which a class can have any number of constructors
that differ in parameter lists.
• The compiler differentiates these constructors by
taking into account the number of parameters in the
list and their type.
Method Overloading
• class have multiple methods by the same name but
different parameters, it is known as Method
Overloading. 14
Constructor Overloading
class Student void display()
{ {
int id; System.out.println(id+" "+name+" "+age);
String name; }
int age; public static void main(String args[])
Student(int i,String n) {
{ Student s1 = new Student(111,“yemane");
id = i; Student s2 = new
name = n; Student(222,"Aryan",25);
} s1.display();
Student(int i,String n,int a) s2.display();
{ }
id = i; }
name = n;
age=a;
}
15
class Calculation
{
void sum(int a,int b)
Method Overloading {
System.out.println(a+b);
}
void sum(int a,int b,int c)
{
System.out.println(a+b+c);
}
public static void main(String args[])
{
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
16
}}
Difference between constructor and method ?
Constructor Method
• Constructor is used to initialize • Method is used to expose
the state of an object. behaviour of an object.
• Constructor must not have • Method must have return
return type. type.
• Constructor is invoked • Method is invoked
implicitly. explicitly.
• The java compiler provides a
• Method is not provided by
default constructor if you don't
compiler in any case.
have any constructor.
• Constructor name must be same • Method name may or may
as the class name. not be same as class name.
17
STATIC KEYWORD
class class_name
{
public static void main(String args[])
{
data / field / instance variables;
methods / instance methods;
}
}
Object creation – class_name object_name=new class_name();
object_name.variables;
object_name.methods();
Every time object created a new copy of each of variable and
methods are created.
Then accessed with (.) operator.
18
STATIC VARIABLE / METHOD
20
Finalizer methods:
• Constructor – used to initialize an object when it’s declared –
initialization
• Finalize() This method is called just before an object is
garbage collected. finalize() method perform clean-up
activities and minimize memory leaks.
• Automatic garbage collection run time, It’s frees up the
memory resources used by objects.
Syntax:
finalize()
21
Abstract keyword
• Opposite to final – make use of the same method name in
subclass
Abstract class in Java
• class that is declared with abstract keyword, is known as abstract
class
• is a restricted class that cannot be used to create objects (to access
it, it must be inherited from another class).
abstract class <class_name>
{
}
abstract method
• method that is declared as abstract - abstract method
• does not have implementation, it’s in derived class
Syntax:
abstract return_type <method_name>();//no braces{} 22
Summary on Objects and Classes
23
Con’t
• Object is the physical as well as logical entity
whereas class is the logical entity only.
• An entity that has state and behavior is known as
an object
• Object is an instance of a class. Class is a template
or blueprint from which objects are created. So
object is the instance(result) of a class.
24
Con’t
• Object Definitions:
– Object is a real world entity.
– Object is a run time entity.
– Object is an entity which has state and behavior.
– Object is an instance of a class.
• Class in java
– A class is a group of objects which have common properties.
– It is a template or blueprint from which objects are created.
– It is a logical entity. It can't be physical.
A class in Java can contain:
– fields
– methods
– constructors
– blocks
– nested class and interface
25
Thank You
?
26