Top 20 Basic Java Interview Programs on Increment Decrement operators

  • In most of the interviews questions on increment and decrement operators will come in face to face or in written test.
  • Every Java Interview written test will have compulsory one question on increment and decrements operators.
  • Lets see some of the frequently asking java interview  programming questions on increment and decrement operators.
  • I this pre increment and post increment , pre decrement and post decrement topics will cover in below programs.
  • Normal programs: use of increment and decrement in normal scenario is same.
  • They differ in while we use them in expressions.
  • To avoid confusions for basic java learners we provided individual programs.

1.Java Interview program on pre increment operator.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a, b;
  7.      a=10;
  8.      b=++a;
  9.     System.out.println(b);
  10.  
  11. }
  12. }
Output:
  1. 11

2.Java Interview program on post increment operator.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a, b;
  7.      a=10;
  8.      b=a++;
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 11

3.Java Interview program on pre increment operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= ++a + 1;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 22

4.Java Interview program on post increment operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= a++ + 1;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 21

5.Java Interview program on pre increment operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= ++a + ++a;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 43

6.Java Interview program on post increment operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= a++ + a++;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 41

7.Java Interview program on pre and post increment operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= a++ + ++a;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 42




8.Java Interview program on pre decrement


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a, b;
  7.      a=10;
  8.      b=--a;
  9.     System.out.println(b);
  10.  
  11. }
  12. }
Output:
  1. 9

9.Java Interview program on post decrement .


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a, b;
  7.      a=30;
  8.      b=a--;
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 29

10.Java Interview program on pre decrement in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= --a - 1;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 18

11.Java Interview program on post decrement in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= a-- - 1;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 19

12.Java Interview program on pre decrement in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= --a - --a;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 1

13.Java Interview program on post decrement operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.     a= a-- - a--;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 1

14.Java Interview program on pre and post decrement operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.    a= a--  - --a;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 2

15.Java Interview program on increment and decrement operator in expression.


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=20;
  7.   a= a--  + ++a;
  8.  
  9.     System.out.println(a);
  10.  
  11. }
  12. }
Output:
  1. 40

16.increment and decrement operators Inside println() method:



  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.    int a=1;
  7.  
  8.     System.out.println(a++);
  9.     System.out.println(a++);
  10.     System.out.println(++a);
  11.  
  12.     System.out.println(a++);
  13.     System.out.println(a++);
  14.  
  15.     System.out.println(a--);
  16.     System.out.println(a--);
  17.  
  18.     System.out.println(--a);
  19.     System.out.println(--a);
  20.     System.out.println(a--);
  21.  
  22. }
  23. }
Output:
  1. 1
  2. 2
  3. 4
  4. 4
  5. 5
  6. 6
  7. 5
  8. 3
  9. 2
  10. 2

17.Java Interview program on increment and decrement operator in println


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.    int i=15;
  7.     System.out.println(i++);
  8.     System.out.println(i--);
  9.  
  10. }
  11. }
Output:
  1. 15
  2. 16

18.Java Interview program on increment and decrement operator in println


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int a=10,b=10;
  7.  
  8.      for(int i=0;i<5;i++){
  9.  
  10.          if(++a>2||++b>2){
  11.             a++;
  12.            }
  13.  
  14.       }
  15.      System.out.println("a= "+a+" b="+b);
  16.  
  17. }
  18. }
Output:
  1. 20
  2. 10

   

19.Java Interview program on increment and decrement operator in loops


  1. package com.instanceofjava;
  2.  
  3. class IncrementDemo{
  4.  
  5. public static void main(String [] args){ 

  6.     int i;
  7.  
  8.     for( i=1;i<4;i++){    
  9.            
  10.          System.out.println(i);
  11.  
  12.       }
  13.       System.out.println("value of i after completion of loop: "+i);
  14.  
  15. }
  16. }
Output:
  1. 1
  2. 2
  3. 3
  4. value of i after completion of loop: 4

20.Java Interview program on increment and decrement




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

  4.  
  5.  static int a = 1111;
  6.  static
  7.  {
  8.         a = a-- - --a;
  9.  }
  10.     
  11. {
  12.         a = a++ + ++a;
  13.  }
  14.  
  15.  public static void main(String[] args)  {
  16.  
  17.        System.out.println(a);
  18.  
  19.     }
  20.  
  21. }

Output:
  1. 2

Top 13 Java Interview Questions on Static keyword

1.what is static in java?

  • Static is a keyword in java.
  • One of the Important keyword in java.
  • Clear understanding of static keyword is required to build projects.
  • We have static variables, static methods , static blocks. 
  • Static means class level.

