How to Iterate ArrayList using Java ListIterator Example

1.Basic Java example program to iterate arraylist elements using list iterator
  1. package com.javaIteratearraylistiterator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. public class IterateListIteratorArrayList{
  8.  
  9. public static void main(String[] args) {
  10.   
  11. //create an ArrayList object
  12. ArrayList<String> arrayList = new ArrayList();
  13.        
  14. //Add elements to Arraylist
  15.  
  16. arrayList.add("A");
  17. arrayList.add("B");
  18. arrayList.add("C");
  19. arrayList.add("D");
  20. arrayList.add("F");
  21. arrayList.add("F");
  22. arrayList.add("G");
  23. arrayList.add("H");
  24. arrayList.add("I");
  25.     
  26.         
  27.  /*
  28. Get a ListIterator object for ArrayList using
  29. istIterator() method.
  30. */
  31.  
  32. ListIterator itr = arrayList.listIterator();
  33.        
  34. /*
  35. Use hasNext() and next() methods of ListIterator to iterate through
  36. the elements in forward direction.
  37. */
  38.  
  39. System.out.println("Iterating through ArrayList elements in forward  direction...");
  40.  
  41. while(itr.hasNext())
  42. System.out.println(itr.next());
  43.      
  44. /*Use hasPrevious() and previous() methods of ListIterator to iterate through
  45. the elements in backward direction.*/
  46.  
  47. System.out.println("Iterating through ArrayList elements in backward   direction...");
  48.         
  49.  
  50. while(itr.hasPrevious())
  51. System.out.println(itr.previous()); 
  52.   
  53. }
  54.  
  55. }
     



Output:

  1. Iterating through ArrayList elements in forward  direction...
  2. A
  3. B
  4. C
  5. D
  6. F
  7. F
  8. G
  9. H
  10. I
  11. Iterating through ArrayList elements in backward   direction...
  12. I
  13. H
  14. G
  15. F
  16. F
  17. D
  18. C
  19. B
  20. A

Top 10 Java interview questions and programs on this keyword

1.What is this key word in java?

  • "this"is a predefined instance variable to hold current object reference

2.What are the uses of this keyword in constructor?



1.this must be used to access instance variable if both instance and local variable names are same.

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.       a=a;
  9.       b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 0
  2. 0


using this keyword:

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.       this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 1
  2. 2


  • We can use this keyword in constructor overloading. 
  • To call one constructor from another we need this(); and this(); call should be first statement of the constructor.

2.Used to invoke current class constructor:

 

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6. ThisDemo(){
  7. System.out.println("Default constructor called");
  8. }

  9.  ThisDemo(int a, int b){
  10.        this();
  11.         this.a=a;
  12.        this.b=b;
  13.  }
  14.  
  15.     public static void main(String[] args){
  16.  
  17.         ThisDemo obj= new ThisDemo(1,2);
  18.         
  19.         System.out.println(obj.a);
  20.         System.out.println(obj.b);
  21. }
  22. }

Output:

 

  1. Default constructor called
  2. 1
  3. 2


