Saturday, 3 March 2018

Static Keyword in inheritence in Java

We will see how inheritance is working with static keyword. Here static methods can be inherited but not overridden and also polymorphism also not applicable for static inherited methods.

1. we can access the super class static methods and variables  using child class name.
2. we can access the super class static methods and variables using child class object. But not suggestible way.


Parent class with static method and variable:

package com.sample.javase.testing;

public class StaticParentClass {

    static int a = 12;

    static void display() {
        System.out.println("this is static method");
    }
   
    final static void show() {
        System.out.println("this is static final method");
    }

    void display1() {
        System.out.println("this is object method");
    }

}

Child class:

package com.sample.javase.testing;

public class StaticInheritenceTest extends StaticParentClass{

    static void parentDisplay() {
        System.out.println("this is static child method");
    }
   
    //this method will give compilation error as we put final keyword in parent class for this method.
    /*static void show() {
        System.out.println("this is static final method");
    }*/
    public static void main(String[] args) {
        StaticInheritenceTest inheritenceTest = new StaticInheritenceTest();
//we can access parent class static method using child class object but not suggested
        inheritenceTest.display();
// we can access parent class static variable using child class object but not suggested
        System.out.println(inheritenceTest.a);
        inheritenceTest.display1();
        inheritenceTest.parentDisplay();
        StaticParentClass.display();
// we can access parent class static method using child class name
        StaticInheritenceTest.display();
// we can access parent class static variable using child class name
        System.out.println(StaticInheritenceTest.a);

    }

}


Output:

this is static method
12
this is object method
this is static child method
this is static method
this is static method
12

Another Example with Same static method name in parent and child class : In this case it will hide parent class static method instead of overriding.

Parent Class:

package com.sample.javase.testing;

public class StaticParentClass {

    static int a = 12;

    static void display() {
        System.out.println("this is static method");
    }
   
    final static void show() {
        System.out.println("this is static final method");
    }

    void display1() {
        System.out.println("this is object method");
    }
    void polyTest(){
        System.out.println("parent poly test");
    }

}
 
 Child Class:
 
package com.sample.javase.testing;

public class StaticInheritenceOverrideTest extends StaticParentClass{

    static int a = 20;
    static void parentDisplay() {
        System.out.println("this is static child method");
    }
   
    void polyTest(){
        System.out.println("child parent poly test");
    }
   
    /*
     * same method as per parentclass
     *
     */
    static void display() {
        System.out.println("this is static child method");
    }
   
    //this method will give compilation error as we put final keyword in parent class for this method.
    /*static void show() {
        System.out.println("this is static final method");
    }*/
    public static void main(String[] args) {
        StaticInheritenceOverrideTest inheritenceTest = new StaticInheritenceOverrideTest();
        inheritenceTest.display();//child class method will call
        System.out.println(inheritenceTest.a);// child class variable will call
        inheritenceTest.display1();
        inheritenceTest.parentDisplay();
        StaticParentClass.display();//parent class method
        StaticInheritenceOverrideTest.display();//child class method will call
        System.out.println(StaticInheritenceOverrideTest.a);//child class method will call
        StaticParentClass parentClass = new StaticInheritenceOverrideTest();
        parentClass.display1();////parent class method
        parentClass.polyTest();//this will call child class and this is polymorphism
        parentClass.display();////parent class method//not applicble polymorphsim here


    }
}

output :

this is static child method
20
this is object method
this is static child method
this is static method
this is static child method
20
this is object method
child parent poly test
this is static method






No comments:

Post a Comment