Can we overload static methods in java

  • Yes. We can overload static methods in java.
  • Method overriding is not possible but method overloading is possible for static methods.
  • Before that lets see about method overloading in java.

Method overloading:

  •  Defining multiple methods with same name and with different arguments is known as method overloading.
  • Multiple methods with same name and different arguments so compile time itself we can tell which method is going to get executed based on method call.
  • Method overloading also known as compile time polymorphism. 

Static methods - method overloading 

  • Its always possibles static method overloading.
  • Defining multiple static methods with same name and different arguments will possible.
  • By this we can define multiple main methods in our class with different arguments but only default main method will be called by the JVM remaining methods we need to call explicitly.
  • Lets see an example java program which explains static method overloading.

  1. class StaticMethodOverloading{
  2.  
  3. public static void staticMethod(){
  4.  
  5. System.out.println("staticMethod(): Zero arguments");
  6.  
  7.  
  8. public static void staticMethod(int a){
  9.  
  10. System.out.println("staticMethod(int a): one argument");
  11.  
  12.  
  13. public static void staticMethod(String str, int x){
  14.  
  15. System.out.println("staticMethod(String str, int x): two arguments");
  16.  
  17. }
  18.  
  19. public static void main(String []args){
  20.   
  21.   StaticMethodOverloading.staticMethod();
  22.   StaticMethodOverloading.staticMethod(12);
  23.   StaticMethodOverloading.staticMethod("Static method overloading",10);
  24.  
  25. }
  26. }


 Output:

  1. staticMethod(): Zero arguments
  2. staticMethod(int a): one argument
  3. staticMethod(String str, int x): two arguments


Java Program to overload main method in java

  1. class mainMethodOverloading{
  2.  
  3. public static void main(boolean x){
  4.  
  5. System.out.println("main(boolean x) called ");
  6.  
  7.  
  8. public static void main(int x){
  9.  
  10. System.out.println("main(int x) called");
  11.  
  12.  
  13. public static void main(int a, int b){
  14.  
  15. System.out.println("main(int a, int b) called");
  16.  
  17. }
  18.  
  19. public static void main(String []args){
  20.    
  21.  
  22. System.out.println("main(String []args) called ");
  23.  
  24.   mainMethodOverloading.main(true);
  25.   mainMethodOverloading.main(10);
  26.  mainMethodOverloading.main(37,46);
  27.  
  28.  
  29. }
  30. }




 Output:

  1. main(String []args) called
  2. main(boolean x) called
  3. main(int x) called
  4. main(int a, int b) called

Can we override static methods in java

  • Exact answer is NO. We can not override static methods.
  • Before discussing this topic lets see what is static in java. 

Static methods in java:

  •  Static means class level if we declare any data or method as static then those data(variables) and methods belongs to that class at class level.
  • Static variables and static methods belongs to class level.
  • Static methods are not the part of objects state . Belongs to class
  • Without using object we can call these static methods.
 here the detailed explanation on static methods

Instance methods in java:


  • Instance methods will be called at run time.
  • Instance variables and instance methods are object level. variables values may change from one object to another where as static variable values are class level so if we access by using any object and changes that values will effect to all objects means its class level variable.
  •  Lets see about overriding in java.

Method overriding in java:


  •  Defining the super class method in sub class with same signature.
  • Even though in inheritance all properties of supers class can access in sub class we have an option of overriding super class methods in sub class known as method overriding.
  • So by this every-time sub-most object method will be called.

Instance methods - Method Overriding

  
  1. class SuperClassDemo{
  2.  
  3. public void instanceMethod(){
  4.  
  5. System.out.println("SuperClassDemo instanceMethodcalled");
  6.  
  7. }
  8.  
  9. }

  1. class SubClassDemo extends SuperClassDemo{
  2.  
  3. public void instanceMethod(){
  4.  
  5. System.out.println("SubClassDemo instanceMethod called");
  6.  
  7. }
  8. public static void main(String []args){
  9.   
  10.   SuperClassDemo superObj= new SuperClassDemo();
  11.   SuperClassDemo  superobj1= new  SubClassDem(); 
  12.   SubClassDemo subObj= new  SubClassDem(); 
  13.   // here no need to create object to call a static method please note that.

  14.   superObj.instanceMethod();
  15.   superObj1.instanceMethod();
  16.   subObj.instanceMethod();
  17.  
  18. }
  19. }

Output

  1. SuperClassDemo instanceMethodcalled
  2. SubClassDemo instanceMethodcalled
  3. SubClassDemo instanceMethodcalled

 Static methods - method overriding:

  • Lest an example program on static methods in method overriding concept and then discus about this clearly.
  1. class SuperClassDemo{
  2.  
  3. public static void staticMethod(){
  4.  
  5. System.out.println("SuperClassDemo staticMethod called");
  6.  
  7. }
  8.  
  9. }

  1. class SubClassDemo extends SuperClassDemo{
  2.  
  3. public static void staticMethod(){
  4.  
  5. System.out.println("SubClassDemo staticMethod called");
  6.  
  7. }
  8. public static void main(String []args){
  9.   
  10.   SuperClassDemo superObj= new SuperClassDemo();
  11.   SuperClassDemo  superobj1= new  SubClassDem(); 
  12.   SubClassDemo subObj= new  SubClassDem(); 
  13.   // here no need to create object to call a static method please note that.

  14.   superObj.staticMethod();
  15.   superObj1.staticMethod();
  16.   subObj.staticMethod();
  17.  
  18. }
  19. }



Output

  1. SuperClassDemo staticMethod called
  2. SuperClassDemo staticMethod called
  3. SubClassDemo staticMethod called

 Method Overriding - Method Hiding:

  • Method overriding : in method overriding we will define super class method in sub class with same signature and these methods will be called at run time based on the object.
  • Method Hiding: In method hiding even though super class methods are accesible in sub class they are not the part of sub class so the methods and these methods will be called based on the class.
  • In method Hiding if we define same static method in super class and sub class they are not same they are unique and distinct from the other.



Return type / return statement in java example

Return type in java: return statement in java

  • Basically return type is used in java methods.
  • Method signature includes this return type.
  • public int show(){ // }
  • we will use methods to do a particular task after completion of task if we want to return something to the calling place these return types will be used.
  • Based on the type of data to be returned will mention it as int , char , float double etc as return type in method signature and return statement should be the last statement of the method body.
  • In Java, the return statement is used to exit a method and return a value to the calling method. The value returned can be of any data type that is specified in the method's return type. 
  • For example, if a method has a return type of int, it can return an integer value. Here's an example of a simple method that uses the return statement to return an int value:


public int add(int a, int b) {
    int sum = a + b;
    return sum;
}

  • In this example, the add method takes two int parameters, a and b, and returns the sum of the two values. The method uses the return statement to return the value of the sum variable back to the calling method.
  • You can also use return statement without returning any value, in this case method should have void return type.


public void printHelloWorld() {
    System.out.println("Hello, World!");
    return;
}

  • In this case the method printHelloWorld doesn't return any value and the calling method doesn't need to assign the return to any variable, just invoking the method will do the job.
  • It is also worth noting that when a return statement is executed, it immediately exits the current method, regardless of where it is in the method's execution flow. So, any code after a return statement will not be executed.

Type of declaration of methods based on return type and arguments:

1.Method with out return type  and without arguments: return statement in java


  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public void add(){
  5.  
  6. int a=40;
  7. int b=50;
  8. int c=a+b;
  9. System.out.println(c);
  10.  
  11. }

  12. public static void main(String args[]) // ->method prototype.
  13.  
  14. sample obj= new sample();
  15. obj.add();
  16.  
  17.  
  18.  }
  19. }

2.Method with out return type and with arguments.

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public void add(int a, int b){
  5.  
  6. int c=a+b;
  7. System.out.println(c);
  8.  
  9. }

  10. public static void main(String args[]) // ->method prototype.
  11.  
  12. sample obj= new sample();
  13. obj.add(13,24);
  14.  
  15.  
  16.  }
  17. }

3.Method with return type  and without arguments.


  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public int add(){
  5. int a=40;
  6. int b=50;
  7. int c=a+b;
  8. return c;
  9. }

  10. public static void main(String args[]) // ->method prototype.
  11.  
  12. sample obj= new sample();
  13. int x=obj.add(); 
  14. System.out.println(x);
  15.  
  16.  }
  17. }