3. Can we call methods using this keyword?

  • Yes we can use this keyword to call current class non static methods .

  1. package com.instanceofjava;
  2.  
  3. public class Test{
  4.     int a, b;
  5.  

  6.  Test(int a, int b){
  7.       
  8.        this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12. void show(){
  13.  
  14. System.out.println("Show() method called");
  15.    
  16. }
  17.  
  18. void print(){
  19.  
  20.     this.show();
  21.     System.out.println(a);
  22.     System.out.println(b);
  23.  
  24.  }
  25.     public static void main(String[] args){
  26.  
  27.         Test obj= new Test(1,2);
  28.         
  29.       
  30.         obj.print()
  31. }
  32. }

Output:

 

  1. Show() method called
  2. 1
  3. 2

3. Can we call method on this keyword from constructor?

  • Yes we can call non static methods from constructor using this keyword.

this keyword in java interview questions for freshers




4.Is it possible to assign reference to this ?

  • No we can not assign any value to "this" because its always points to current object and it is a final reference in java.
  • If we try to change or assign value to this compile time error will come.
  • The left-hand side of an assignment must be a variable
this keyword in java with example program
5.Can we return this from a method?

  • Yes We can return this as current class object. 

  1. public class B{

  2.    int a;
  3.     
  4.  public int getA() {
  5.         return a;
  6.  }
  7.  
  8. public void setA(int a) {
  9.         this.a = a;
  10. }
  11.  
  12. B show(){
  13.     return this;
  14. }
  15.  
  16. public static void main(String[] args) {
  17.        
  18.  B obj = new B();
  19.  
  20.   obj.setA(10);
  21.  
  22.  System.out.println(obj.getA());
  23.  B obj2= obj.show();
  24.  System.out.println(obj2.getA());
  25.  
  26. }

  27. }

Output:

  1. 10
  2. 10

6.Can we pass this as parameter of method?

  • Yes we can pass this as parameter in a method

7. Can we use this to refer static members?

  •  Yes its possible to access static variable of a class using this but its discouraged and as per best practices this should be used on non static reference.

8.Is it possible to use this in static blocks?

  •  No its not possible to use this keyword in static block.

use this keyword in static block


9.Can we use this in static methods? 

  • No we can not use this in static methods. if we try to use compile time error will come:Cannot use this in a static context

10.What are all the differences between this and super keyword?

  • This refers to current class object where as super refers to super class object
  • Using this we can access all non static methods and variables. Using super we can access super class variable and methods from sub class.
  • Using this(); call we can call other constructor in same class. Using super we can call super class constructor from sub class constructor.
11. write a java program on constructor overloading or constructor chaining  using this and super keywords

Top 10 Java interview questions on final keyword

1. What is the use of final keyword in java?   


  • By using final keyword we can make
  • Final class
  • Final method
  • Final variables
  • If we declare any class as final we can not extend that class
  • If we declare any method as final it can not be overridden in sub class
  • If we declare any variable as final its value unchangeable once assigned.

2. What is the main difference between abstract method and final method?

  • Abstract methods must be overridden in sub class where as final methods can not be overridden in sub class
3. What is the actual use of final class in java?

  • If a class needs some security and it should not participate in inheritance in this scenario we need to use final class.
  • We can not extend final class.
4. What will happen if we try to extend final class in java?
  • Compile time error will come.
  1. package com.finalkeywordintweviewprograms;
  2.  public final  Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10. }


  1. package com.finalkeywordintweviewprograms;
  2.  public Class Sample  extends SuperDemo{  //The type Sample cannot subclass the final class
  3. SuperDemo

  4. }

5.Can we declare interface as final?

  • No We can not declare interface as final because interface should be implemented by some class so its not possible to declare interface as final.
final keyword interface in java




6. Is it possible to declare final variables without initialization?

  • No. Its not possible to declare a final variable without initial value assigned.
  • While declaring itself we need to initialize some value and that value can not be change at any time.

final variable in java
  1. package com.finalkeywordintweviewprograms;
  2. public final  Class Sample{ 
  3.  
  4. final int x=12,y=13;
  5.  
  6. public void Method() {

  7. x=25;// compile time error:The final field Super.x cannot be assigned
  8. y=33;// compile time error: The final field Super.y cannot be assigned
  9.  
  10. }
  11.  
  12. }

7. Can we declare constructor as final?
  • No . Constructors can not be final.

8.What will happen if we try to override final methods in sub classes?

  • Compile time error will come :Cannot override the final method from Super class
9.Can we create object for final class?
  • Yes we can create object for final class. 
10.What is the most common predefined final class object you used in your code? 
  • String (for example)

Super keyword interview questions java

Super Keyword:


  • The functionality of super keyword is only to point the immediate super class object of the current object.
  • super keyword is applicable only in the non static methods and super keyword not applicable in the static methods.
  • super keyword used to access the members of the super class object.
  • super.member;
  • It is used to store super class non static members memory reference through current sub class object for separating super class members from subclass members.
  • We can call super class constructor in sub class using super() call.
  • We can access super class methods and variables in sub class using super.variable_name, super.method();