2.Why we use static keyword in java?

  • Static keyword is mainly used for memory management.
  • Static variables get memory when class loading itself.
  • Static variables can be used to point common property all objects.

3.What is static variable in java?

  • Variables declared with static keyword is known as static variables.
  • Static variables gets memory on class loading.
  • Static variables are class level.
  • If we change any static variable value using a particular object then its value changed for all objects means it is common to every object of that class.
  • static int a,b;


  1. package com.instanceofjavastatic;
  2. class StaticDemo{
  3.  
  4. static int a=40;
  5. static int b=60;
  6.  
  7. }



  • We can not declare local variables as static it leads to compile time error "illegal start of expression".
  • Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.

  1. package com.instanceofjava;
  2. class StaticDemo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  public static void main(String [] args){
  7.  
  8.    //local variables should not be static
  9.  static int a=10;// compile time error: illegal start of expression
  10. }
  11. }

 Read more : Explain about static variables in java?

4. what is static method in java with example

  • Method which is having static in its method definition is known as static method.
  1. static void show(){
  2.  
  3. }
  • JVM will not call these static methods automatically. Develioper needs to call these static methods from main method or static block or variable initialization.
  • Only Main method will b called by JVM automatically.
  • We can call these static methods by using class name itself no need to create object.

 5.what is static block in java?

  •  Static blocks are the blocks which will have braces and with static keyword.
  • These static blocks will be called when JVM loads the class.
  • Static blocks are the blocks with static keyword.
  • Static blocks wont have any name in its prototype.
  • Static blocks are class level.
  • Static block will be executed only once.
  • No return statements.
  • No arguments.
  • No this or super keywords supported.
  •  
    1. static{ 
    2.  
    3.  }

 Read more @ Static blocks in java with example programs

 6.What is the need of static block?

  • Static blocks will be executed at the time of class loading.
  • So if you want any logic that needs to be executed at the time of class loading that logic need to place inside the static block so that it will be executed at the time of class loading.

7.Why main method is static in java?

  • To execute main method without creating object then the main method should be static so that JVM will call main method by using class name itself.

8.Can we overload static methods in java?

  • Yes we can overload static methods in java.

9.Can we override static methods in java?

  • NO we can not override static methods in java.

10.Can we write static public void main(String [] args)?


  • Yes we can define main method like static public void main(String[] args){}
  •  Order of modifiers we can change.

  1. package instanceofjava;
  2. public MainDemo{
  3. static public void main(String [] args){
  4.  
  5. }
  6. }
11.Can we call super class static methods from sub class?





Java programming interview questions and answers

  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. Super keyword interview questions java 
  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

Essential Classes of Java

  • Lets discuss about what are the common functionalities everyone needs when developing a application and what are the classes we require mostly to rectify common problems.

Exceptions:

  •  Exception classes are very important in every java project,
  • Finding the places where the chances of occurring exception and placing in try and in catch block assigning to corresponding exception class or super class of all exceptions Exception.
  1. Checked Exceptions
  2. Unchecked Exceptions
  3. Errors

Basic IO:

  •  Basic input and output operations covered by java platform classes focuses on IO streams and serialization.
I/O Streams:
  1.     Byte Streams are provided to handle I/O of raw binary data.
  2.     Character Streams handles I/O of character data
  3.     Buffered Streams optimize input and output by reducing the number of calls to the native API.
  4.     Scanning and Formatting allows a program to read and write the formatted text.
  5.     I/O from the Command Line describes the Standard Streams and the Console object.
  6.     Data Streams will handle the binary I/O Strings and primitive data types
  7.     Object Streams provided to handle binary I/O of all objects.

 Concurrency:

  • Will Explains how to build applications which perform multiple tasks simultaneously.
  • Java Platform having full support to concurrent programming 
  • In JDK 5 Java introduces high level concurrent programming APIs.
  • Like java.util.concurrent packages. 

  The Platform Environment 

  • To examine and configure java environment java provides classes and libraries.
  • JVM and Java class libraries
  • Configuration Utilities describes APIs used to access configuration data supplied when the application is deployed, or by the application's user.
  • System Utilities describes miscellaneous APIs defined in the System and Runtime classes.
  • PATH and CLASSPATH describes environment variables used to configure JDK development tools and other applications.
  • Here the List of  commonly used java classes

  1. java.lang.Exception
  2. java.lang.System
  3. java.lang.String
  4. java.util.ArrayList
  5. java.util.HashMap
  6. java.lang.Object
  7. java.lang.Thread
  8. java.lang.Class
  9. java.util.Date
  10. java.util.Iterator