4.Method with return type and with arguments.

 

  1. package com.instanceofjava;
  2. class sample{
  3.  
  4. public int add(int a, int b){
  5.  
  6. int c=a+b;
  7. return c;
  8. }

  9. public static void main(String args[]) // ->method prototype.
  10.  
  11. sample obj= new sample();
  12.  
  13. int x=obj.add(1,2);
  14. System.out.println(x);
  15.  
  16.  }
  17. }


If Else Statment in Java

If statement :
  • if statement in java is decision making statement in java.
  • if statement will have a condition and if that condition is true then the corresponding block will be executed.
  • if(condition){ //  }
If condition syntax:



  1. if(condition){
  2. //  statements
  3. }
 Sample program on if statement:


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifExample { 
  5.  
  6. public static void main(String[] args) {
  7.  
  8. if(true){
  9.  
  10.  System.out.println("if condition executed");
  11.  
  12. }
  13.  
  14. }

Output:
  1. if condition executed

Java program to compare two numbers in java


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class simpleIfExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.   int x= 37;
  9.   int y= 37;
  10.                
  11. if(x==y)
  12.     System.out.println(x+ " is equal to " + y);
  13. }
  14.  
  15. if(x>y){
  16.     System.out.println(x+ " is greater than " + y);
  17. }
  18.  
  19. if(x<y){
  20.     System.out.println(x+ " is less than " + y);
  21. }
  22. }