Uses of super : 

 
1. By using super keyword we can access super class variables in sub class.

  • Using super keyword we can access super class variables from sub class.
  • super.variable_name.
  1. package com.superkeywordinjava;
  2.  public Class SuperDemo{ 
  3.  
  4. int a,b;
  5.  
  6. }

  1. package com.superkeywordinjava;
  2. public Class Subdemo extends SuperDemo{ 
  3. int a,b;
  4. void disply(){

  5. System.out.println(a);
  6. System.out.println(b);
  7. super.a=10;
  8. super.b=20;
  9.  
  10. System.out.println(super.a);
  11. System.out.println(super.b);
  12.  
  13. }
  14.  
  15. public static void main (String args[]) {
  16.  Subdemo obj= new Subdemo();
  17.  
  18. obj.a=1;
  19. obj.b=2;
  20.  
  21. obj.disply();
  22.  

  23.  
  24. }
  25. }

Output:

  1. 1
  2. 10
  3. 20

2. By using super keyword we can access super class methods in sub class.
  • Using super keyword we can call super class methods from sub class.
  • super.method();.

  1. package com.instanceofjavaforus;
  2.  public Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10. }

  1. package com.instanceofjavaforus;
  2. public Class Subdemo extends SuperDemo{ 
  3. int a,b;
  4. void disply(){

  5. System.out.println(a);
  6. System.out.println(b);
  7.  
  8. super.a=10;
  9. super.b=20;
  10.  
  11.  super.show();
  12.  
  13. }
  14.  
  15. public static void main (String args[]) {
  16.  
  17.  Subdemo obj= new Subdemo();
  18.  
  19. obj.a=1;
  20. obj.b=2;
  21.  
  22. obj.disply();

  23.  
  24. }
  25. }

Output:

  1. 1
  2. 10
  3. 20
 
