Lecture 1C
CS F213
Object Oriented Programming
08/01/25
BITS, Pilani Goa campus
Prof. Anita Agrawal
2
Encapsulation
A mechanism of wrapping the data (variables) and code
acting on the data (methods) together as a single unit.
1/10/2025 6:21 PM Anita Agrawal CS F213
Encapsulation and 3
classes
▪ Class contains: Data
Member/Instance
variables
Code Member methods
o Class encapsulates complexity
1/10/2025 6:21 PM Anita Agrawal CS F213
4
1/10/2025 6:21 PM Anita Agrawal CS F213
5
Inheritance
o A mechanism in which one class acquires the property of
another class.
o Example, a child inherits the traits of his/her
parents.
o With inheritance, the fields and methods of the existing
class can be reused.
o An inherited class is called a subclass of its parent class
or super class.
o It can be of different types- Single, Multiple, Multilevel,
Hierarchical and Hybrid
1/10/2025 6:21 PM Anita Agrawal CS F213
6
Real World example of
inheritance
1/10/2025 6:21 PM Anita Agrawal CS F213
Another example of Inheritance:
One class acquires properties of another class
Supports hierarchical classification
7 1/10/2025 6:21 PM Anita Agrawal CS F213
8
Real World Example in a
banking system
1/10/2025 6:21 PM Anita Agrawal CS F213
9
Polymorphism
▪ It is a OOP concept where one name can have many
forms.
▪ In a real world example, a person at a same time can
have different characteristic.
▪ Example- Calculate the Area-
• For Circle: Radius r
• For Rectangle: Length L and Width W
• For Sector: Radius r, Angle O.
1/10/2025 6:21 PM Anita Agrawal CS F213
Introduction
to Java
Topics:
▪ Anatomy of a Java Program
▪ History of Java
▪ Salient Features of Java
▪ Stages of Execution of Java Program
▪ JVM vs. JRE vs. JDK
10 1/10/2025 6:21 PM Anita Agrawal CS F213
11
Java Program
▪ A Java program is defined as a collection of objects
that communicate with each other to perform specific
tasks.
1/10/2025 6:21 PM Anita Agrawal CS F213
12
Anatomy of a Java Program
A. Reserved words / Keywords
B. Classes
C. Package
D. Modifiers
E. Statements
F. Blocks
G. Methods
H. The main method
I. Comments
1/10/2025 6:21 PM Anita Agrawal CS F213
13
My first program
1/10/2025 6:21 PM Anita Agrawal CS F213
14
Breaking down the program
▪ “class" keyword is used to declare a new class.
▪ “Myfirstjavaprogram" is the identifier
▪ public static void main
▪ public:
» It is an example of an access specifier.
» Allows programmer to control the visibility of class
members
» Members may be accessed from outside the class in which
they are declared.
1/10/2025 6:21 PM Anita Agrawal CS F213
15
Breaking down the program
(Contd.)
main() be declared as public since it will be called
by outside it’s class
1/10/2025 6:21 PM Anita Agrawal CS F213
Breaking down the program (Contd.)
▪ static:
» Allows main() to be called without creating an object of the
class.
» It is necessary since main() is called by JVM before any
objects are made.
▪ void:
» Defines the return type, in this case it is void.
16 1/10/2025 6:21 PM Anita Agrawal CS F213
main
» The system locates and runs the main method for a class
when you run a program.
» Other methods get executed when called by the main
method.
17 1/10/2025 6:21 PM Anita Agrawal CS F213
18
▪ String args[ ]
– Declares a variable (object) named args
– It is an array of String class instances
– args[ ] receives any command line arguments during
program execution
1/10/2025 6:21 PM Anita Agrawal CS F213
19
System.out.println
▪ println: It is a method
▪ It is used by invoking a statement with a string argument.
▪ The string argument is enclosed within parentheses. In this
case, the argument is "This is my first java program!"
▪ You can call the same println method with a different
argument to print a different message.
1/10/2025 6:21 PM Anita Agrawal CS F213
20
Blocks
▪ A pair of braces in a program form a block that groups
components of a program.
1/10/2025 6:21 PM Anita Agrawal CS F213
21
Comments
//my first java program
class myfirstjavaprogram
{
public static void main(String args[])
{
System.out.println("this is my first java
program");
}
}
// This is a single line comment
/* These are multiple
line comments */
1/10/2025 6:21 PM Anita Agrawal CS F213
22
Lexical constraints
▪ Whitespace – Java is a free-form language (indentation is not
necessary but desirable).
» As long as there is one whitespace character between each token –
Space, tab, and newline.
▪ Identifiers – Used for class, method, and variable names.
▪ Literals – A constant value in Java is called literal
▪ Comments – /* */ and //
1/10/2025 6:21 PM Anita Agrawal CS F213
Identifiers
▪ Combination of uppercase and lowercase letters,
and numbers
▪ Underscore_ and $ sign are allowed.
▪ Must begin with an alphabet, underscore or $
▪ Case-sensitive
▪ 3-15 characters
▪ Reserved keywords cannot be used
23 1/10/2025 6:21 PM Anita Agrawal CS F213