Lecture 03 06092022
Lecture 03 06092022
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
} 4
} BITS Pilani, Pilani Campus
Questions
1. When you compile a program written in the Java programming language, the
compiler converts the human-readable source file into platform-independent code that
a Java Virtual Machine can understand. What is this platform-independent code called?
Answer: Bytecode
4. When declaring the main method, which modifier must come first, public or static?
Answer: They can be in either order, but the convention is public static
5
BITS Pilani, Pilani Campus
Inheritance
more accurately
webbed feet.
When a class inherits from another class it inherits both the states
and methods of that class, so in the case of the Car class inheriting
from the Vehicle class the Car class inherits the methods of
the Vehicle class
engineStart(), gearChange(), lightsOn() etc.
The Car class will also inherit the states of the Vehicle class
isEngineOn, isLightsOn, numberWheels etc.
Polymorphism means "multiple forms".
In OOP these multiple forms refer to multiple forms of the same
method, where the exact same method name can be used in
different classes, or the same method name can be used in the
same class with slightly different parameters.
There are two forms of polymorphism:
Over-riding
Over-loading
10
11
Object-Oriented Programming
Lecture -3 [Variables, Operators, Expressions, Statements and Blocks]
Lecture Outline
Language Basics
▪ Variables
▪ Primitive Data Types
▪ Operators
▪ Assignment, Arithmetic, and Unary Operators
▪ Equality, Relational, and Conditional Operators
▪ Bitwise and Bit Shift Operators
▪ Expressions, Statements, and Blocks
▪ Control Flow Statements
▪ if – then and if – then – else
▪ switch
▪ while and do – while
▪ for and branching statements
In the Java programming language, the terms "field" and "variable" are both used; this is a
common source of confusion among new developers, since both often seem to refer to
the same thing.
▪ Class Variables (Static Fields) - A class variable is any field declared with the
static modifier; this tells the compiler that there is exactly one copy of this
variable in existence, regardless of how many times the class has been
instantiated. A field defining the number of gears for a particular kind of bicycle
could be marked as static since conceptually the same number of gears will
apply to all instances. The code static int numGears = 6; would create such a
static field. Additionally, the keyword final could be added to indicate that the
number of gears will never change.
▪ Local Variables - a method will often store its temporary state in local
variables. There is no special keyword designating a variable as local;
that determination comes entirely from the location in which the
variable is declared — which is between the opening and closing braces
of a method. As such, local variables are only visible to the methods in
which they are declared; they are not accessible from the rest of the
class.
• For more than one word, capitalize the first letter of each subsequent word.
▪ Example: gearRatio, numberOfGears, monthlySalary etc
• Capitalize every letter and separate subsequent words with the underscore
character for variables that are made to store constant value, such that
those with modifiers static, final, and static final both.
▪ Example: static final NUM_GEARS = 6;
8
BITS Pilani, Pilani Campus
Primitive Data Types (Contd.)
• It's not always necessary to assign a value when a field is declared. Fields that are
declared but not initialized will be set to a reasonable default by the compiler.
Generally speaking, this default will be zero or null, depending on the data type.
Relying on such default values, however, is generally considered bad programming
style.
Operators Precedence
postfix expr++ expr–
unary ++expr --expr +expr -expr ~ !
multiplicative */%
additive +-
shift << >> >>>
relational < > <= >= instanceof
equality == != Operators with higher precedence are evaluated before
bitwise AND & operators with relatively lower precedence. Operators on the
same line have equal precedence. When operators of equal
bitwise exclusive OR ^
precedence appear in the same expression, a rule must govern
bitwise inclusive OR | which is evaluated first. All binary operators except for the
assignment operators are evaluated from left to right;
logical AND &&
assignment operators are evaluated right to left.
logical OR ||
ternary ?:
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
14
BITS Pilani, Pilani Campus
Integer and Floating point division
15
BITS Pilani, Pilani Campus
Type Casting
• A type cast takes a value of one type and produces a value of another
type with an "equivalent" value
– If n and m are integers to be divided, and the fractional portion of the
result must be preserved, at least one of the two must be type cast to
a floating-point type before the division operation is performed
double ans = n / (double)m;
– Note that the desired type is placed inside parentheses immediately in
front of the variable to be cast
– Note also that the type and value of the variable to be cast does not
change
16
BITS Pilani, Pilani Campus
More on Type Casting
17
BITS Pilani, Pilani Campus
Questions