3. We can call super class constructor from class constructor:
  •  By using super keyword we can able to call super class constructor from sub class constructor.
  • Using super(); 
  • For  the super(); call must be first statement in sub class constructor.  

  1. package com.superinterviewprograms;
  2. public Class SuperDemo{ 
  3.  
  4. int a,b;
  5.  
  6. SuperDemo(int x, int y){
  7.  a=x;
  8. b=y
  9. System.out.println("Super class constructor called ");
  10.  
  11.  
  12. }



  1. package com.superinterviewprograms;
  2.  
  3. public Class Subdemo extends SuperDemo{ 
  4.  
  5. int a,b;
  6.  
  7. SubDemo(int x, int y){
  8. super(10,20);
  9.  a=x;
  10. b=y
  11. System.out.println("Sub class constructor called ");
  12. }
  13.  
  14. public static void main (String args[]) {
  15.  Subdemo obj= new Subdemo(1,2);

  16.  
  17. }
  18. }

Output:
  1. Super class constructor called
  2. Sub class constructor called 

key points:

  • Super(); call must be first statement inside constructor.
  • By default  in every class constructor JVM adds super(); call in side the constructor as first statement which calls default constructor of super class, if we are not not calling it.
  • If we want to call explicitly we can call at that time default call wont be there.

4.What will happen if  we are calling super() in constructor but our class does not extending any class?

  •  if our class not extending any class Yes still we can use super(); call in our class 
  • Because in java every class will extend Object class by default this will be added by JVM.
  • But make sure we are using only super(); default call we can not place parameterized super call because Object class does not have any parameterized constructor.

  1. package com.superinterviewprograms;
  2.  
  3. public Class Sample{ 
  4.  
  5. Sample(){
  6. super();
  7. System.out.println("Sample class constructor called "); 
  8.  
  9. }
  10.  
  11. public static void main (String args[]) {
  12.  
  13.  Sample obj= new Sample();
  14.  
  15. }
  16. }

Output:

  1. Sample class constructor called


5.What if there is a chain of extended classes and 'super' keyword is used


  1. package com.superinterviewprograms;
  2. public Class A{ 
  3.  
  4. A(){
  5.  System.out.println("A class constructor called ");
  6.  
  7.  
  8. }

  1. package com.superinterviewprograms;
  2. public Class B extends A{ 
  3.  
  4. B(){
  5.  
  6.  System.out.println("B class constructor called ");
  7.  
  8.  
  9.  
  10. }

  1. package com.superinterviewprograms;
  2. public Class C extends B{ 
  3.  
  4. C(){
  5.  System.out.println("C class constructor called ");
  6.  
  7.  
  8. public static void main (String args[]) {
  9.  C obj= new C();

  10.  
  11. }
  12. }

Output:

  1. A class constructor called
  2. B class constructor called
  3. C class constructor called

6. Can we call super class methods from static methods of sub class?

  • No we can not use super in static methods of sub class Because super belongs to object level so we can not use super in static methods. 
  • If we try to use in sub class static methods compile time error will come.

super keyword in java interview example program frehser

Data types in java with example programs

  • The data type is something which gives information about
  • Size of the memory location and range of the data that can be accommodated inside that location.
  • Possible legal operations those can be performed on that location.
  • What type of result come out from an expression when we use these types inside that expression.
  • Whichever the keyword gives these semantics is treated as "data type".
  • The java language provides different categories of data types. 

1.Primitives Data types:

  • The primitive data types are the predefined data types given by the programming language and they are meant for storing a single value.
  • Based on the type and range of data, primitive types are divided into 8 types.

1.Integer category: 

  • This category can be used for storing numbers which can be either positive value  or negative value without decimal points.
  • In this category we have 4 primitive data types whose memory sizes are different. 
  • All the 4 types under this category are used for storing same data. But there ranges are different. The java language is providing 4 types under Integer category. So that the memory is utilized efficiently. 
  1. byte  
  2. short
  3. int
  4. long 
data types in java


byte data type in java:

  • Size 1 byte. i.e 8 bits
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (2^7 -1)
  • Default value is 0
Java example program on byte data type:

  1. package com.instanceofjava;
  2. Class ByteDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  byte x=10;
  7.  byte y=20;
  8.  
  9.   byte z= (byte) (x+y);
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 30 

 short data type in java:

  • Size 2 bytes.
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (2^15-1)
  • Default value is 0

Java example program on short data type:

  1. package com.instanceofjava;
  2. Class ShortDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  short x=10;
  7.  short y=20;
  8.  
  9.   short z= (short) (x+y);
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 30

int data type in java:

  • Size 4 bytes.
  • Minimum value is - 2,147,483,648.(-2^31)
  • Maximum value is 2,147,483,647 (2^31 -1)
  • Default value is 0

Java example program on int data type:

  1. package com.instanceofjava;
  2. Class IntDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  int x=10;
  7.  int y=20;
  8.  
  9.   int z=x+y;
  10.   System.out.println(z);
  11.  
  12. }

  13. }

Output:

  1. 30

long data type in java:
  • Size 8 bytes.
  • Minimum value is -9,223,372,036,854,775,808.(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (2^63 -1)
  • Default value is 0

Java example program on long data type:

  1. package com.instanceofjava;
  2. Class LongDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  long x=109876677777l;
  7.  long y=20666677766l;
  8.  
  9.  long z=x+y;
  10.  
  11.  System.out.println(z);
  12.  
  13. }

  14. }



Output:

  1. 130543355543

2.Floating point category: 

  • This category can be used for storing numbers which can be either +VE or –VE with decimal point. In the floating point category we have two types whose size is different. The two data types are float and double.
  • Both the data types under the floating point category are used for storing same data but there range is different. The java language provide as two data types under floating point category so that memory is utilized efficiently. 
  • Default value of float is 0
  • Default value of double is 0.0 
  1. float
  2. double


float data type in java

float data type in java:

  • Size 4 bytes.
  • Minimum value is 1.4e-45
  • Maximum value is 3.4e38
  • Default value is 0.0f

Java example program on float data type:

  1. package com.instanceofjava;
  2. Class FloatDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  float x=231231.0031f;
  7.  float y= 53423423434.43231f;

  8.  float z= x+y; 

  9.  System.out.println(z);
  10.  
  11. }

  12. }