Output:
  1. 37 is equal to 37



 Else Statement:

  •  if(condition){ //} else { // }

Java program to compare two numbers in java using if else


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.   int x= 37;
  9.   int y= 37;
  10.                
  11. if(x==y){
  12.     System.out.println(x+ " is equal to " + y);
  13. }else if(x>y){
  14.     System.out.println(x+ " is greater than " + y);
  15. }else{
  16.     System.out.println(x+ " is less than " + y);
  17. }
  18. }



Output:
  1. 37 is equal to 37


Java program to find leap year or not in java using if else


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseExample { 
  5.   
  6. public static void main(String[] args) {
  7.  
  8.     int year = 2015;                      
  9.   //if year is divisible by 4, it is a leap year

  10.  if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
  11.  System.out.println("Year " + year + " is a leap year");
  12.  else
  13.       System.out.println( year + " is not a leap year");
  14.   
  15.  
  16. }

Output:
  1. 2015 is not a leap year


Java program to find even or odd using if else statment


  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class ifElseEvenOddExample { 
  5.  
  6. public static void main(String[] args) {
  7.   
  8. int a=10;
  9. if((a%2)==0){
  10.  
  11.  System.out.println(a+"is even number");
  12.  
  13. }else{ 

  14.     System.out.println(a+"is odd number");
  15.  
  16. }
  17.  
  18. }

Output:
  1. 10 is even number

Top 25 Java Basic Interview Questions for Freshers

Imp Note: Openings for 2024 & 2025 pass outs (MNC) in india. Send your profile to Instanceofjava@gmail.com subject should be: yourName_fresher_resume

Java Freshers Interview Questions



1.What is the most Important feature of java?

  • Java is platform independent language.

2.What do you mean by Platform Independence?

  • Platform Independence  you can run and compile program in one platform and can execute in any other platform.


3.What is JVM(Java Virtual Machine)?
  • JVM is Java Virtual Machine which is a run time Environment for the compiled java class.

4.What is JIT Compiler?

  • Just-In-Time(JIT) compiler is  used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time,

5.Are JVM's Platform Independent?

  •  JVM'S are not platform Independent. JVM'S are platform Specific.

6.What if main method declare as private?

  • No,It will compile fine but in run time it will error like main method should be in public.cannot find main method.

7.What is platform?

  • A platform is basically the hardware or software environment in which a program runs.
  • There are two types of platforms software and hardware.
  • Java provides software-based platform.

8.What all memory areas are allocated by JVM?

  • Heap, Stack, Program Counter Register and Native Method Stack

9.What is the base class of all classes?

  • java.lang.Object

10.What is javac ?

  • It produces the java byte code from *.java file.
  • It is the intermediate representation of your source code that contains instructions.

11.Can we mark constructors final?

  • No, Constructor cannot be declared final.

12.What are two different ways to call garbage collector?

  • System.gc() OR Runtime.getRuntime().gc().

13.Can we override a static method?

  • No, we cannot override a static method. Static means class level.

14.Use of finalize() method in java?

  •  finalize() method is used to free the allocated resource.

15.Is it possible to overload main() method of a class?

  • Yes, we can overload main() method as well. 
  • But every time public static main(String[] args) will be called automatically by JVM.
  • Other methods need to call explicitly.

16. Does Java support operator overloading?

  • Operator overloading is not supported in Java.

17.Can I declare a data type inside loop in java?

  • Any Data type declaration should not be inside the loop. Its possible but not recommended.


18.List two java ID Es?

  • 1.Eclipse, 2.Net beans and 3.IntelliJ

19.Can we inherit the constructors?

  • No, we cannot inherit constructors. 
  • We can call super class constructors from subclass constructor by using super() call.

20.Can this keyword be assigned null value?
  • No, this keyword cannot have null values assigned to it.
21.Basic java interview programs for freshers 
22.Java Quiz for freshers

23. Top 25 java interview Questions for freshers  

24.Top 15 oops concepts interview questions and answers

25. Top 40 Java interview Programs asked in interview for freshers

You Might Like:



Java programming interview questions
  1. Print prime numbers? 
  2. What happens if we place return statement in try catch blocks 
  3. Write a java program to convert binary to decimal 
  4. Java Program to convert Decimal to Binary
  5. Java program to restrict a class from creating not more than three objects
  6. Java basic interview programs on this keyword 
  7. Interfaces allows constructors? 
  8. Can we create static constructor in java 
  9. simple java interview questions on Super keyword
  10. Java interview questions on final keyword
  11. Can we create private constructor in java
  12. Java Program Find Second highest number in an integer array 
  13. Java interview programming questions on interfaces 
  14. Top 15 abstract class interview questions  
  15. Java interview Questions on main() method  
  16. Top 20 collection framework interview Questions
  17. Java Interview Program to find smallest and second smallest number in an array 
  18. Java Coding Interview programming Questions : Java Test on HashMap  
  19. Explain java data types with example programs 
  20. Constructor chaining in java with example programs 
  21. Swap two numbers without using third variable in java 
  22. Find sum of digits in java 
  23. How to create immutable class in java 
  24. AtomicInteger in java 
  25. Check Even or Odd without using modulus and division  
  26. String Reverse Without using String API 
  27. Find Biggest substring in between specified character
  28. Check string is palindrome or not?
  29. Reverse a number in java?
  30. Fibonacci series with Recursive?
  31. Fibonacci series without using Recursive?
  32. Sort the String using string API?
  33. Sort the String without using String API?
  34. what is the difference between method overloading and method overriding?
  35. How to find largest element in an array with index and value ?
  36. Sort integer array using bubble sort in java?
  37. Object Cloning in java example?
  38. Method Overriding in java?
  39. Program for create Singleton class?
  40. Print numbers in pyramid shape?
  41. Check armstrong number or not?
  42. Producer Consumer Problem?
  43. Remove duplicate elements from an array
  44. Convert Byte Array to String
  45. Print 1 to 10 without using loops
  46. Add 2 Matrices
  47. Multiply 2 Matrices
  48. How to Add elements to hash map and Display
  49. Sort ArrayList in descending order
  50. Sort Object Using Comparator
  51. Count Number of Occurrences of character in a String
  52. Can we Overload static methods in java
  53. Can we Override static methods in java 
  54. Can we call super class static methods from sub class 
  55. Explain return type in java 
  56. Can we call Sub class methods using super class object? 
  57. Can we Override private methods ? 
  58. Basic Programming Questions to Practice : Test your Skill
  59. Java programming interview questions on collections

Long Wrapper Class

Long Wrapper Class:

  • Long Class is presented in java.lang package.
  • java.lang.Long class is used to represent primitive Long Value to Long object.

Long Class :

Float Class Definition

  1. public final class Long
  2. extends Number 
  3. implements Comparable<Long>



Constructors:

1.Long(long value):
  • The Constructor Long(long value) represents the specified long argument.

2.Long(String s):
  • The Constructor Long (String s) represents the long value indicated by the String parameter.

Methods:

1.public static int bitCount(long i); Program:

  1. package com.instanceofjava; 
  2. import java.lang.*; 
  3.  
  4. public class LongExample { 
  5.  
  6. public static void main(String[] args) {
  7.  
  8.   long l = 4561; 

  9.  System.out.println("Number = " + l);
  10.  System.out.println("Binary = " + Long.toBinaryString(l));
  11.  System.out.println("Number of one bits = " + Long.bitCount(l));
  12.  
  13. }
  14. }

Output:
  1. Number = 4561 
  2. Binary = 1000111010001 
  3. Number of one bits = 6

2.public byte byteValue():

This method returns value of long as byte.

Program:

 

  1. package com.instanceofjava;
  2.  import java.lang.*;

  3. public class LongExample {

  4. public static void main(String[] args) {

  5.  Long obj = new Long(30);

  6.  byte b = obj.byteValue();
  7.  System.out.println("Value of b = " + b);
  8.  
  9.  }
  10. }


Output:
  1. Value of b = 30

3.public int compareTo(Long anotherLong):

Program:

  1. package com.instanceofjava;
  2. import java.lang.*;

  3. public class LongExample {

  4. public static void main(String[] args) {
  5.  
  6.    Long a = new Long(63255);
  7.   Long b = new Long(71678);
  8.  
  9.    int result=  a.compareTo(b);
  10.    ifresult> 0) {
  11.    System.out.println("a is greater than b");
  12.    }
  13.    else ifresult< 0) {
  14.    System.out.println("a is less than b");
  15.    }
  16.    else {
  17.    System.out.println("a is equal to b");
  18.    }
  19.  
  20.    }
  21. }




Output:
  1. a is less than b

4.public static Long decode(String nm) throws NumberFormatException:

Program:


  1. package com.instanceofjava;
  2. import java.lang.*;

  3. public class LongExample{

  4.  public static void main(String[] args) {

  5.    Long l = new Long(10);
  6.    String str = "57820";
  7.    System.out.println("Number = " + l.decode(str));
  8.  
  9.   }
  10. }

Output:
  1. 57820




Select Menu