After Mid Slide Set 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

OBJECT ORIENTED PROGRAMMING

(OOP)

15 TL-Sec I & II
Engr. Maria Shaikh
[email protected]

Method Overriding
If subclass (child class) has the same method as declared in the parent class,
it is known as method overriding in java.
In other words, If subclass provides the specific implementation of the
method that has been provided by one of its parent class, it is known as
method overriding.
When overriding a method, you might want to use the @Override annotation
that instructs the compiler that you intend to override a method in the
superclass .

Usage of Java Method Overriding


Method overriding is used to provide specific implementation of a method that
is already provided by its super class.
Method overriding is used for runtime polymorphism.
Engr. Maria Shaikh

Rules for Java Method Overriding


method must have same name as in the parent class
method must have same parameter as in the parent class.
must be IS-A relationship (inheritance).

Engr. Maria Shaikh

Implementation of Java Method


Overriding
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal{
@Override
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String[] args) {
Animal a = new Animal();

// Animal reference and object

Engr. Maria Shaikh

Implementation of Java Method


Overriding
Dog b = new Dog();
a.move();
b.move();

// dog reference and object


// runs the method in Animal class

Engr. Maria Shaikh

Operators
Operators are special symbols that perform specific operations on
one, two, or three operands, and then return a result.

The Simple Assignment Operator

One of the most common operators that you'll encounter is the


simple assignment operator "=".
You saw this operator in the Bicycle class; it assigns the value on
its right to the operand on its left:
int total = 0;
int speed = 0;
int gear = 1;
Engr. Maria Shaikh

The Arithmetic Operators


The Java programming language provides operators that perform
addition, subtraction, multiplication, and division.
"%", which divides one operand by another and returns the
remainder as its result.
Operator

Description

Additive operator (also used for String


concatenation)

Subtraction operator

Multiplication operator

Division operator

Remainder operator

Engr. Maria Shaikh

Implementation of Arithmetic Operators


class ArithmeticDemo {
public static void main (String[] args) {
int result = 1 + 2;
System.out.println("1 + 2 = " + result);
int original_result = result;
result = result - 1;
System.out.println(original_result + " - 1 = " + result);
original_result = result;
result = result * 2;
System.out.println(original_result + " * 2 = " + result);
original_result = result;
result = result / 2;
System.out.println(original_result + " / 2 = " + result);

Engr. Maria Shaikh

// result is now 3

// result is now 2

// result is now 4

// result is now 2

Implementation of Arithmetic Operators


(cont.)
original_result = result;
result = result + 8;
// result is now 10
System.out.println(original_result + " + 8 = " + result);
original_result = result;
result = result % 7;
// result is now 3
System.out.println(original_result + " % 7 = " + result);
}
}
Engr. Maria Shaikh

Output

Engr. Maria Shaikh

+ Operator as a Concatenation Operator


The + operator can also be used for concatenating (joining) two strings
together, as shown in the following ConcatDemo program:
class ConcatDemo {
public static void main(String[] args){
String firstString = "This is";
String secondString = " a concatenated string.";
String thirdString = firstString+secondString;
System.out.println(thirdString);
}
}

Engr. Maria Shaikh

Output

Engr. Maria Shaikh

The Unary Operators


The unary operators require only one operand; they perform various
operations such as incrementing/decrementing a value by one, negating
an expression, or inverting the value of a boolean.
Operator

Description

Unary plus operator; indicates positive value


(numbers are positive without this, however)

Unary minus operator; negates an expression

++

Increment operator; increments a value by 1

--

Decrement operator; decrements a value by 1

Logical complement operator; inverts the value of a


boolean

Engr. Maria Shaikh

Implementation of Unary Operator


class UnaryDemo {
public static void main(String[] args) {
int result = +1;
System.out.println(result);
result--;
System.out.println(result);
result++;
System.out.println(result);
result = -result;
System.out.println(result);
boolean success = false;
System.out.println(success);
System.out.println(!success);
}
}

// result is now 1
// result is now 0
// result is now 1
// result is now -1
// false
// true

Engr. Maria Shaikh

Output

Engr. Maria Shaikh

Increment / Decrement with Prefix and


Postfix
The increment/decrement operators can be applied before (prefix) or
after (postfix) the operand.
The code result++; and ++result; will both end in result being incremented
by one.
The only difference is that the prefix version (++result) evaluates to the
incremented value, whereas the postfix version (result++) evaluates to
the original value.
If you are just performing a simple increment/decrement, it doesn't
really matter which version you choose. But if you use this operator in
part of a larger expression, the one that you choose may make a
significant difference.
Engr. Maria Shaikh

Implementation of Prefix
public class PreDemo {
public static void main(String[] args) {
int a = 5;
int b = 3;
int d = a * ++b;
System.out.println(d);
}
}

// d is set to 20

Engr. Maria Shaikh

Implementation of Postfix
public class PostDemo {
public static void main(String[] args) {
int a = 5;
int b = 3;
int c = a * b++;
// c is set to 15
System.out.println(c);
}
)

Engr. Maria Shaikh

Implementation of Prefix / Postfix


class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
System.out.println(i);
// prints 4
++i;
// prints 5
System.out.println(i);
// prints 6
System.out.println(++i);
// prints 6
System.out.println(i++);
// prints 7
System.out.println(i);
}
Engr. Maria Shaikh
}

