0% found this document useful (0 votes)
33 views26 pages

OOP Chapter 3

1. The document discusses key concepts in object-oriented programming like objects, classes, encapsulation, inheritance, and polymorphism. 2. It defines an object as having state (data) and behavior (methods) and provides examples like a dog having name, color as state and bark, run as behaviors. 3. A class is a blueprint that defines the properties and methods for an object. Objects are instances of classes and share the attributes defined in their class.

Uploaded by

Abel Gulilat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views26 pages

OOP Chapter 3

1. The document discusses key concepts in object-oriented programming like objects, classes, encapsulation, inheritance, and polymorphism. 2. It defines an object as having state (data) and behavior (methods) and provides examples like a dog having name, color as state and bark, run as behaviors. 3. A class is a blueprint that defines the properties and methods for an object. Objects are instances of classes and share the attributes defined in their class.

Uploaded by

Abel Gulilat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter three:

Java Objects and Classes

1
Introduction

• Java is an object-oriented programming language


• In object-oriented programming (OOP),
programs are organized into objects
• The properties of objects are determined by their
class
• Objects act on each other by passing messages

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

object state behavior


dog breed, isHungry eat, bark
grade book grades mean, median
light on/off switch
State = Properties Behavior = Method 5
Java Class
• A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
Class= fields + methods

• A class is the blueprint from which individual objects


are created.

• Thus, a class is a template for an object, and an


object is an instance of a class.

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

• Constructor is a special type of method that is used to


initialize the object.

• Rules for creating constructor


• Constructor name must be same as its class name
• Constructor Name=Class Name
• Constructor is invoked at the time of object creation.
• Constructor must have no explicit return type

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

• Define a member that is common to all object not to specific


object.
• Static variables – called as class variable
• Static methods – called as class methods
 static variable:
 declare any variable as static, it is known static variable.
 static method:
 static keyword with any method, it is known as static
method.
 A static method belongs to the class rather than object of a
class.
 invoked without the need for creating an instance of a class.
 access static data member and can change the value of it
19
• Final Keyword In Java
• The final keyword in java is used to restrict the user.
• Generally all the variables / methods by default overridden in
subclasses
• The final keyword can be used in many context. Final can be:
• Variable - make any variable as final, you cannot change the
value of final variable(It will be constant).
• Method - make any method as final, you cannot override it.
• Class - make any method as final, you cannot override it.

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

• When writing an object-oriented program, we


define classes, which describe categories of
objects, and the states and behaviors that they
have in common.

• We then create objects which belong to classes,


and share the common features of their class.

• Objects interact with each other by passing


messages.

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

You might also like