java inhertance infaces

Upload: pranay-kinra

Post on 06-Jul-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 Java Inhertance Infaces

    1/23

     Java

  • 8/17/2019 Java Inhertance Infaces

    2/23

    Inheritance Inheritance is a mechanism in which one object acquires all the

    properties and behaviours of parent object.

     The idea behind inheritance is that you can create new classes that arebuilt upon existing classes.

    When you inherit from an existing class, you reuse or inherit! methodsand "elds, and you add new methods and "elds to adapt your new classto new situations.

     Inheritance represents the IS-A relationship.

    Why use Inheritance#

    $%or &ethod 'verriding (o )untime *olymorphism!.

    $%or +ode )eusability.

    class (ubclassname extends (uperclassname

    -methods and "elds

    /

     The 0eyword extends indicates that you are ma0ing a new class thatderives from an existing class.

    In the terminology of Java, a class that is inherited is called asuperclass. The new class is called a subclass.

  • 8/17/2019 Java Inhertance Infaces

    3/23

    Inheritance 1asics

     This is how the extends 0eyword is used toachieve inheritance. public class Animal{

    }

     public class Mammal extends Animal{

    }

     public class Reptile extends Animal{

    }

     public class Dog extends Mammal{

    }

  • 8/17/2019 Java Inhertance Infaces

    4/23

    Is a example

    class 2mployee-

    3oat salary4566667 can be protected or public

    /

    class *rogrammer extends 2mployee

    -int bonus4866667

    public static void main(tring args9:!

    -

    *rogrammer p4new *rogrammer!7(ystem.out.println;*rogrammer salary is

  • 8/17/2019 Java Inhertance Infaces

    5/23

    'utput

  • 8/17/2019 Java Inhertance Infaces

    6/23

     Types of Inheritance

  • 8/17/2019 Java Inhertance Infaces

    7/23

     Types of Inheritance

  • 8/17/2019 Java Inhertance Infaces

    8/23

    (ingle Inheritance8. class >nimal-

    ?. public void move!-

    @. (ystem.out.println;>nimals can move;!75. /

    A. /

    B. class Cog extends >nimal-

    D. public void move!-

    E. (ystem.out.println;Cogs can wal0 and run;!7//F. class TestCog-

    86. public static void main(tring args9:!-

    88. >nimal a 4 new >nimal!7

    8?. >nimal reference and object

    8@. Cog b 4 new Cog!785.

    8A. a.move!7 runs the method in >nimal class

    8B. b.move!7)uns the method in Cog class

    8D. /

    8E./

  • 8/17/2019 Java Inhertance Infaces

    9/23

    class +alculation

    -

    int G7

    public void additionint x, int y!

    - G4x=y7(ystem.out.println;The sum of the given numbers

  • 8/17/2019 Java Inhertance Infaces

    10/23

    (uper!. class "oom

    ?. -

    @. int length75. int breadth7

    A. )oomint x, int y!

    B. -

    D. length4 x7

    E. breadth4y7F. /

    86.int area!

    88.-

    8?.return length breadth!7

    8@.//!#. class $ed"oom extends

    "oom

    8A. -

    8B. int height7

    8. 1ed)oom int x, int y, int G!

    ?. -

    @. superx,y!75. height 4 G7

    A. /

    B. int vol!

    D. -

    E. returnlength breadth height!7

    F. /86./

    !!.class Inher%est

    8?.-

    [email protected] static void main(tring args9:!

    85.-

    8A.1ed)oom room84 new1ed)oom?,@,5!7

    8B.int area84 room8.area!7

    8D.int vol84 room8.vol!7

    8E.(ystem.out.printlnarea8!7

    8F. (ystem.out.printlnvol8!7

    ?6. /

    ?8. /

  • 8/17/2019 Java Inhertance Infaces

    11/23

    (uper

    • (uper may only be used in a subclassconstructor.

    • +all to superclass constructor mustbe the "rst statement in subclassconstructor

    • *arameter of super call must matchthe order or type of the instancevariable of the super class.

  • 8/17/2019 Java Inhertance Infaces

    12/23

    Cefault constructor of superclassis available to subclass by default

    class 'ne -

    'ne!

    -

    (ystem.out.println;one;!7

    /

    /class Two extends 'ne -

     Two!

    -

    (ystem.out.println;Two;!7

    //

    class (uper8 -

    public static void main(tring args9:! -

     Two sub'b 4 new Two!7

    /

    /

    'utput<one

     Two

  • 8/17/2019 Java Inhertance Infaces

    13/23

    Ksing super to +all (uperclass variable

    class Lehicle-

    int speed4A67

    /

    class 1i0e extends Lehicle-

    int speed48667

    void display!-

    (ystem.out.printlnsuper.speed!7 will print speed of Lehicle now

    /

    public static void main(tring args9:!-

    1i0e b4new 1i0e!7b.display!7

    /

    /

    'utput<A6

  • 8/17/2019 Java Inhertance Infaces

    14/23

    Ksing super to +all (uperclass constructor

    class 'ne -

    'neint i!

    -

    (ystem.out.printlni!7

    /

    /

    class Two extends 'ne - Twoint a!

    -

    supera!7

    (ystem.out.println;Two;!7

    //

    class (uper8 -

    public static void main(tring args9:! -

     Two sub'b 4 new Two?6!7

    /

    /

    'utput<?6

     Two

  • 8/17/2019 Java Inhertance Infaces

    15/23

  • 8/17/2019 Java Inhertance Infaces

    16/23

    Ksing super to +all (uperclass method

    class *erson-

    void message!

    -(ystem.out.println;welcome;!7/

    /

    class (tudent extends *erson-

    void message!-(ystem.out.println;welcome to java;!7/

    void display!-

    message!7 will invo0e current class message! method

    super.message!7 will invo0e parent class message! method

    /public static void main(tring args9:!-

    (tudent s4new (tudent!7

    s.display!7

    /

    /

    'utput<welcome to java

    welcome

  • 8/17/2019 Java Inhertance Infaces

    17/23

    &ultilevel Inheritance

    !.class A &

    ?.int x7 int y7

    @.int getint p, int q!-

    5.x4p7 y4q7 return6!7

    A./B.void (how!-

    D.(ystem.out.printlnx!7

    E./

    F./

    !'.class $ extends A-

    88.void (howb!-

    8?.(ystem.out.println;1;!7

    8@./ //

    !.class C extends $&

    ?.void display!-

    @.(ystem.out.println;+;!75./

    A.public static voidmain(tring args9:!-

    B.+ c 4 new +!7D.c.getA,B!7

    E.c.(how!7

    F./

  • 8/17/2019 Java Inhertance Infaces

    18/23

    Interfaces

    • Interfaces are to support the conceptof multiple inheritance.

    •  Java cannot be a subclass of morethan one superclass but it canimplements more than one interface.

  • 8/17/2019 Java Inhertance Infaces

    19/23

    • Interface Item

    -

    (tatic "nal int code 4 @667(tring name4M%anN7

    void show!7

    /

  • 8/17/2019 Java Inhertance Infaces

    20/23

  • 8/17/2019 Java Inhertance Infaces

    21/23

    Interface

    !. interace Area

    ?. - "nal static 3oat [email protected]%7

    @. 3oat compute3oat x, 3oat y!7 /

    #. class "ectanle implementsArea

    A. -

    *. public +oat compute(+oat x,+oat y)

    D. - return x y!7 / /

    . class Circle implements Area

    F. - public +oat compute (+oatx, +oat y)

    86.- return pi x x!7 / /

    !. class Interace%est

    ?. - public static void main(tring args9 :!

    . & "ectanle rect = new"ectanle()

    #. Circle cir = new Circle()

    /. Area area*. area=rect

    D. (ystem.out.println;>rea; =area.compute86,?6!!7

    . area=cir

    F. (ystem.out.printlnarea.compute86,?6!!7

    86./

    88./

  • 8/17/2019 Java Inhertance Infaces

    22/23

    • class Student -

    • int rollOumber7

    • void getOumberint n! -

    rollOumber 4 n7/• void putOumber!

    -(ystem.out.println;)ollOKmber< ; = rollOumber!7 //

    • class %est extends Student-

    • 3oat part8, part?7

    void get&ar0s3oat m8, 3oat m?!-

    • part8 4 m87

    • part? 4 m?7 /

    void put&ar0s! -(ystem.out.println;&ar0s

    'btained;!7

    (ystem.out.println;*art 8 4;=part8!7

    (ystem.out.println;*art ? 4;=part?!7 //

    •  class "esults extends %estimplements Sports -3oat total7

    • public void putWt! -

    (ystem.out.println;(ports Wt 4 ;=sportWt!7/

    • void display! -

    • total 4 part8 = part? = sportWt7

    • putOumber!7

    • put&ar0s!7

    • putWt!7• (ystem.out.println;Total (core 4;

    =total!7 //

    • class 0ybrid extends "esults -

    • public static void main(tring9:

    args! -

    • )esults student8 4 new )esults!7

    • student8.getOumber8?@5!7

    • student8.get&ar0s?D.A%, @@.6%!7

    • student8.display!7 //

  • 8/17/2019 Java Inhertance Infaces

    23/23

      (tudent

     Test (ports

      )esult

      Pybrid