Static class in java

  • Yes we can create a class as static. But class should be inner class or nested class.
  • We know how to create static methods, static variables and static blocks.
  • As Java Supports defining a class within a class we can create a static inner class inside a class.
  • This inner static class inside a class can access static members of outer class even its private.

Java Program to create static inner class:


  1. package instanceofjavaTutorial;
  2.  
  3. public class Outer{ 
  4.  
  5.   static class inner{
  6.  
  7. public void print(){
  8.  
  9. System.out.println("static inner class method called");
  10.  
  11. }
  12.  
  13. }
  14.  
  15. public static void main(String args[]){
  16.  
  17. Outer.inner in= new Outer.innner();
  18.  
  19. in.print();
  20.  
  21. }

Output:

  1. static inner class method called



Static inner class accessing outer class static variable:

  1. package instanceofjavaTutorial;
  2.  
  3. public class Outer{ 
  4.  static int a=10;
  5.   static class inner{
  6.  
  7. public void print(){
  8.  
  9. System.out.println(a);
  10.  
  11. }
  12.  
  13. }
  14.  
  15. public static void main(String args[]){
  16.  
  17. Outer.inner in= new Outer.innner();
  18.  
  19. in.print();
  20.  
  21. }

Output:

  1. 10

  • In java there are two types of nested classes one is static and another one is non static nested classes.
  • We saw static classes in java lets see what will be there in non static nested classes.
Non-static nested class(inner class)
  1. Member inner class
  2. Anonymous inner class
  3. Local inner class

  • A class is defined within a class and outside of methods of that class known as member inner class.
  • Anonymous inner class is a inner class which does not have a name and whose instance is created at the time creating class itself.
  • A class which is defined inside a method of another class known as local inner class

Click here for more information about inner classes

Java Versions, Features and History


Java Versions, Features and History

  • Released on 23 January 1996, JDK 1.0 version.
  • Released on 19 February 1997 JDK 1.1 version.
    New features in JDK 1.1
    1. JDBC (Java Database Connectivity)
    2. Inner Classes
    3. Java Beans
    4. RMI (Remote Method Invocation)
    5. Reflection (introspection only)
  • Released on 8 December 1998 J2SE 1.2 version.
    New features in J2SE 1.2
    1. Collections framework.
    2. Java String memory map for constants.
    3. Just In Time (JIT) compiler.
    4. Jar Signer for signing Java ARchive (JAR) files.
    5. Policy Tool for granting access to system resources.
    6. Java Foundation Classes (JFC) which consists of Swing 1.0, Drag and Drop, and Java 2D class libraries.
    7. Java Plug-in
    8. Scrollable result sets, BLOB, CLOB, batch update, user-defined types in JDBC.
    9. Audio support in Applets.
  • Released on 8 May 2000 J2SE 1.3 version.
    New features in J2SE 1.3
    1. Java Sound
    2. Jar Indexing
    3. A huge list of enhancements in almost all the java area.
  • Released on 6 February 2002 J2SE 1.4 version.
    New features in J2SE 1.4
    1. XML Processing
    2. Java Print Service
    3. Logging API
    4. Java Web Start
    5. JDBC 3.0 API
    6. Assertions
    7. Preferences API
    8. Chained Exception
    9. IPv6 Support
    10. Regular Expressions
    11. Image I/O API
  • Released on 30 September 2004 J2SE 1.5 version.
    New features in J2SE 1.5
    1. Generics
    2. Enhanced for Loop
    3. Autoboxing/Unboxing
    4. Enum
    5. Varargs
    6. Static Import
    7. Metadata (Annotations)
    8. Instrumentation
  • Released on 11 December 2006 J2SE 1.6 version.
    New features in J2SE 1.6
    1. Scripting Language Support
    2. JDBC 4.0 API
    3. Java Compiler API
    4. Pluggable Annotations
    5. Native PKI, Java GSS, Kerberos and LDAP support.
    6. Integrated Web Services.
    7. Lot more enhancements.
  • Released on 28 July 2011 J2SE 1.7 version.
    New features in J2SE 1.7
    1. Strings in switch Statement
    2. Type Inference for Generic Instance Creation
    3. Multiple Exception Handling
    4. Support for Dynamic Languages
    5. Try with Resources
    6. Java nio Package
    7. Binary Literals, underscore in literals
    8. Diamond Syntax
    9. Automatic null Handling


  • Released on 18th march 2014 JDK 1.8 version.
    New features in JDK 1.8
           

  1. Default and Static methods in Interface
  2. Lambda Expressions
  3. Optional
  4. Streams
  5. Method References
  6. Data Time API
  7. Nashorn Javascript Engine
  8. Parallel Arrays

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.



Select Menu