The Artima Developer Community
Sponsored Link

Java Buzz Forum
Can we call super class static method from subclass in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Can we call super class static method from subclass in java Posted: Feb 15, 2016 11:38 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Can we call super class static method from subclass in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • If you want to call static method of a class we can call directly from another static method or by using its class name we can call static method of that class.
  • let us see a program on how to call a static method of a class

    1. package com.instanceofjava;
    2. public class Sample{

    3. public static void show(){
    4.  
    5.  System.out.println("show() method called");
    6.  
    7. }
    8.  public static void main(String args[]){
    9.  
    10.  show();
    11. Sample.show();
    12.  
    13. }
    14. }


     Output:
    1. show() method called
    2. show() method called

    • Now our question is can we call super class static method from sub class?
    • Yes we can call super class static method inside sub class using super_class_method();
    • We can also call super class static method using Sub_class_name.superclass_staticMethod()


    1. package com.instanceofjava;
    2.  
    3. public class SuperDemo{

    4. public static void show(){
    5.  
    6.   System.out.println("Super class show() method called");
    7.  
    8. }
    9.  
    10. }

    1. package com.instanceofjava;
    2. public class SubDemo extends SuperDemo{

    3. public void print(){
    4.  
    5.  System.out.println("Sub class print() method called");
    6.  
    7. }
    8.  public static void main(String args[]){
    9.  
    10. SuperDemo.show();
    11. SubDemo.show();
    12. }
    13. }

    Output:
    1. Super class show() method called
    2. Super class show() method called

    • If the same static method defined in sub class also then we can not call super class method using sub class name if we call them sub class static method will be executed.

    1. package com.instanceofjava;
    2.  
    3. public class SuperDemo{

    4. public static void show(){
    5.  
    6.   System.out.println("Super class show() method called");
    7.  
    8. }
    9.  
    10. }

    1. package com.instanceofjava;
    2. public class SubDemo extends SuperDemo{

    3. public static void show(){
    4.  
    5.   System.out.println("Sub class show() method called");
    6.  
    7. }
    8.  public static void main(String args[]){
    9.  
    10. SuperDemo.show();
    11. SubDemo.show();
    12. }
    13. }

     Output:
    1. Super class show() method called
    2. Sub class show() method called

    Key points to remember:
    1. Can we Overload static methods in java
    2. Can we Override static methods in java

    Read: Can we call super class static method from subclass in java

    Topic: Constructor chaining in java using this and super Keywords Previous Topic   Next Topic Topic: Building Reactive Systems with JavaFX

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use