Interface Class in Java.

 

What is the interface in java?

Interface in java is one of OOP’s concepts. The interface in java is a mechanism to achieve 100% abstraction.

The interface provides achieve 100% abstraction because it uses only abstract methods (not method body).

An interface can be used only abstract methods (without body), before java 8.

After java 8 we can have default and static methods in an interface. In java 9 we have private methods also.

The interface is declared by using the interface keyword, and all the fields are public, static, and final by default.

 

Why we are using Java interface?.

There are many reasons to use Java interface but there are mainly three reasons.

  • The interface provides 100% abstraction.
  • Interface we can achieve multiple inheritance functionality in java.
  • Interface provides loose coupling.

 

Java Interface program Example

interface printer {
	void print();
}

class Book implements printer {
	public void print() {
		System.out.println("Book Printing");
	}

	public static void main(String args[]) {
		Book obj = new Book();
		obj.print();
	}
}

Output:

Book Printing

 

Java Interface program another Example

interface Shape {
	void draw();
}

class Rectangle implements Shape {
	public void draw() {
		System.out.println("drawing rectangle");
	}
}

class Circle implements Shape {
	public void draw() {
		System.out.println("drawing circle");
	}
}

class Test {
	public static void main(String args[]) {
		Shape d = new Circle();
		d.draw();
		Shape d = new Rectangle();
		d.draw();
	}
}

Output:

drawing circle
drawing rectangle

 

In the above example, we are creating an interface Shape, and it has only one method that is abstract method draw() (without body).

Shape interface implementation is provided by Rectangle and Circle classes. In this scenario, the implementation part is always hidden by the user, who uses the interface.

In this program Interface declaration by one user, and its implementation provided by other users, based on their requirement.

Multiple inheritances not supported by java class, but it is supported by the Java interface.

 

How Multiple inheritances supported by Java interface?

Multiple inheritances mean if a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritances in java.

 

Class multiple interfaces Example.

interface Shape {
	void draw();
}

interface Print {
	void print();
}

class Example implements Shape, Print {
	public void print() {
		System.out.println("Printing");
	}
	public void draw() {
		System.out.println("Drawing");
	}

	public static void main(String args[]) {
		Example obj = new Example();
		obj.print();
	}
}

Output:

Printing
Drawing

In this example, the class Example implements multiple interface Shape, Print.

 

Interface extends multiple interfaces for Example.

interface Print {
	public void printing();
}
interface Draw {
	public void drawing();
}

interface Painter extends Print, Draw {
	public void printing();
}

class Example implements Painter {
	public void printing() {
		System.out.println("printing\n");
	}

	public void drawing() {
		System.out.println("drawing\n");
	}

	public void printingMain() {
		System.out.println("printing main");
	}
}

public class Test {
	public static void main(String[] args) {
		Example ex = new Example();
		ex.printing();
		ex.printing();
		ex.printingMain();
	}
}

Output:

printing
drawing
printing main

 

Multiple inheritances are not supported by class in java, but it is possible by an interface, why?

Multiple inheritances are not supported in the case of class, because of ambiguity but it is supported by interface because we are not getting any ambiguity.

For more details of Multiple inheritance Click.

 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

10 Replies to “Interface Class in Java.”

  1. I am just writing to make you understand what a outstanding experience my wife’s girl obtained using your web site. She noticed so many pieces, which included what it is like to possess an excellent helping nature to let other people with ease learn specific specialized things. You truly surpassed people’s expected results. Thank you for producing these precious, trusted, informative and in addition fun tips about this topic to Lizeth.

  2. My spouse and i got quite glad that Ervin could deal with his investigations through the ideas he got out of the web page. It’s not at all simplistic to just continually be giving freely key points that many other people have been selling. And we also fully grasp we have got the website owner to be grateful to for that. The entire explanations you have made, the simple site menu, the friendships you can help foster – it’s got many great, and it’s really helping our son and the family imagine that the issue is entertaining, and that is particularly indispensable. Thank you for everything!

  3. My spouse and i have been really lucky that Louis managed to finish off his research through your precious recommendations he made through your web pages. It is now and again perplexing to just find yourself handing out solutions which people today may have been selling. Therefore we know we need the writer to give thanks to for that. The specific illustrations you have made, the simple web site navigation, the relationships you will help to instill – it’s got most powerful, and it’s really leading our son in addition to the family reason why the subject is fun, which is really pressing. Thank you for the whole lot!

  4. Thank you for every one of your labor on this web site. My daughter really loves making time for investigations and it’s really obvious why. Most people learn all regarding the lively mode you present priceless tactics through this web site and even inspire participation from some others on the point then our favorite princess is now starting to learn a whole lot. Take advantage of the rest of the year. You are always doing a remarkable job.

  5. I am only writing to make you understand what a really good experience my wife’s princess obtained viewing your webblog. She noticed some things, which included what it is like to possess an ideal teaching spirit to make others without difficulty learn about specific tortuous things. You undoubtedly surpassed readers’ expected results. Thanks for rendering those productive, trusted, informative as well as unique tips on your topic to Mary.

  6. Thanks for the useful information on credit repair on your web-site. The thing I would offer as advice to people will be to give up a mentality they will buy at this moment and pay back later. As being a society most people tend to repeat this for many factors. This includes family vacations, furniture, plus items we really want to have. However, you must separate a person’s wants from the needs. When you’re working to fix your credit score you really have to make some trade-offs. For example you possibly can shop online to economize or you can click on second hand suppliers instead of highly-priced department stores to get clothing.

  7. A lot of thanks for all your efforts on this site. Gloria enjoys doing investigation and it’s easy to understand why. Almost all know all concerning the dynamic means you deliver great information on the web blog and as well encourage contribution from other people on that concept plus our daughter is actually being taught a lot. Have fun with the remaining portion of the new year. You’re the one conducting a glorious job.

  8. My wife and i have been so relieved when Michael managed to finish up his studies via the precious recommendations he obtained using your web site. It is now and again perplexing to simply happen to be offering things which often some others might have been trying to sell. We remember we need you to thank for this. The type of illustrations you made, the straightforward website navigation, the relationships your site make it easier to instill – it is most spectacular, and it is letting our son in addition to us reckon that this subject matter is pleasurable, which is certainly truly serious. Thanks for the whole lot!

Leave a Reply

Your email address will not be published.