0% found this document useful (0 votes)
3 views31 pages

Chapter Two Oop

Nice work

Uploaded by

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

Chapter Two Oop

Nice work

Uploaded by

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

Chapter 2:Introduction to object

oriented programming

1
Programming language paradigms
• It is the style of the programming language .
• It is only style no language.
• It is model or design the program.
• It can be structure and object oriented
programming paradigm

2
Structure programming language paradigm

• It is procedure oriented language .


• The main task is divide into subtask with
inputs & Functions to achieve the desired
result.
• A group of procedure can be termed as
program.
• The program developed into separete
smaller ,more manageable components

3
Con..
• The problem is that
 Understand the system to create the new system .
Change the existing software as the user requirements
change
Complex system (when the problem is large )

4
Object oriented programming paradigms

• Use object and class to develop a program.


• It is involves the creation of class by modeling
the real world .
• Use different feature or principles OOP such as
 data hiding (Encapsulations)
data abstractions
Inheritance
Polymorphism

5
Why use object oriented programming
paradigms
• Most problem arise when a software is
develop
 poor analysis and design :The computer
system we create doesn’t do the right things.
Poor maintainability : the system is hard to
understand & revise when the as inevitable
request for change arise.

6
Con..
• Due to this object oriented programming
helps :
 better abstractions (modeling information).
Better maintainability (behavior together with
more comprehensible and less fragile )
Better reuse able (class encapsulation)

7
What is class and Objects
• What is Object ?
• Objects are key to understanding object-
oriented technology.
• Real-world objects share two characteristics:
They all have state and behavior.
• Examples: your dog, your desk, your television
set, your bicycle.

8
Con….
• Software objects are conceptually similar to real-world objects:
• they too consist of state and related behavior.
• An object stores its state in fields (variables in some
programming languages) and exposes its behavior through
methods (functions in some programming languages).
• Methods operate on an object's internal state and serve as the
primary mechanism for object-to-object communication.
• Hiding internal state and requiring all interaction to be
performed through an object's methods is known as data
encapsulation — a fundamental principle of object-oriented
programming.

9
Object-oriented programming focuses on the
development of reusable software components,
called objects.

An object is a building block


which contains variables and methods.

Objects are key to understanding object oriented


technology.

10
All real world objects have two characteristics:

state and behavior

For example car have states

(current gear, number of gears, color, number of wheels)

and behaviors

(braking, accelerating, slowing down, changing gears)

11
Software objects are modeled after real-world objects and
they also have state and behavior.

A software object maintains its state in one or more variable.

A software object implements its behavior with methods.

variables
methods (state)
(behavior)

Software Object
12
What is class ?
• A class is the blueprint from which individual objects
are created.
• In the real world, you'll often find many individual
objects all of the same kind. There may be thousands
of other bicycles in existence, all of the same make
and model.
• Each bicycle was built from the same set of blueprints
and therefore contains the same components.
• In object-oriented terms, we say that your bicycle is
an instance of the class of objects known as bicycles.
13
Con…
• A programmer defines the characteristics of a type of object
through a class.
• The term class as used here means class of objects.
• A class is a template or pattern that defines the structure and
capabilities of an object.
• All objects that are members of the same class share many
common characteristics. A class can define both data and
behavior:
• data—objects can store primitive values (ints, booleans, etc.),
as well as references to other objects
• behavior–objects can specify operations that produce useful
results to the user of the object.
14
In the real world, we often have many objects of the same
kind. For example, my car is just one of many cars in the
world.

Using object-oriented terminology, we can say that my car


object is an instance of the class of objects known as cars.

Cars have state (4 gears, 1 engine, 4 wheels) and behavior


(change gears, accelerate) in common. However, each car’s
state is independent and can be different from each other.

15
When car manufacturers build cars they take
advantage of the fact that cars share common
characteristics, by building many cars from the
same blueprint.

It would be very inefficient to produce a new


blueprint for every individual car manufactured.

In object-oriented, it is also possible to have many


objects of the same kind that share characteristics.
Classes provide the benefits of creating a template
of objects.

16
we can take advantage of the fact that objects
of the same kind are similar and we can
create a blueprint for those objects.

A template or blueprint of objects is called a


class.

“A class is a template or blueprint that defines the


variables and the methods common to all objects
of a certain kind.”

17
Number
Change of gears
gear

Number of wheels
Brake

Car object

After you've created the car class, you can create


any number of car objects from the class. Each
object gets its own copy of all the variables defined
in the class.

18
Creating classes
class ClassName
{
variable 1;
variable 1;
method1(){}
method2(){}
} 19
class Car
{
int gears;
int wheels;

public void changeGear()


{}
}
20
Creating objects

You can create an object of class with the


following syntax: -

ClassName objVariable = new ClassName( );

So the car class object can be created as:

Car c = new Car( );

21
The object creation statement has three parts.

Car c = new Car( );

Declaration Instantiation Initialization

1. Declaration

2. Instantiation

3. Initialization
22
Declaration
You declare variables of int type as: -
int a;
You can say that a is a variable who can refer
to any type of int data.

Classes in java are also types so you can


declare class type variable as: -
Car c;
You can say that c is a variable who can refer
to any type of Car.

23
Declaring a variable do not create any object.
The code Car c; does not create a new car
object, it just declare a variable named c that
will be used to refer to a Car object. The
reference is still empty until assigned with a
new keyword.
The empty reference is called null reference
in java.

c
24
Instantiation
The new operator instantiates a class by
allocating a memory for a new object. The
new operator returns a reference to the
object it created and this reference is
assigned to the appropriate variable.

Car object
25
Initialization
The object is always initialize by calling a
constructor.

A constructor is a special method which has a


same name as class and used to initialize the
variables of that object.

In this case the Car class object is initialized


by calling the constructor of Car class Car( );

26
Import statements:
• In java if a fully qualified name, which includes the
package and the class name, is given then the
compiler can easily locate the source code or classes.
• Import statement is a way of giving the proper
location for the compiler to find that particular class.
• The syntax is:-
import package;

27
What are instance variables in Java?
• Instance variables are any variables, without "static" field modifier,
that are defined within the class body and outside any class's
methods body.
• Instance variables are in scope as long as their enclosing object is in
scope.
• An instance variable is a part of the object that contains it and
cannot live independently of it.
• All object instances have their own copies of instance variables. One
object instance can change values of its instance variables without
affecting all other instances.
• Instance variables can be used by all methods of a class unless the
methods are marked with "static" modifier. You access instance
variables directly from their containing object instances.
28
Example:
• public class Students {
• String name;
• int id; // instance variables
• String dept;
• public Students()
• {
• name=“Jone";
• id=19;
• dept="computer sciences"; // Constructor
• }
• public static void main(String[] args) {
• // TODO code application logic here
• Students s=new Students();
• System.out.println("Name"+s.name+"id"+s.id+"Department"+s.dept); }} // methods

29
Constructor
Constructor is a special method in java class.
It is used to initialize a new object variables.
A constructor has the same name as the class.
For example if you have Colour class the
constructor of colour class is also Colour( );

30
Constructors cannot return any value not even void.
If we are not providing any constructor for a class than
default (no parameter) constructor is automatically
provided by the runtime system.
When we will create our own parameterize
constructor. We have to create our own default
constructor as well because then default constructor is
not available.
The constructor can be private, public or protected.

31

You might also like