CSF213 L3
CSF213 L3
BITS Pilani
Hyderabad Campus
BITS Pilani
Hyderabad Campus
Todays Agenda
Object Oriented paradigm Object Oriented concepts Basic terminology of Object Oriented principles
Whats class?
Objects are grouped in classes. Class is a collection of objects having similar behavior and properties. A single object is simply an instance of class. Objects can not be instantiated (or created) without defining a class. Classes are defined whereas objects are created.
void print_details();
}
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Objects
class
Car
maruthi800 Attributes: name Maruthi 800 color red Price 2.5 lacs Methods: void print_details( )
wagonR Attributes: name WagonR color silky silver Price 4.5 lacs Methods: void print_details( )
CS/IS F213 First Semester 2012-13
objects
Whats Object?
Term Object Means Combination of Data (Attributes) and Logic (Behavior, Functions, Operations) of some real world entity. Every object has two parts :
Data Part [ Instance Fields in Java, Attributes or properties] Operations Part [ methods in java, Behavior] Examples : 1. Account Attributes : Account Holder, Account type [saving , current] ,Balance Operations : deposit, withdraw, 2. BOX: Attributes : length, width, height, color Operations : compute area, compute volume
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Object State
Properties/Attribute Values at a particular moment represents the state of an object. Object may or may not change state in response to an outside stimuli.
States of Three Different INSTANCES of CAR CLASS
MARUTI800
MARUTIESTEEM
MARUTIZEN
car mar1 = new car(maruthi800, red, 200759); car mar2 = new car(maruthiesteem, white, 500789);
11
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Abstraction in OOPs
Abstraction in Object Oriented Programming helps to hide the irrelevant details of an object. Abstraction is separating the functions and properties that logically can be separated to a separate entity which the main type depends on.
public class Car {
Engine engine = new Engine(); Wheel wheel = new Wheel(); int price; String name; String color;
public class Engine { int engineCapacity; int engineHorsePower; public class Wheel { String wheelName; int wheelPrice; void rotate(){ //Wheels method }
Pillars of OOP
Encapsulation
Inheritance Polymorphism
Encapsulation
Encapsulation means wrapping up of data and methods (operations , code) together Access to code and data is tightly controlled. Though, we can define what can be and what can not be accessible outside. [ public , private , protected ].
Methods Methods
Data
Methods Methods
Encapsulation Example
class BOX
BOX Class
area()
{
private double length; private double width; private double height; public double area() { }
length width height
volume()
Inheritance
Inheritance
Hierarchical Inheritance
X X
BB
MultiLevel Inheritance A A
Multiple Inheritance B
B
C
CS/IS F213 First Semester 2012-13
Examples
Summary
Class is a collection of objects. Objects cannot be instantiated. Abstraction is concealing the details of implementation from user. Encapsulation is creating a wrapper for the data or methods of your class. Inheritance allows us to inherit basic properties or methods of a class.