Can we call super class static method from subclass in java

  • 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. }

  • We can also call static method like this but this is not recommended.

static method in java interview questions




 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

Inheritance interview programming questions : Part 2

1. what is the output of following program:

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

  4. public 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 show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo supobj= new SuperDemo();
  11.  supobj.show(); 
  12.  
  13. }
  14. }






2. what is the output of following program:

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

  4. public 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 show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo supobj= new SuperDemo();
  11.  supobj.show(); 
  12.  
  13.  SubDemo subobj=new SubDemo();
  14.  subobj.show();
  15.  
  16.  }
  17. }





3. what is the output of following program:

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

  4. public 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 show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo supobj= new SubDemo();
  11. supobj.show(); 
  12.  
  13. }
  14. }





4. what is the output of following program:

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

  4. public void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10.  public void superMethod(){
  11.  
  12.         System.out.println("Super class superMethod() called");
  13.  
  14. }
  15.  
  16. }

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

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

  11.  public static void main(String args[]){
  12.  
  13. SuperDemo supobj= new SubDemo();
  14. supobj.superMethod();
  15.  
  16. // supobj.subMethod();  // compile time error: The method subMethod() is undefined for the
  17. type Super

  18.  ((SubDemo)supobj).subMethod();
  19. ((SuperDemo)supobj).show();
  20. }
  21. }







Constructor chaining in java using this and super Keywords

Constructor chaining example program in same class using this keyword.


  1. package instanceofjava;
  2. class ConstructorChaining{
  3. int a,b 
  4. ConstructorChaining(){
  5. this(1,2);
  6.  System.out.println("Default constructor");
  7.  
  8.  
  9. ConstructorChaining(int x , int y){
  10.  
  11. this(1,2,3); 
  12. a=x;
  13. b=y;
  14.  System.out.println("Two argument constructor");
  15.  
  16. }
  17.  
  18. ConstructorChaining(int a , int b,int c){
  19.  System.out.println("Three argument constructor")
  20.  
  21. public static void main(String[] args){
  22.  
  23.  ConstructorChaining obj=new ConstructorChaining();
  24.   System.out.println(obj.a);
  25.   System.out.println(obj.b);
  26.  
  27. }
  28. }


Output:

  1. Three argument constructor
  2. Two argument constructor
  3. Default argument constructor
  4. 1
  5. 2

Constructor chaining example program in same class using this and super keywords.

  1. package instanceofjava;
  2. class SuperClass{
  3.  
  4. SuperClass(){
  5. this(1);
  6.  System.out.println("Super Class no-argument constructor");
  7.  
  8.  
  9. SuperClass(int x ){
  10.  
  11. this(1,"constructor chaining"); 
  12. System.out.println("Super class one -argument constructor(int)");
  13.  
  14. }
  15.  
  16. SuperClass(int x , String str){
  17.  System.out.println("Super class two-argument constructor(int, String)");
  18.  
  19. }




  1. package instanceofjava;
  2. class SubClass extends SuperClass{

  3. SubClass(){
  4. this(1);
  5.  System.out.println("Sub Class no-argument constructor");
  6.  
  7.  
  8. SubClass(int x ){
  9.  
  10. this("Constructor chaining"); 
  11.  System.out.println("Sub class integer argument constructor");
  12.  
  13. }
  14.  
  15. ConstructorChaining(String str){
  16. //here by default super() call will be there so it will call super class constructor
  17.   System.out.println("Sub class String argument constructor");
  18.  
  19. public static void main(String[] args){
  20.  
  21.  SubClass obj=new SubClass();
  22.  
  23.  
  24. }
  25. }

Output:

  1. Super class two-argument constructor(int, String)
  2. Super class one -argument constructor(int)
  3. Super Class no-argument constructor
  4. Sub class String argument constructor
  5. Sub class String argument constructor
  6. Sub class integer argument constructor




Java Coding Interview programming Questions : Java Test on HashMap

1.What will be the Output of this program.


  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.     System.out.println(hm.size()); 
  11.  
  12. for (Integer name: hm.keySet()){
  13.  
  14.    System.out.println(name + " " + hm.get(name)); 
  15.   
  16. }
  17.  
  18. }
  19. }





2.Basics of java programming : Hashmap

  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.      hm.put(1, "ONE");
  11.  
  12.     System.out.println(hm.size()); 
  13.  
  14. for (Integer name: hm.keySet()){
  15.  
  16.    System.out.println(name + " " + hm.get(name)); 
  17.   
  18. }
  19.  
  20. }
  21. }









3.Java programming examples: Hashmap

  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.      hm.put(1, "ONE");
  11.      hm.put(null,null);
  12.  
  13.     System.out.println(hm.size()); 
  14.  
  15. for (Integer name: hm.keySet()){
  16.  
  17.    System.out.println(name + " " + hm.get(name)); 
  18.   
  19. }
  20.  
  21. }
  22. }






