lec 01 [oop basics]

14
BITS Pilani Pilani Campus Avinash Gautam Department of Computer Science and Information Systems BITS Pilani

Upload: garapati-avinash

Post on 02-Jul-2015

50 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lec 01 [oop basics]

BITS PilaniPilani Campus

BITS PilaniPilani Campus

Avinash GautamDepartment of Computer Science and Information Systems

BITS Pilani

Page 2: Lec 01 [oop basics]

BITS PilaniPilani Campus

Object-Oriented Programming (BITS C342)Avinash Gautam

[email protected]# 3254-G

Consultation HourBy appointment through mail

Page 3: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

Lecture-1 [OOP Basics]

3

OOP Concepts What is an object?

What is a Class?

What is Inheritance?

What is an Interface?

BITS C342, Object Oriented Programming

Page 4: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

OOP Concepts

BITS C342, Object Oriented Programming

4

Page 5: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

What is an object?

Look around you and identify some objectsReal world objects share two characteristics They all have state They all have behavior

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

Two Questions What possible states can this object be in? What possible behavior can this object perform? Write down your observation

Real world objects vary in complexity (Radio, Table Lamp)Objects may contain other objectsThese real world observations all translate into object oriented

programming

BITS C342, Object Oriented Programming

5

Page 6: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

6

Software objects are conceptually similar to real world objects: they too consist of state and related behavior

An object store its state in fields and exposes its behavior through methods

Methods operate on objects internal state and serve as the primary mechanism for object-to-object communication

Hiding internal state and requiring all interaction through an object’s method is known as data encapsulation - a fundamental principle of OOP

BITS C342, Object Oriented Programming

Page 7: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

7

Bicycle - By attributing state and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.

Benefits of building code into individual software objects

Modularity Information-hiding Code re-use Plug-ability and debugging ease

What is an object? (Contd.)

BITS C342, Object Oriented Programming

Page 8: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

What is a Class?

• A class is a blueprint or prototype from which objects are

created

• A class defines the general nature of a collection of

objects of the same type

• The process creating an object from a class is called

instantiation

• Every object is an instance of a particular class.

• There can be many instances of objects from the same

class possible with different values for data

BITS C342, Object Oriented Programming

8

Page 9: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

Example

9

class Rose

blueRose

redRose

class

objectsObject References

BITS C342, Object Oriented Programming

Page 10: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

What is Inheritance?

Different kinds of objects have a certain amount in

common with each other

For example mountain bikes, road bikes, and tandem

Bikes 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 etc

Object-oriented programming allows classes to inherit

commonly used state and behaviour from other classes

BITS C342, Object Oriented Programming

10

Page 11: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

What is Inheritance?

BITS C342, Object Oriented Programming

11

Page 12: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

What is a Interface?

• Objects define their interaction with the outside world through the methods that they expose

• Methods form the object's interface with the outside world

• The buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off

BITS C342, Object Oriented Programming

12

Page 13: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

Questions

• Real-world objects contain _____ and ________

• A software object's state is stored in its _____

• A software object's behaviour is exposed through

_______

• Hiding internal data from the outside world, and

accessing it only through publicly exposed methods

is known as data ___________

• A blueprint for a software object is called a ____

• Common behaviour can be defined in a _________

and inherited into a _______

BITS C342, Object Oriented Programming

13

state behaviour

fields

methods

encapsulation

class

super-class

sub-class

Page 14: Lec 01 [oop basics]

BITS Pilani, Pilani Campus

14

THANK YOU

BITS C342, Object Oriented Programming

1. The material presented in these slides is used only for academic purpose

2. Copyright © Oracle Sun Microsystems