The Artima Developer Community
Sponsored Link

Java Buzz Forum
Constructor in interface ?

0 replies on 1 page.

Welcome Guest
  Sign In

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

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Constructor in interface ? Posted: Feb 25, 2016 4:21 AM
Reply to this message Reply

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

Advertisement
Can we write constructor inside interface in java?

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



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. Java8InterfaceDemo.display(); calling using interface name
  14.  
  15. }
  16.  
  17. }

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

Read: Constructor in interface ?

Topic: The most important thing to know in Cassandra data modeling: The primary key Previous Topic   Next Topic Topic: Java EE 8 MVC: Working with form parameters

Sponsored Links



Google
  Web Artima.com   

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