Output:

  1. 5.3423653E10

double data type in java:
  • Size 8 bytes.
  • Minimum value is 4.9e-324
  • Maximum value is 1.8e308
  • Default value is 0.0d

Java example program on double data type:

  1. package com.instanceofjava;
  2. Class DoubleDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  double x=231231.0031f;
  7.  double y= 53423423434.43231f;

  8.  double z= x+y; 

  9.  System.out.println(z);
  10.  
  11. }

  12. }

Output:

  1. 5.342365466543541E10

3.Character category: 

  • This category can be used for storing a single character. A character can be represented by alphabets, a digit and special symbols. 
  • This category contains only one data type an it is char.
  • Default value of char is "one space"
  1. char

char data type in java:
  • Size 2  bytes.
  • Minimum value is 0
  • Maximum value is 65,535
  • Default value is ' '

Java example program on char data type:

  1. package com.instanceofjava;
  2. Class CharDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  char a='a';
  7.  System.out.println(a);
  8.  
  9. }

  10. }


Output:

  1. a

4.Boolean category: 

  • This category can be used for storing either true or false. Under the Boolean category only one data type an it is Boolean. The size is dependent on JVM to JVM.

boolean data type in java:

  • boolean data type represents one bit of information.
  • There are only two possible values: true and false.
  • This data type is used for simple flags that track true/false conditions.
  • Default value is false. '

Java example program on boolean data type:

  1. package com.instanceofjava;
  2. Class BooleanDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. boolean x = false;
  7. System.out.println(x);
  8.  
  9. }

  10. }

Output:

  1. false

Top 10 interview questions on java interfaces

1.What is an interface in Java.

  • Before Java 8 interfaces are pure abstract classes which allow us to define public static final variables and public abstract methods(by default).
  • In java 8 introduced static methods and default methods in interfaces. 
  • We can develop interfaces by using "interface" keyword.  
  • A class will implements all the methods in an interface.



  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x=12;
  5. void show(); 

  6. }

  1. package com.instanceofjava;
  2. Class A implements JavaInterface {
  3.  
  4. void show(){
  5. // code
  6. }

  7. }



  1. package com.instanceofjava;
  2. interface Java8Interface{
  3.  
  4. int x=12;
  5. void show(); 

  6.   
  7. default void display(){
  8.  
  9. System.out.println("default method of interface");
  10.  
  11. }
  12.  
  13. Static void print(String str){
  14.  
  15. System.out.println("Static method of interface:"+str);
  16.  
  17. }
  18.  
  19.  
  20. }




2.What will happen if we define a concrete method in an interface?
  • By default interface methods are abstract.
  • if we declare any concrete method in an interface compile time error will come.
  • Error:Abstract methods do not specify a body

Interface concrete method in java


3.Can we create non static variables in an interface?

  • No.We can not create non static variables in an interface.
  • If we try to create non static variables compile time error comes.
  • By default members will be treated as public static final variables so it expects some value to be initialized.

  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x, y; // compile time error


  5. }


4.What will happen if we not initialize variables in an interface.
  • Compile time error will come because by default members will be treated as public static final variables so it expects some value to be initialized.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. int x, y; // compile time error: The blank final field y may not have been initialized


  5. }
5.Can we declare interface members as private or protected?
  • No.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. private int x; // compile time error: Illegal modifier for the interface field Sample.x; only
  5. public, static & final are permitted
  6. protected int a; // compile time error: Illegal modifier for the interface field Sample.a; only
  7. public, static & final are permitted

  8. }


7.When we need to use extends and implements?
  • A class will implements an interface.
  • A class will extends another class.
  • An interface extends another interface.

6.Can we create object for an interface?

  • NO. We can not create object for interface.
  • We can create a variable fro an interface

  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. void show(); 

  5. }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface {
  3.  
  4. void show(){
  5. // code
  6. }
  7. public static void main(String args[]){
  8.  
  9.  JavaInterface obj= new JavaInterface(); // Error: Cannot instantiate the type JavaInterface
  10.  
  11. }
  12. }

