0% found this document useful (0 votes)
12 views26 pages

OOP-Class As A Basic Unit of Computation

The document explains the concepts of classes and objects in object-oriented programming (OOP), defining a class as a blueprint for creating objects with similar properties and methods. It covers key OOP principles such as encapsulation, inheritance, polymorphism, and abstraction, illustrating how objects interact through message passing. Additionally, it discusses data types, the significance of the 'new' keyword for object instantiation, and provides a question bank for further understanding of the topics covered.

Uploaded by

Armin Shroff
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)
12 views26 pages

OOP-Class As A Basic Unit of Computation

The document explains the concepts of classes and objects in object-oriented programming (OOP), defining a class as a blueprint for creating objects with similar properties and methods. It covers key OOP principles such as encapsulation, inheritance, polymorphism, and abstraction, illustrating how objects interact through message passing. Additionally, it discusses data types, the significance of the 'new' keyword for object instantiation, and provides a question bank for further understanding of the topics covered.

Uploaded by

Armin Shroff
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/ 26

Classes As A Basic Unit Of

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

Class =blueprint, Object


Factory
&
Object = Instance of a
Class
A CLASS IS A COLLECTION OF OBJECTS WITH SIMILAR PROPERTIES AND
METHODS. It is a blueprint, template or an object factory.

Object is an identifiable entity with specific characteristics and method


It is an instance of a class.

Properties
Colour
Fuel
Brand
Make
Chair mychair = new
Chair();

Mychair==object of class 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;
}

In the class In other classes in Outside the Child class


the package package
Public (least— yes yes yes yes
Least secure)
Package Yes Yes No No
(default..friendly..)

Protected Yes Yes No Yes


(inheritance.. Child
class)
Private (highest) Yes No No No
Math.ceil(-4.5) not bothered about background
details .ie. How is the actual calculation done 2nd point
Class As An Abstraction
Sop(“hello”);
Class represents abstraction of
properties and methods

Private – to hid
Public – to access

Student
Acads : marks, rollno,
dateofexam
Extra-curricular: events, prizes,
participation

Object is an instance : object is


a type of abstraction .

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;
}

class Car extends Vehicle


–child/derived /sub
{
}
class bicycle extends VehicLe
{
}

Deriving properties and methods


from another class (inheritance)
Inheritance is an OO principle to derive(use/share)
the properties and methods from another class
(keyword: extends)
ADVANTAGE :REUSABILITY
Parent class
& keyword:extends vehicles Base class
Super class
automatic manual
Parent class car
Base class
Super class bicycle Parent class
Base class
sportsCar taxi Super class

Luxury Car

Child class Child class


Derived class Derived class
Sub class Sub class
Polymorphism – MANY FORMS….Property of an object
to behave differently in different situations and
have multiple operations. Thus, function
overloading and constructor overloading.
==1 FUNCTION MULTIPLE OPERATIONS
class AreaPolymorphism
{
public static void area(int r)
{//area of circle
}
public static void area(double s)
{
//area of square
}
public static void area(int b, int h)//rectangle
{

}
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

Why is it user defined?


Bcoz it is not predefined
Call by value Call by reference
• Software?
• Class
• Properties/Attributes/Characteristics that represent the state of the
object in software terms are known as instance variables/data
members

• <object == instance == member>

• Behaviour is represented as member methods.


Question Bank on Chapter 6, Chapter 1-unit1+2
1. What is an object?
2. What is a class? Why is it considered a composite datatype ? Reference datatype ? User defined datatype ?
3. How is a class related to an object?
4. What is state and behaviour? Behaviour changes the state of the object
5. How do objects interact with each other? //by calling each other’s methods and is called MESSAGE PASSING
6. How is a class represented in software terms?
7. Why is a class an object factory?
8. Why is a class considered an abstraction?
9. State the OOP principles.
10. Define encapsulation + eg
11. Define inheritance + eg
12. Define polymorphism +eg
13. Define abstraction, data hiding
14. Define modularity.
15. Class – User defined/ Reference datatype (new) / Composite
16. Diff between primitive and user-defined datatypes
17. The new keyword is an operator? .. Dynamic memory allocation .. For a new array and a new object creation in memory /declaration
18. Use of the this keyword? What is the implicit and explicit use of this keyword.

You might also like