Output

Engr. Maria Shaikh

The Equality and Relational Operators


The equality and relational operators determine if one operand is greater
than, less than, equal to, or not equal to another operand. The majority of
these operators will probably look familiar to you as well. Keep in mind that
you must use "==", not "=", when testing if two primitive values are equal.
==
!=
>
>=
<
<=

equal to
not equal to
greater than
greater than or equal to
less than
less than or equal to
Engr. Maria Shaikh

Implementation of Equality and Relational


Operators
class ComparisonDemo {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
if(value1 == value2)
System.out.println("value1 == value2");
if(value1 != value2)
System.out.println("value1 != value2");
if(value1 > value2)
System.out.println("value1 > value2");
if(value1 < value2)
System.out.println("value1 < value2");
if(value1 <= value2)
System.out.println("value1 <= value2");
}
Engr. Maria Shaikh
}

Output

Engr. Maria Shaikh

The Conditional Operators


The && and || operators perform Conditional-AND and Conditional-OR
operations on two boolean expressions. These operators exhibit "shortcircuiting" behavior, which means that the second operand is evaluated
only if needed.
&& Conditional-AND
|| Conditional-OR

Engr. Maria Shaikh

Implementation of Conditional Operators


class ConditionalDemo1 {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
if((value1 == 1) && (value2 == 2))
System.out.println("value1 is 1 AND value2 is 2");
if((value1 == 1) || (value2 == 1))
System.out.println("value1 is 1 OR value2 is 1");
}
}

Engr. Maria Shaikh

Output

Engr. Maria Shaikh

Bitwise and Bit Shift Operators


The Java programming language also provides operators that perform bitwise and
bit shift operations on integral types. The operators discussed are less commonly
used. Therefore, their coverage is brief; the intent is to simply make you aware
that these operators exist.
The unary bitwise complement operator "~" inverts a bit pattern; it can be applied
to any of the integral types, making every "0" a "1" and every "1" a "0". For example,
a byte contains 8 bits; applying this operator to a value whose bit pattern is
"00000000" would change its pattern to "11111111".
The signed left shift operator "<<" shifts a bit pattern to the left, and the signed
right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given
by the left-hand operand, and the number of positions to shift by the right-hand
operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost
position, while the leftmost position after ">>" depends on sign extension.
Engr. Maria Shaikh

Bitwise and Bit Shift Operators (cont.)


The bitwise & operator performs a bitwise AND operation.
The bitwise ^ operator performs a bitwise exclusive OR
operation.
The bitwise | operator performs a bitwise inclusive OR
operation.

Engr. Maria Shaikh

Implementation of Complement Operator


class ComplementlDemo1 {
public static void main(String[] args) {
byte x = 3;
byte y = 5;
System.out.println(~x);
System.out.println(~y);
}
}

Engr. Maria Shaikh

Implementation of bitwise AND operator


class BitDemo {
public static void main(String[] args) {
int bitmask = 0x000F;
int val = 0x2222;
// prints "2"
System.out.println(val & bitmask);
}
}

Engr. Maria Shaikh

END OF SLIDES

Engr. Maria Shaikh

You might also like