after mid slide set 1

Upload: maya

Post on 19-Feb-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 After Mid Slide Set 1

    1/31

    OBJECT ORIENTED PROGRAMM

    (OOP)

    15 TL-Sec I & IIEngr. Maria Shaikh

    [email protected]

  • 7/24/2019 After Mid Slide Set 1

    2/31

    Method Overriding

    If subclass (child class) has the same method as declared in the

    it is known as method overriding in java. In other words, If subclass provides the specific implementation

    method that has been provided by one of its parent class, it is kmethod overriding.

    When overriding a method, you might want to use the @Over

    that instructs the compiler that you intend to override asuperclass .

    Usage of Java Method Overriding Method overriding is used to provide specific implementation of

    is already provided by its super class.

    Method overriding is used for runtime polymorphism.Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    3/31

    Rules for Java Method Overrid

    method must have same name as in the parent class

    method must have same parameter as in the parent c

    must be IS-A relationship (inheritance).

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    4/31

    Implementation of Java MethoOverriding

    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

  • 7/24/2019 After Mid Slide Set 1

    5/31

    Implementation of Java MethoOverriding

    Dog b = new Dog(); // dog reference and object

    a.move(); // runs the method in Animal cb.move();

    }

    }

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    6/31

    Operators

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

    The Simple Assignment Operator

    One of the most common operators that you'll enco

    simple assignment operator "=". You saw this operator in the Bicycle class; it assignsits right to the operand on its left:

    int total = 0;

    int speed = 0;

    int gear = 1;Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    7/31

    The Arithmetic Operators

    The Java programming language provides operators thaaddition, subtraction, multiplication, and division.

    "%", which divides one operand by another and returnsremainder as its result.

    Engr. Maria Shaikh

    Operator Description

    + Additive operator (also used for Stconcatenation)

    - Subtraction operator

    * Multiplication operator

    / Division operator

    % Remainder operator

  • 7/24/2019 After Mid Slide Set 1

    8/31

    Implementation of Arithmetic Ope

    class ArithmeticDemo {

    public static void main (String[] args) {int result = 1 + 2;

    System.out.println("1 + 2 = " + result); // result

    int original_result = result;

    result = result - 1;

    System.out.println(original_result + " - 1 = " + result); // resu

    original_result = result;

    result = result * 2;

    System.out.println(original_result + " * 2 = " + result); // re

    original_result = result;

    result = result / 2;

    System.out.println(original_result + " / 2 = " + result); // re

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    9/31

    Implementation of Arithmetic Ope(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

  • 7/24/2019 After Mid Slide Set 1

    10/31

    Output

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    11/31

    + Operator as a Concatenation Op The + operator can also be used for concatenating (joining) tw

    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

  • 7/24/2019 After Mid Slide Set 1

    12/31

    Output

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    13/31

    The Unary Operators

    The unary operators require only one operand; they perform v

    operations such as incrementing/decrementing a value by onean expression, or inverting the value of a boolean.

    Engr. Maria Shaikh

    Operator Description

    +Unary plus operator; indicates pos

    (numbers are positive without this,

    - Unary minus operator; negates an++ Increment operator; increments a v

    -- Decrement operator; decrements a

    !Logical complement operator; inve

    boolean

  • 7/24/2019 After Mid Slide Set 1

    14/31

    Implementation of Unary Opera

    class UnaryDemo {

    public static void main(String[] args) {int result = +1;

    System.out.println(result); // result is now 1

    result--;

    System.out.println(result); // result is now 0

    result++;

    System.out.println(result); // result is now 1

    result = -result;

    System.out.println(result); // result is now -1

    boolean success = false;

    System.out.println(success); // false

    System.out.println(!success); // true

    }

    } Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    15/31

    Output

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    16/31

    Increment / Decrement with PrefPostfix

    The increment/decrement operators can be applied before

    after (postfix) the operand. The code result++; and ++result; will both end in result being

    by one.

    The only difference is that the prefix version (++result) evalincremented value, whereas the postfix version (result++)

    the original value. If you are just performing a simple increment/decrement

    really matter which version you choose. But if you use thispart of a larger expression, the one that you choose msignificant difference.

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    17/31

    Implementation of Prefix

    public class PreDemo {

    public static void main(String[] args) {int a = 5;

    int b = 3;

    int d = a * ++b; // d is set to 20

    System.out.println(d);

    }

    }

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    18/31

    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

  • 7/24/2019 After Mid Slide Set 1

    19/31

    Implementation of Prefix / Post

    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

  • 7/24/2019 After Mid Slide Set 1

    20/31

    Output

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    21/31

    The Equality and Relational Opera

    The equality and relational operators determine if one operan

    than, less than, equal to, or not equal to another operand. Thethese operators will probably look familiar to you as well. Keepyou must use "==", not "=", when testing if two primitive values

    == equal to

    != not equal to> greater than

    >= greater than or equal to

    < less than

  • 7/24/2019 After Mid Slide Set 1

    22/31

    Implementation of Equality and RelaOperators

    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

  • 7/24/2019 After Mid Slide Set 1

    23/31

    Output

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    24/31

    The Conditional Operators

    The && and || operators perform Conditional-AND and Condit

    operations on two boolean expressions. These operators exhibcircuiting" behavior, which means that the second operand is only if needed.

    && Conditional-AND

    || Conditional-OR

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    25/31

    Implementation of Conditional Ope

    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

  • 7/24/2019 After Mid Slide Set 1

    26/31

    Output

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    27/31

    Bitwise and Bit Shift Operato

    The Java programming language also provides operators that perform

    bit shift operations on integral types. The operators discussed are leused. Therefore, their coverage is brief; the intent is to simply makethat these operators exist.

    The unary bitwise complement operator "~" inverts a bit pattern; it cto any of the integral types, making every "0" a "1" and every "1" a "0a byte contains 8 bits; applying this operator to a value whose bit pat"00000000" would change its pattern to "11111111".

    The signed left shift operator "" shifts a bit pattern to the right. The bit patby the left-hand operand, and the number of positions to shift by thoperand. The unsigned right shift operator ">>>" shifts a zero into thposition, while the leftmost position after ">>" depends on sign exten

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    28/31

    Bitwise and Bit Shift Operators (

    The bitwise & operator performs a bitwise AND oper

    The bitwise ^ operator performs a bitwise exclusive operation.

    The bitwise | operator performs a bitwise inclusive Ooperation.

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    29/31

    Implementation of Complement Op

    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

  • 7/24/2019 After Mid Slide Set 1

    30/31

    Implementation of bitwise AND op

    class BitDemo {

    public static void main(String[] args) {int bitmask = 0x000F;

    int val = 0x2222;

    // prints "2"

    System.out.println(val & bitmask);

    }

    }

    Engr. Maria Shaikh

  • 7/24/2019 After Mid Slide Set 1

    31/31

    END OF SLIDES

    Engr. Maria Shaikh