4.Java programming for beginners : HashMap

  1. public class HashMapJavaTricky{
  2.  
  3. public static void main(String[] args) {
  4.  
  5.     HashMap<Integer,String> hm= new HashMap<Integer, String>();  

  6.      hm.put(1, "one");
  7.      hm.put(2, "two");
  8.      hm.put(3, "three");
  9.  
  10.      hm.put(1, "ONE");
  11.      hm.put(null,null);
  12.  
  13.      Integer i=null;
  14.      String str="java programming basics Interview questions";
  15.  
  16.      hm.put(i, str);
  17.  
  18.     System.out.println(hm.size()); 
  19.  
  20. for (Integer name: hm.keySet()){
  21.  
  22.    System.out.println(name + " " + hm.get(name)); 
  23.   
  24. }
  25.  
  26. }
  27. }





Find Second smallest number in java without sorting

Java interview Program to find second smallest number in an integer array without sorting the elements.


  1. package com.instanceofjava;
  2. class SecondSmallestNumber{
  3.  
  4. int[] x ={10,11,12,13,14,6,3,-1};
  5.  
  6.         int small=x[0];
  7.  
  8.  for(int i=0;i<x.length;i++)
  9.  {
  10.         if(x[i]<small)
  11.         {
  12.         small=x[i];
  13.         }
  14.  }
  15.  
  16.    int sec_Small=x[0];
  17.  
  18. for(int i=0;i<x.length;i++)
  19.  {
  20.         if(x[i]<sec_Small && x[i]!=small)
  21.         {
  22.         sec_Small=x[i];
  23.         }
  24.   }
  25.  
  26.         System.out.println("Second Smallest Number: "sec_Small);
  27.         }
  28. }



Output:
 
  1. Second Smallest Number:3

Java interview  Program to find second Smallest number in an integer array by sorting the elements.


  1. package com.instanceofjava;
  2. class SecondSmallestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7.  
  8.   Arrays.sort(numbers);
  9.  
  10.   System.out.println("Smallest Number: "+numbers[0]);
  11.   System.out.println("Second Smallest Number: "+numbers[1]);

  12.  }
  13.  
  14. }




Output:
 
  1. Smallest Number: 3
  2. Second Smallest Number: 5



Find second highest number in an integer Array

Java Interview Program to find second highest number in an integer array without sorting the elements.


  1. package com.instanceofjava;
  2. class SecondLargestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7. int highest = 0;
  8.  int second_highest = 0;
  9.  
  10. for(int n:numbers){
  11.  
  12. if(highest < n){
  13.  
  14.       second_highest = highest;
  15.       highest =n;
  16.  
  17.  } else if(second_highest < n){
  18.  
  19.                 second_highest = n;
  20.  
  21. }
  22.  
  23. }
  24.         System.out.println("First Max Number: "+highest);
  25.         System.out.println("Second Max Number: "+second_highest);

  26.  
  27.  }
  28.  
  29. }



Output:
 
  1. First Max Number: 64
  2. Second Max Number: 46

Java Interview Program to find second highest number in an integer array by sorting the elements.


  1. package com.instanceofjava;
  2. class SecondLargestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7.  
  8.   Arrays.sort(numbers);
  9.  
  10.   System.out.println("Largest Number: "+numbers[numbers.length-1]);
  11.   System.out.println("Second Largest Number: "+numbers[numbers.length-2]);
  12.  }
  13.  
  14. }




Output:
 
  1. Largest Number: 64
  2. Second Largest Number: 46



How to find Biggest Substring in between specified character or String

1. Java Program to find biggest substring in between specified character or string

String: i am rajesh kumar ravi

in between: 'a'

 

  1. package com.instaceofjava;
  2.  
  3. public class BiggestSubString{
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String str="i am rajesh kumar ravi";
  8.  
  9.  String longest="";
  10.  int maxlength=0;
  11.  
  12.  String arr[]=str.split("a");
  13.  
  14. for (int i = 1; i < arr.length-1; i++) {
  15.  
  16.    System.out.println(arr[i]); // printing substrings
  17.  
  18.           if(arr[i].length() > maxlength){
  19.                maxlength = arr[i].length();
  20.                 longest = arr[i];
  21.             }   
  22.  
  23.    }      
  24.   
  25. System.out.println("Longest substring is: "+longest);
  26.  
  27. }
  28. }


Output:

  1. m r
  2. jesh kum
  3. r r
  4. Longest substring is: jesh kum


Select Menu