7.Can we declare interface as final?

  • No. Compile time error will come.
  • Error: Illegal modifier for the interface Sample; only public & abstract are permitted
declare interface final in java



8.Can we declare constructor  inside an interface?

  • No. Interfaces does not allow constructors.
  • The variables inside interfaces are static final variables means constants and we can not create object fro interface so there is no need of constructor in interface that is the reason interface doesn't allow us to create constructor.

final interface interview freshers


9.What will happen if we are not implementing all the methods of an interface in class which implements an interface?

  • A class which implements an interface should implement all the methods (abstract) otherwise compiler will throw an error.
  • The type Example must implement the inherited abstract method JavaInterface.show() 
  • If we declare class as abstract no need to implement methods. 
  • No need of overriding default and static methods.
  1. package com.instanceofjava;
  2. interface JavaInterface{
  3.  
  4. void show(); 

  • }

  1. package com.instanceofjava;
  2. interface A implements JavaInterface { // The type Example must implement the inherited
  3. abstract method JavaInterface.show()
  4.  
  5. public static void main(String args[]){
  6.  

  7. }
  8. }

10.How can we access same variables defined in two interfaces implemented by a class?

  • By Using corresponding interface.variable_name we can access variables of corresponding interfaces.
11.If  Same method is defined in two interfaces can we override this method in class implementing these interfaces.
  • Yes implementing the method once is enough in class.
  • A class cannot implement two interfaces that have methods with same name but different return type.

Constructor in interface ?

Can we write constructor inside interface in java?

  • No. Interfaces does not allow constructors.
  • Why interface does not have constructor? The variables inside interfaces are static final variables means constants and we can not create object fro interface so there is no need of constructor in interface that is the reason interface doesn't allow us to create constructor.
  • what happens when a constructor is defined for an interface



What will happens if we try to create constructor inside interfaces in java

  • If we try to create constructor in interface compile time error will come.
  • Error description: Interfaces cannot have constructors.


  1. public interface sample{
  2.  
  3.  int a=10;

  4. sample(){//Interfaces cannot have constructors.
  5.  
  6. }




constructor in interface java





Interfaces in Java 8:

  • Before java 8 interfaces allows only public abstract methods.
  • If we declare any method in interface with default it will be treated as public abstract method.
  • Interface methods doesn't have body. The class which implements interfaces are responsible for implementing unimplemented methods of interface.
  • But in Java 8 static and default methods added.

Default methods:

  • Defaults methods are also  known as defender methods or virtual extension methods
  • Default methods will help us to avoid utility classes.
  • We can define utility methods inside the interface and use it in all classes which is implementing.
  • One of the major reason to introduce this default methods in java 8 is to support lambda expressions in collections API and to enhance.


  1. package com.instanceofjava;
  2. interface Java8InterfaceDemo{
  3.  
  4. abstract void print();
  5.   
  6. default void display(){
  7.  
  8. System.out.println("default method of interface");
  9.  
  10. }
  11.  
  12. }

  1. package com.instanceofjava;
  2. class Sample implements Java8InterfaceDemo{
  3.  
  4. void print(){
  5. System.out.print("overridden method ")
  6.  }
  7. public static void main(String[] args){
  8.   
  9. Sample obj= new Sample();
  10.  
  11. obj.print(); // calling implemented method
  12. obj.display(); // calling inherited method
  13.  
  14. }
  15.  
  16. }

Output:

  1. overridden method
  2. default method of interface
  3. default method of interface

Static methods in Java 8:

  • These static method will act as helper methods.
  • These methods are the parts of interface not belongs to implementation class objects.

  1. package com.interfacesinJava8;
  2. interface StaticInterface{
  3.  
  4. Static void print(String str){
  5.  
  6. System.out.println("Static method of interface:"+str);
  7.  
  8. }
  9. }

  1. package com.instanceofjava;
  2. class Demo implements StaticInterface{
  3.  
  4. public static void main(String[] args){
  5.   
  6.  StaticInterface.print("Java 8")
  7.  
  8. }
  9.  
  10. }

Output:

  1. Static method of interface: Java 8

Select Menu