sub classes

Upload: chandrakant

Post on 06-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Sub Classes

    1/28

    Sub Classes

  • 8/2/2019 Sub Classes

    2/28

    Sub Classes

    The process of creating a new class from theexisting class is called as Sub Class orinheritance in Java

    The Existing Class is called as Super class

    The New class is called as Sub Class

  • 8/2/2019 Sub Classes

    3/28

    Sub Classes

    The Sub class is also called the child class andsuper class is also called the Parent class. InJava a child class has only one super class.

    In Java all classes must be derived from someclass. The top-most class from which all otherclasses are derived is the Object class definedin java.lang package.

  • 8/2/2019 Sub Classes

    4/28

    Eff ect o f Sub Classes

    All the Fields of Super class are available inSub Class

    All the Methods of Super class are available inSub Class

    All the Constructor of Super class are availablein Sub Class

  • 8/2/2019 Sub Classes

    5/28

    Typ es o f Sub Classes

    Single

    Multiple

    Multi Level

    Tree

    Hybrid

  • 8/2/2019 Sub Classes

    6/28

    Single Inheritance

    In case of singleinheritance there isonly a sub class and

    one parent class. It isalso called singleinheritance or one levelinheritance.

  • 8/2/2019 Sub Classes

    7/28

    class Person{

    String Name;

    }

    class Student extends Person{

    int RollNo;

    }class Single{

    public static void main(String abc[]){

    Student s1=new Student();s1.RollNo=1001;s1.Name="ABC";System.out.println("RollNo is "+ s1.RollNo);System.out.println("Name is "+ s1.Name);

    }}

  • 8/2/2019 Sub Classes

    8/28

    M ulti p le Inheritance

    W hen a subclass is derived from a derivedclass then this mechanism is known as themultilevel inheritance.

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

  • 8/2/2019 Sub Classes

    9/28

    M ulti p le Inheritance

  • 8/2/2019 Sub Classes

    10/28

    Java does not support multiple inheritancebut the multiple inheritances can be achievedby using the interface .

    M ulti p le Inheritance

  • 8/2/2019 Sub Classes

    11/28

    M ulti Level Inheritance

    Java Does not support multiple inheritancedirectly. The mechanism of inheriting thefeatures of more than one base class into asingle class is known as multiple inheritance.

    Java does not support multiple inheritancebut the multiple inheritances can be achievedby using the interface.

  • 8/2/2019 Sub Classes

    12/28

    M ulti Level Inheritance

  • 8/2/2019 Sub Classes

    13/28

    class A{

    int i;}

    class B extends A{

    int j;}

    class C extends B{

    int k;}

    class ML

    {

    public static void main(String abc[])

    {

    B b1=new B();

    C c1=new C();

    b1.j=1000;b1.i=2000;

    c1.k=100;

    c1.i=200;

    System.out.println(b1.j);System.out.println(b1.i);

    System.out.println(c1.k);

    System.out.println(c1.i);

    }

    }

  • 8/2/2019 Sub Classes

    14/28

    T ree Inheritance

    It is a method of inheritance where one or moresub classes is derived from common super class.

    All the elements of hierarchical data model iscalled as node.

    The highest level of element in the hierarchicaldata model is called as root and the lowest levelof element is called as leaves.

  • 8/2/2019 Sub Classes

    15/28

    T ree Inheritance

    Class A

    Class X

    Class X1 Class X2

    Class Y

    Class Y1 Class Y2

  • 8/2/2019 Sub Classes

    16/28

    class A{

    int i;}class B extends A{

    int j;

    }class C extends A{

    int k;}

    class Tree{

    public static void main(String abc[]){

    B b1=new B();C c1=new C();

    b1.j=1000;b1.i=2000;

    c1.k=100;c1.i=200;

    System.out.println(b1.j);System.out.println(b1.i);

    System.out.println(c1.k);System.out.println(c1.i);

    }}

  • 8/2/2019 Sub Classes

    17/28

    H y brid Inheritance

    It is a method where one or more types of inheritance are combined together and used.

    This type of inheritance is combination of single, multiple, multiple and treeinheritance.

  • 8/2/2019 Sub Classes

    18/28

    H y brid Inheritance

    Class A

    Class B

    Class D

    Class C

    Class E Class F

  • 8/2/2019 Sub Classes

    19/28

    class A{

    int i;}class B extends A{

    int j;}class C extends A

    {int k;

    }class D extends B{

    int l;}class E extends D{

    int m;}

    class Hybrid{

    public static void main(String abc[]){

    C c1=new C();c1.k=100;c1.i=200;System.out.println(c1.k);System.out.println(c1.i);

    }}

  • 8/2/2019 Sub Classes

    20/28

    Method Overriding

    In a class hierarchy, when a method in a subclass has thesame name and type signature as a method in itssuperclass, then the method in the subclass is said tooverride the method in the super class.

    W hen an overridden method is called from a subclass, Javawill always refer to method defined by the subclass. Themethod of the super class will be ignore/hidden.

    In order to override any particular method of super class,the return type, method name, type and order of arguments must be identical to method of sub class.

  • 8/2/2019 Sub Classes

    21/28

    class A{

    void Print(){System.out.println("SPIRE");}

    }class B extends A{

    void Print(){System.out.println("Academy");}

    }

    class Test{

    public static void main(Stringabc[]){

    B b1=new B();b1.Print();

    }

    }

  • 8/2/2019 Sub Classes

    22/28

    Am biguit y in Subclass

    W e can create a method with the same namein Super Class and Sub Class.

    If we want to solve this problem then we useFinal keyword.

  • 8/2/2019 Sub Classes

    23/28

    Am biguit y in Subclass

    W e can create a method with the same namein Super Class and Sub Class. W hen we callthe method JVM execute the child classmethod.

    If we want to execute the super class methodthen we can use the Super keyword.

  • 8/2/2019 Sub Classes

    24/28

    Constructor in Subclass

    Case 1 Default Constructor

    Case 2 Parameterized Constructor

  • 8/2/2019 Sub Classes

    25/28

    Constructor in Subclass

    Case 1 Default ConstructorW hen we create an instance of the child class ,

    JVM first call the default constructor of theSuper class then call the default constructor of the Child class

  • 8/2/2019 Sub Classes

    26/28

    Case 2 Parameterized ConstructorW hen we create an instance of the child class ,

    JVM first call the default constructor of theSuper class then call the default constructor of the Child class

    W e must Initialize all the instance variable of Super class in the child Class Constructor.

    Constructor in Subclass

  • 8/2/2019 Sub Classes

    27/28

    A dvantage

    Code Reusability

    Implementation of Run Time PolymorphismMethod Overriding

    Specialization and Generalization can beImplemented

  • 8/2/2019 Sub Classes

    28/28

    D isadvantage

    Complexity Increase

    Constructor in Subclass