OOP-Class As A Basic Unit of Computation
OOP-Class As A Basic Unit of Computation
Computation + Constructor
So Many Different Things -
Objects
OBJECTS IN A Object Eg:
FOOTBALL GAME
Books
Property shape
State(rect)
Colour = state blue
Pages= state=300
Behaviour/method
Cutting
Recolouring
pageChange
WHAT IS AN OBJECT?
Object is an identifiable entity/thing having
specific properties and methods. Eg. Book
Laptop class.. HarryPotter object
Properties/
describe the laptop Attributes/Features/Properties—state (real world) –
Width instance variable/data member /member variables
Length (software term)
Colour
Weight
Brand
Behaviour-Action (real world)
Memory Member methods /functions /non-static
Chair
methods
Cost
----------- Properties/
Behaviour- describe the chair
Function-Methods- Width , colour, handles,
Do?? Length, material, price,
Working -----------
Networking- Behaviour-Function-Methods-
PlayingGame Do??
Repairing Study, fence, working, sleep
Booting
CLASS = DESIGN FOR SIMILAR
OBJECTS
Properties
Colour
Fuel
Brand
Make
Chair mychair = new
Chair();
=========
Scanner sc = new
Scanner(System.in);
sc.next();
sc.nextInt();
NEW KEYWORD =
OPERATOR THAT
INSTANTIATES/CREATE
S A NEW OBJECT
USING DYNAMIC
MEMORY
ALLOCATION..
What does this statement mean
Student s = new Student();
1. creating a new object s of the class Student using the new operator
2. invokingdefault constructor…
Sports car
object/instance/member(object)
Properties: characteristics, attributes OF THE OBJ,
HOW IS THE OBJECT
Methods : behaviour : what is the car
STATE OF THE OBJECT are the values of the doing ?
properties of the object Action – Objects interact with each
other by message passing ( invoking
each other’s methods)
class SportsCar
{
The behaviour changes the state of
String colour; the object.
double speed;
String make;
String date;
Software Terms?
void accept(){input colour, speed } ,
void driving(… .accelaration
State data members, instance
variable, member variable(non-
static)
Behaviour member methods
Car
Traffic
Driver
Police
Me c
er
eng
han
s
Pas
ic
Action – Objects interact with each other by message passing ( invoking each other’s methods)
Procedure & OOP programs
• Procedural programming : focuses on the “how” or the procedure to
perform a task. /function!
• Eg: calculate discount given on the mobile..
=============================================
Oop: define a class mobile phone with following properties and methods : DATA
[for whom? Who? – properties, methods]
Properties : brand, cost, size
Methods : accept(),
calcDiscount cost <10000 disc=0, cost <30000 2%
print()
Encapsulation – Wrapping up of data members
(properties) and methods(behaviour) into a
single unit called a class == security
class Book //Class
{
private String name; //DATA MEMBERS
public String author;
double cost; //package level
protected String genre;
Book() { } //METHODS
void accept(){ }
void rating(){}
void print() { }
Laptops User(Rugmini)
Used()
Repair()
Charged()
}
Access Specifier
class Book
{ public String title;
private String alias;
int cost;
protected String series;
}
Private – to hid
Public – to access
Student
Acads : marks, rollno,
dateofexam
Extra-curricular: events, prizes,
participation
Academic:Name, rollno
Exam seat number.marks
Data Abstraction – Act of representing the
essential features without including the
background details.
• Example : when we are driving the sportscar, we are only using gear and
steering while, not the background /inner details of the engine working.
• ++data hiding : hiding the background details and showing only the
relevant details.
• Why is a Class an abstraction?
Class is an abstraction because it is a named representation of the
object.
SportsCar () { } ==mechanic //engine details
• ==driver() //comfort, wheel/driving
==fanLover //history, colour
Example of Class is an abstraction
class Student
{ String name;
int age;
int marks,
String name;
}
Academics: marks,
Extra-curricular:events, prizes
Personal : name, address, parents
REUSABILI
TY
class Vehicle //super – parent /base
{
String name;
}
Luxury Car
}
Overloading is possible due to function signature
Function Signature = number and type of parameters
}
MODULARITY : Act of
dividing/partitioning/chunking the program into
individual methods/components.
• class SportsCar //Class
• {
• private String name; //DATA MEMBERS
• public String make;
• double cost;
• SportsCar() { } //METHODS
• void accept(){
• } //different module
• void avgSpeed(){} //different modules
• void print() { } //different module
• }
Primitive/ Fundamental/Primary Derived /Reference /Composite
1. Primitive datatypes are predefined by Java. 1. Reference/Composite are user-defined
[8 primitive ] 1 2 4 8 Array and Class [String, Math, System]
byte -1 String, Math, System are predefined in java API but
boolean -1 they are classes, composite ,reference
short -2 int arr[] = new int[10];
char -2
int -4 (whole) class Book //user-defined class
float -4 (real.. floating) {
long – 8 (whole) String author; double cost; String title; float f
double - 8 (floating ..) void accept() {}
void calc() {}
void print() {}
}
2. Size is fixed [int: 4 bytes, byte:1 byte] 2. Size is dependent on the composite data items
Why is it composite ?
Bcoz it is composed/made of one or more primitive or
reference datatypes