16921 l7 inheritance

Upload: amit-kumar-verma

Post on 02-Jun-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 16921 L7 Inheritance

    1/35

    Modern Programming Tools And

    Techniques-I Inheritance

    ByArvind Kumar

    Asst. Professor, LPU

  • 8/11/2019 16921 L7 Inheritance

    2/35

    Inheritance Inheritance allows us to use one class by another class.

    A class that is derived from another class is calleda subclass (also a derived class , extended class , or child class ).

    The class from which the subclass is derived is called

    a superclass (also a base class or a parent class ).

    Classes can be derived from classes that are derived fromclasses that are derived from classes, and so on, and ultimatelyderived from the topmost class called Object.

  • 8/11/2019 16921 L7 Inheritance

    3/35

    Excepting Object, which has no superclass, every class hasone and only one direct superclass (single inheritance). In the

    absence of any other explicit superclass, every class isimplicitly a subclass of Object.

    A subclass inherits all the members (fields, methods, and

    nested classes) from its superclass. Constructors are notmembers, so they are not inherited by subclasses, but theconstructor of the superclass can be invoked from the subclass.

    A class can inherits only one superclass at a time. But a classcan have several sub classes.

  • 8/11/2019 16921 L7 Inheritance

    4/35

  • 8/11/2019 16921 L7 Inheritance

    5/35

    Simple Inheritance

    When a subclass is derived simply from it's parentclass then this mechanism is known as simpleinheritance.

    In case of simple inheritance there is only a sub classand it's parent class. It is also called single inheritanceor one level inheritance.

  • 8/11/2019 16921 L7 Inheritance

    6/35

    A subclass includes all of the members of its superclass , itcannot access those members of the superclass that have beendeclared as private .

    To inherit a class, you simply incorporate the definition of oneclass into another by using the extends keyword.

    Syntax:class sub_class_name extends super_class{

    //body of the sub class. }

  • 8/11/2019 16921 L7 Inheritance

    7/35

    class A{ int i, j;

    void showij()

    {System.out.println("i and j: " + i + " " + j);

    } }class B extends A{int k;void showk()

    { System.out.println("k: " + k); }void sum()

    { System.out.println("i+j+k: " + (i+j+k)); }

    public static void main (String args[]){B r=new B();r.sum();}

    }

  • 8/11/2019 16921 L7 Inheritance

    8/35

    Multilevel Inheritance

    It is the enhancement of the concept of inheritance. When asubclass is derived from a derived class then this mechanism isknown as the multilevel inheritance.

    The derived class is called the subclass or child class for it's parent class and this parent class works as the child class forit's just above (parent) class.

    Multilevel inheritance can go up to any number of level .

  • 8/11/2019 16921 L7 Inheritance

    9/35

    class A{ int x; int y;

    int get( int p, int q){ x=p; y=q; return (0); }void Show(){ System.out.println(x); }

    }class B extends A

    { void Showb(){ System.out.println("B"); }}class C extends B{ void display(){ System.out.println("C"); }

    public static void main(String args[]){ B a = new B();

    a.get(5,6);a.Show();

    }}

  • 8/11/2019 16921 L7 Inheritance

    10/35

    Multiple Inheritance

    The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance.

    Java does not support multiple inheritance but the multiple

    inheritance can be achieved by using the interface.

    In Java Multiple Inheritance can be achieved through use ofInterfaces by implementing more than one interfaces in a class.

  • 8/11/2019 16921 L7 Inheritance

    11/35

    Method Overloading

    Method overloading means having two or more methods withthe same name but different signatures in the same scope.

    It allows creating several methods with the same name which

    differ from each other in the type of the input and the output ofthe method.

    It is simply defined as the ability of one method to perform

    different tasks.

  • 8/11/2019 16921 L7 Inheritance

    12/35

    Exampleclass Area11

    {void area(int a)

    {int area = a*a;System.out.println("area of square is:" + area);

    }void area (int a, int b)

    {int area = a*b;

    System.out.println("area of rectangle is:" + area);}

    }

  • 8/11/2019 16921 L7 Inheritance

    13/35

    class OverloadDemo{

    public static void main (String arr[]){Area11 ar= new Area11();ar.area(10);ar.area(10,5);

    }}

  • 8/11/2019 16921 L7 Inheritance

    14/35

    Method Overriding Method overriding means having a different implementation

    of the same method in the inherited class .

    These two methods would have the same signature, but

    different implementation.

    One of these would exist in the base class and another in thederived class . These cannot exist in the same class.

  • 8/11/2019 16921 L7 Inheritance

    15/35

  • 8/11/2019 16921 L7 Inheritance

    16/35

    class Superclass{ public void printMethod()

    {System.out.println("Printed in Superclass.");

    }}class Subclass extends Superclass{ // overrides printMethod in Superclass

    public void printMethod(){

    System.out.println("Printed in Subclass");}

    public static void main(String[] args){ Subclass s = new Subclass();

    s.printMethod();}}

  • 8/11/2019 16921 L7 Inheritance

    17/35

  • 8/11/2019 16921 L7 Inheritance

    18/35

    class Override{ public void display()

    {System.out.println("Hello...This is superclass display");}

    }

    class Override1 extends Override{ public void display()

    {System.out.println("Hi...This is overriden method in

    subclass");}

    }

  • 8/11/2019 16921 L7 Inheritance

    19/35

    class OverrideDemo{

    public static void main(String arr[]){

    Override o = new Override();o.display();

    Override1 o1 = new Override1();o1.display();

    }

    }

  • 8/11/2019 16921 L7 Inheritance

    20/35

    Dynamic Method Dispatch

    Dynamic method dispatch is the mechanism by whicha call to an overridden method is resolved at run time,rather than compile time.

    Dynamic method dispatch is important because this ishow Java implements run-time polymorphism.

  • 8/11/2019 16921 L7 Inheritance

    21/35

    class A {void callme() {

    System.out.println("Inside A's callme method");}

    }

    class B extends A {

    void callme() {System.out.println("Inside B's callme method");}

    }

    class C extends A {void callme() {

    System.out.println("Inside C's callme method");}

    }

  • 8/11/2019 16921 L7 Inheritance

    22/35

    class Dispatch { public static void main(String args[]) {

    A a = new A(); // object of type AB b = new B(); // object of type BC c = new C(); // object of type CA r; // obtain a reference of type A

    r = a; // r refers to an A objectr.callme(); // calls A's version of callme

    r = b; // r refers to a B objectr.callme(); // calls B's version of callme

    r = c; // r refers to a C objectr.callme(); // calls C's version of callme}

    }

  • 8/11/2019 16921 L7 Inheritance

    23/35

  • 8/11/2019 16921 L7 Inheritance

    24/35

    class Superclass{ public void printMethod()

    {System.out.println("Printed in Superclass.");

    }}class Subclass extends Superclass{ // overrides printMethod in Superclass

    public void printMethod(){

    super.printMethod();System.out.println("Printed in Subclass");

    } public static void main(String[] args){ Subclass s = new Subclass();

    s.printMethod();}}

  • 8/11/2019 16921 L7 Inheritance

    25/35

    Using super A subclass can call a constructor defined by its

    superclass by use of the following form of super:super( arg-list);

    Here, arg-list specifies any arguments needed by theconstructor in the superclass.

    super( ) must always be the first statement executedinside a subclass constructor.

  • 8/11/2019 16921 L7 Inheritance

    26/35

    class Superclass{ Superclass ()

    {System.out.println("Printed in Superclass.");

    }}class Subclass extends Superclass{ // overrides printMethod in Superclass

    Subclass (){

    super();System.out.println("Printed in Subclass");

    } public static void main(String[] args){ Subclass s = new Subclass();}}

  • 8/11/2019 16921 L7 Inheritance

    27/35

    Private Members in a Superclass A subclass does not inherit the private members of its parent

    class. However, if the superclass has public or protectedmethods for accessing its private fields, these can also be used

    by the subclass .

    If subclass overrides public method of superclass,specify call to public method of superclass:super.MethodName(parameter list)If subclass does not override public method ofsuperclass, specify call to public method of superclass:MethodName(parameter list)

  • 8/11/2019 16921 L7 Inheritance

    28/35

    Final Classes You can declare some or all of a class's methods final . You use

    the final keyword in a method declaration to indicate that themethod cannot be overridden by subclasses.

    A final class cannot be subclassed. This is done for reasons ofsecurity and efficiency. Accordingly, many of the Javastandard library classes are final.

    A final class implicitly has all the methods as final, but notnecessarily the data members.

    The Object class does this a number of its methods are final.Syntax:public final class MyFinalClass {...}

    http://en.wikipedia.org/wiki/Class_(computer_science)http://en.wikipedia.org/wiki/Class_(computer_science)
  • 8/11/2019 16921 L7 Inheritance

    29/35

    public final class FinalClass{Void meth(){System.out.println("Printed in FinalClass.");}}

    public class SubClass extends FinalClass{

    public static void main(String[] args){

    System.out.println("Printed in SubClass."); }

    }

  • 8/11/2019 16921 L7 Inheritance

    30/35

    Final Methods

    A final method can't be overridden by subclasses.This is used to prevent unexpected behavior from asubclass altering a method that may be crucial to thefunction or consistency of the class.

    Examplepublic class MyClass{

    public final void myFinalMethod() {...}}

    http://en.wikipedia.org/wiki/Method_(computer_science)http://en.wikipedia.org/wiki/Method_overridinghttp://en.wikipedia.org/wiki/Method_overridinghttp://en.wikipedia.org/wiki/Method_(computer_science)
  • 8/11/2019 16921 L7 Inheritance

    31/35

    Abstract Methods

    A method that has only the heading with no body.

    Must be declared abstract.

    public abstract void print(); public abstract object larger(object, object);abstract void insert( int insertItem);

  • 8/11/2019 16921 L7 Inheritance

    32/35

    Abstract Classes

    A class that is declared with the reserved word abstract inits heading.

    An abstract class can contain instance variables,constructors, finalizers, and non-abstract methods.

    An abstract class can contain abstract methods. If a class contains an abstract method, the class must be

    declared abstract. You can instantiate an object of a subclass of an abstract

    class, but only if the subclass gives the definitions of all

    the abstract methods of the superclass.

  • 8/11/2019 16921 L7 Inheritance

    33/35

    Abstract Class Example public abstract class AbstractClassExample{

    protected int x; public abstract void print();

    public void setX( int a){

    x = a;}

    public AbstractClassExample(){

    x = 0;}

    }

  • 8/11/2019 16921 L7 Inheritance

    34/35

    abstract class Shape{ public static float pi = 3.142f; protected float height; protected floatwidth; abstract float area() ;}

    class Square extends Shape{Square(float h, float w){height = h; width = w;}final float area(){return height * width;}}class FinalMethodDemo{

    public static void main(String args[]){

    Square sObj = new Square(5,5);

    System.out.println("Area of square : " + sObj.area());}}

  • 8/11/2019 16921 L7 Inheritance

    35/35

    class A{void get()

    { System.out.println("A"); }}class B extends A{void get()

    { System.out.println("B"); }}class C{public static void main(String ar[]){A obj= new B();

    obj.get();}}

    Output?