J2SE - Core Java - PG-DAC - Session 4v1

download J2SE - Core Java - PG-DAC - Session 4v1

of 40

Transcript of J2SE - Core Java - PG-DAC - Session 4v1

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    1/40

    J2SE Core Java

    PG-DESD

    Aug -2013

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    2/40

    Session 4:

    Learning Objectives

    By the end of this session, you must be able to

    Describe Inheritance

    Explain Method Overriding in classes

    Understand run-time polymorphism

    Use super keyword

    Explain Abstract Class

    Describe Interfaces

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    3/40

    Inheritance

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    4/40

    INHERITANCE isone of the pillars of object-orientation

    A new class is derived from an existing class:1) Existing class is called super-class2) Derived class is called sub-class

    A sub-class is a specialized version of its super-class:1) has all non-private members of its super-class2) may provide its own implementation of super-class

    methods

    Objects of a sub-class are a special kind of objects of a super-

    class

    Inheritance

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    5/40

    Class Hierarchy

    A child class of one parent can be the parent of another child,forming class hierarchies

    At the top of the hierarchy theres a default class called Object

    Animal

    Reptile Bird Mammal

    Snake Lizard BatHorseParrot

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    6/40

    Class Object

    In Java, all classes use inheritance.

    If no parent class is specified explicitly, the base class Objectisimplicitly inherited.

    All classes defined in Java, is a child of Objectclass, whichprovides minimal functionality guaranteed to e common to allobjects.

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    7/40

    Class Object

    Methods defined in Object class are;1. equals(Object obj) Determine whether the argument object is the

    same as the receiver

    2. getClass() Returns the class of the receiver, an object of type Class

    3. hashCode()Returns a hash value for this object. Should be

    overridden when the equals method is changed

    4. toString() Converts object into a string value. This method is alsooften overridden

    5. finalize() Called by the garbage collector on an object when garbagecollectior determines that there are no more references to the object

    6. clone() Creates and returns a copy of this object.7. wait()

    8. wait(long timeout)

    9. wait(long timeout, int nanos)

    10. notify()

    11. notifyAll()

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    8/40

    Extends Key Word

    It is a keyword used to inherit a class from another class

    Allows to extend from only one class

    class One

    {

    int a=5;

    }

    class Two extends One

    {

    int b=10;

    }

    One baseobj=new One(); // base class object.

    Two subobj=new Two(); // child class object

    super class objectbaseobjcan be used to refer its sub class objects.

    For example,Baseobj=subobj// now its pointing to sub class

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    9/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    10/40

    Example

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    11/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    12/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    13/40

    Exampleclass SuperClass{

    void m1(){

    System.out.println("Super Class m1");

    }

    }

    public class SubClass extends SuperClass{void m1(){

    System.out.println("Sub Class m1");

    }

    public static void main(String[] args) {

    SubClass sb=new SubClass();sb.m1();

    SuperClass sp=new SubClass();

    sp.m1();

    }

    }

    extends SuperClass {

    Syntax for

    Inheriting a

    class

    Overriding the

    methodSuperClass sp=new SubClass();

    Reffering a

    sub class

    object usingsuper class

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    14/40

    The Benefits of Inheritance

    Software Reusability (among projects)

    Increased Reliability (resulting from reuse and sharing of well-tested code)

    Code Sharing (within a project)

    Consistency of Interface (among related objects)

    Software Components

    Rapid Prototyping (quickly assemble from pre-existingcomponents)

    Polymorphism and Frameworks (high-level reusablecomponents)

    Information Hiding

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    15/40

    The Costs of Inheritance

    Execution Speed

    Program Size

    Message-Passing Overhead

    Program Complexity (in overuse of inheritance)

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    16/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    17/40

    1 . Referring immediate parent class instance variable

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    18/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    19/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    20/40

    1. Final variable can not be changed

    2. Final method can not be overridden

    3. Final class can not be inherited

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    21/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    22/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    23/40

    abstract class - features

    If a class contains at least one abstractmethod should be declared asabstract

    We can declare a class as abstract, even if the class does not have anyabstractmethods

    Abstract class can not be instantiated but can be referred

    If a class is extending from an abstractclass, the extending class shouldprovide the body(implementation) for all the abstractmethods of superclass

    If the extended class fails to provide body for at least one abstractmethod, should be declared abstract

    l M Cl {

    b t t

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    24/40

    class MyClass{

    void m1(){

    System.out.println("in MyClass m1");

    }

    abstract void m2();

    }

    abstract class MyClass{void m1(){

    System.out.println("in MyClass m1");

    }

    void m2(){

    System.out.println("in MyClass m1");}

    }

    Abstract class Examples

    abstract void m2();

    This class should be

    declared abstract

    because of this

    abstract method

    abstract

    abstract

    This class can be

    declared abstract even

    it does not have any

    abstract methods

    An abstract can also have all abstract methods, but not so

    useful, for this we have a different mechanism called

    interfaces

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    25/40

    abstract class MyClass{

    abstract void m1();

    abstract void m2();

    void m3(){

    System.out.println("in MyClass m3");}

    }

    public class AbstractTest extends MyClass{

    void m1(){

    System.out.println("MyClass-m1 defined by AbstractTest");

    }

    void m2(){

    System.out.println("MyClass-m2 defined by AbstractTest");

    }

    public static void main(String[] args) {

    MyClass mc=new AbstractTest();

    mc.m1(); mc.m2(); mc.m3();

    }

    }

    Example

    abstract void m1();

    abstract void m2();

    This class is having

    2 Abstract

    methods

    void m1(){

    System.out.println("MyClass-m1 defined by AbstractTest");

    }

    void m2(){

    System.out.println("MyClass-m2 defined by AbstractTest");

    }

    So we are

    Implementing the 2

    Abstract methods of

    super class

    AbstractTest extends MyClass{

    AbstractTest should implement the 2

    abstract methods of MyClass

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    26/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    27/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    28/40

    Interfaces

    An interfaceis a reference type, similar to a class, that can

    contain onlyconstants, method signatures, and nested types.

    There are no method bodies.

    Interfaces cannot be instantiatedthey can only be implementedbyclasses or extendedby other interfaces.

    A method declaration within an interface is followed by a semicolon, butno braces.

    All methods declared in an interface are implicitly public.

    An interface can contain constant declarations in addition to methoddeclarations.

    All constant values defined in an interface are implicitly public staticfinal.

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    29/40

    Uses of Interfaces

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    30/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    31/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    32/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    33/40

    Run time Polymorphism

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    34/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    35/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    36/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    37/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    38/40

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    39/40

    Interface vs Abstract Class

    Abstract Classes1. Contain one or more abstract

    methods, can contain noabstract methods also i.e. onlyconcrete methods & can haveinstance variables.

    2. Keyword abstract

    3. Contain private ,protected andpublic members.

    4. A class extending an abstractclass need not implement any ofthe methods defined in theabstract class

    Interfaces1. Contain only method

    declarations and public staticfinal constants

    2. Keyword interface

    3. Only have public members

    4. A class implementing aninterface must implement all of

    the methods defined in theinterface

  • 8/13/2019 J2SE - Core Java - PG-DAC - Session 4v1

    40/40

    Next

    Packages

    Access Modifiers

    Wrapper Classes