• No results found

Object Oriented Concepts

N/A
N/A
Protected

Academic year: 2022

Share "Object Oriented Concepts"

Copied!
41
0
0

Loading.... (view fulltext now)

Full text

(1)

Object Oriented Concepts

Unit-1 Object Class Encapsulation Abstraction Inheritance Method Overriding Polymorphism

(2)

It is an approach to building computer programs that are reusable, reliable, and understandable.

It looks at a program from a different angle, focusing on the task for which you are using the computer

rather than the way a computer handles tasks.

OOP

(3)

In this computer program is conceptualized as a set of objects that work together to accomplish the task.

Each object is a separate part of the program,

interacting with the other parts in specific, highly controlled ways.

OOP

(4)

Object Class

Encapsulation Data hiding Abstraction Inheritance

Method overloading Method Overriding Polymorphism

OOP

(5)

An object is a software bundle of related state and

behavior. Software objects are often used to model the real-world objects that you find in everyday life.

What is an Object?

(6)

Objects are key to understanding object-oriented technology.

Look around right now and you'll find many examples of real-world objects: your dog, your desk, your

television set, your bicycle.

Objects

(7)

Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented

programming.

Real-world objects share two characteristics:

They all have state and behavior.

◦ Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).

◦ Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).

Objects

(8)

Object

(9)

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.

Object

(10)

Objects provides a number of benefits, including:

◦ Modularity: The source code for an object can be written and maintained independently of the source code for other objects.

Once created, an object can be easily passed around inside the system.

Object

(11)

Information-hiding:

◦ By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.

Object

(12)

Code re-use:

◦ If an object already exists (perhaps written by another software developer), you can use that object in your program. This

allows specialists to implement/test/debug complex,

task-specific objects, which you can then trust to run in your own code.

Object

(13)

Pluggability and debugging ease:

◦ If a particular object turns out to be problematic, you can simply remove it from your application and plug in a

different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

Object

(14)

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. A class is the blueprint from which individual objects are created.

Class

(15)

The following Bicycle class is one possible implementation of a bicycle:

class Bicycle {

Int int speed = 0; int gear = 1;

void changeGear(int newValue) {

gear = newValue;

}

void speedUp(int increment) {

speed = speed + increment;

}

void applyBrakes(int decrement) {

speed = speed - decrement;

}

void printStates() {

System.out.println(“ speed:"+speed+" gear:"+gear);

} }

(16)

The fields speed, and gear represent the object's state, and the methods (changeGear, speedUp etc.) define its interaction with the outside world.

You may have noticed that the Bicycle class does not contain a main method.

That's because it's not a complete application; it's just the blueprint for bicycles that might be used in an application.

The responsibility of creating and using new Bicycle objects belongs to some other class in your application.

Class

(17)

class BicycleDemo {

public static void main(String[] args) {

// Create two different Bicycle objects Bicycle bike1 = new Bicycle();

Bicycle bike2 = new Bicycle();

// Invoke methods on those objects bike1.changeCadence(50);

bike1.speedUp(10);

bike1.changeGear(2);

bike1.printStates();

bike2.changeCadence(50);

bike2.speedUp(10);

bike2.changeGear(2);

bike2.changeCadence(40);

bike2.speedUp(10);

bike2.changeGear(3);

bike2.printStates();

} }

The output of this test prints

the ending pedal cadence, speed, and gear for the two bicycles:

cadence:50 speed:10 gear:2 cadence:40 speed:20 gear:3

(18)

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.

Encapsulation

(19)

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as single unit.

Encapsulation

(20)

In encapsulation the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class, therefore it is also known as data hiding.

Encapsulation

(21)

To achieve encapsulation in Java

◦ Declare the variables of a class as private.

◦ Provide public setter and getter methods to modify and view the variables values.

Encapsulation

(22)

The fields of a class can be made read-only or write-only.

A class can have total control over what is stored in its fields.

The users of a class do not know how the class

stores its data. A class can change the data type of a field and users of the class do not need to

change any of their code.

Benefits of Encapsulation:

(23)

Abstraction is the quality of dealing with ideas rather than events.

Abstraction

(24)

for example

◦ when you consider the case of e-mail, complex details such as what happens soon you send an e-mail, the

protocol your email server uses are hidden from the user, therefore to send an e-mail you just need to type the

content, mention the address of the receiver and click send.

Abstraction

(25)

Object oriented programming Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user.

Abstraction

(26)

In other words user will have the information on what the object does instead of how it does it.

Abstraction

(27)

In Java Abstraction is achieved using Abstract classes, and Interfaces.

Abstraction

(28)

Different kinds of objects often have a certain amount in common with each other.

Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear).

Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.

What is an Inheritance

(29)

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.

In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike.

In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the

potential for an unlimited number of subclasses:

What is an Inheritance

(30)
(31)

The syntax for creating a subclass is simple.

At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from:

class MountainBike extends Bicycle {

// new fields and methods defining a mountain bike would go here

}

Inheritance

(32)

This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique.

This makes code for your subclasses easy to read.

However, you must take care to properly document the state and behavior that each superclass defines, since that code will not appear in the source file of each subclass.

Inheritance

(33)

If subclass (child class) has the same method as

declared in the parent class, it is known as method overriding in java.

Method Overriding

(34)

In other words, If subclass provides the specific

implementation of the method that has been provided by one of its parent class, it is known as method

overriding.

Method Overriding

(35)

Usage of Java Method Overriding

◦ Method overriding is used to provide specific implementation of a method that is already provided by its super class.

◦ Method overriding is used for runtime polymorphism.

Method Overriding

(36)

Rules for Java Method Overriding

◦ method must have same name as in the parent class

◦ method must have same parameter as in the parent class.

◦ must be IS-A relationship (inheritance).

Method Overriding

(37)

Polymorphism is the ability of an object to take on many forms.

The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Polymorphism

(38)

Any Java object that can pass more than one IS-A test is considered to be polymorphic.

In Java, all Java objects are polymorphic since any

object will pass the IS-A test for their own type and for the class Object.

Polymorphism

(39)

It is important to know that the only possible way to access an object is through a reference variable.

A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed.

Polymorphism

(40)

The reference variable can be reassigned to other objects provided that it is not declared final.

The type of the reference variable would determine the methods that it can invoke on the object.

Polymorphism

(41)

A reference variable can refer to any object of its declared type or any subtype of its declared type.

A reference variable can be declared as a class or interface type.

Polymorphism

References

Related documents

BEGINNING at an existing magnetic nail in the southeast intersection of Third Street and Town Run Lane said nail being the northwest corner of the Forsyth County Property as

WHEREAS, these partners assist the Los Angeles City Council and its corporate sponsor, 1600 VINE, in efforts to restore California as the world center of film and

(a) No individual or organization shall operate, or cause to be operated, any mobile food vending unit within the corporate limits of the City of Painesville without a current

rights, the definition of an issuer under federal law has been explicitly limited to owners of fractional undivided interests in oil, gas, or other mineral rights who

Foreign Aff airs, Trade and Development Canada Synthesis Report: Summative Evaluation of Canada’s Afghanistan Development Program 2004–2013 2015 2004–2013 CAD $1546 million

Downregulation of long noncoding RNA LINC01419 inhibits cell migration, invasion, and tumor growth and promotes autophagy via inactivation of the PI3K/Akt1/mTOR pathway in gastric

It has been reported that taurine play an important role in production and reproduction behavior of the species (Gaylord et al., 2014). Fish hydrolysate contains well

The primary tasks of the PPE Student Ambassadors are to (i) promote the PPE Program at Virginia Tech, (ii) work closely with the PPE Program Director as well as with the