answersLogoWhite

0


Best Answer

We have been using references to the term "Implementing an Interface" throughout the preceding chapters and we havent yet actually dug deep into this topic. As you might remember, an Interface is nothing but a contract as to how a class should behave. It just declares the behavior as empty methods and the implementing class actually writes the code that will determine the behavior.

When you implement an interface, you're agreeing to adhere to the contract defined in the interface. That means you're agreeing to provide legal implementations for every method defined in the interface, and that anyone who knows what the interface methods look like can rest assured that they can invoke those methods on an instance of your implementing class. (Thy need not bother much about how you have implemented it. All they bother about is whether a method of the name mentioned in the interface is available or not)

Now, you might stop me and ask, what if I implement an interface and opt not to write code for a method that I am supposed to? The answer is simple. The compiler wouldn't let you do that. You cannot successfully implement an interface without providing method implementation for all the methods declared inside the interface. This is how the java system ensures that when someone knows a certain method name in an interface and has an instance of a class that implements it, can actually call that method without fear that the method isn't implemented inside the class.

Assuming an interface, Convertible, with two methods: openHood(), and setOpenHoodFactor(), the following class will compile:

public class Ball implements Convertible { // Keyword 'implements'

public void openHood() { }

public void setOpenHoodFactor(int bf) { }

}

Ok, I know what you are thinking now. "This has got to be the worst implementation class that you have seen". Though it compiles and runs as well, it is actually doing nothing… the interface contract guarantees that the class implementing it will have a method of a particular name but it never guaranteed a good implementation. In other words, the compiler does not bother whether you have code inside your method or not. All it cares is if you have methods of the matching names as in the interface. That's all…

Implementation classes must adhere to the same rules for method implementation as a class extending an abstract class. In order to be a legal implementation class, a nonabstract implementation class must do the following:

• Provide concrete (nonabstract) implementations for all methods from the declared interface.

• Follow all the rules for legal overrides.

• Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method.

• Maintain the signature of the interface method, and maintain the same return type (or a subtype).

• It does not have to declare the exceptions declared in the interface method declaration.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

When you create an interface, you're defining a contract for what a class can do, without saying anything about how the class will do it. Interfaces can be implemented by any class, from any inheritance tree. This lets you take radically different classes and give them a common characteristic.

Ex:

public interface MyExInterface {

...

//code

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Implements denotes defining an implementation for the methods of an interface. However interfaces have no implementation so that's not possible. An interface can however extend another interface, which means it can add more methods and inherit its type.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

1. Single Inheritance

A Scenario where one class is inheriting/extending the behavior of just one super class.

Ex: public class Ferrari extends Car {…}

2. Multilevel Inheritance

A Scenario where one class is inheriting/extending the bahavior of another class which in turn is inheriting behavior from yet another class.

Ex: public class Automobile {…}

Public class Car extends Automobile {…}

Public class Ferrari extends Car {…}

This multilevel inheritance actually has no limitations on the number of levels it can go. So as far as java goes, it is limitless. But for maintenance and ease of use sakes it is better to keep the inheritance levels to a single digit number.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Yes it can. Read below http://www.coderanch.com/t/410331/Beginning-Java/java/interface-implements-interface

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

No interface can't be extended it can only be implemented.Yes we can implement 2 or more interface in java using multiple interface concept

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you code to inherit from an interface in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you define variables in interface in java?

yes we can define a variable in an interface in java.


How do you use Java Native Interface to access Delphi code from Java classes?

The Delphi code would need to be compiled into a DLL, and the DLL is then called from java using the JNI. See http://home.pacifier.com/~mmead/jni/delphi/JavaToDPR/ to get started.


Can we implement multiple inheritance in java using package and interface?

Yes. Java does not support full fledged/proper multiple inheritance. But, whatever partial inheritance that Java supports can be implemented using interfaces Actually, java does not support multiple inheritance. You can achieve partial multiple inheritance using interfaces but java is not like C or C++ where you can do direct multiple inheritance. However, you can achieve partial multiple inheritance with the help of interfaces. Ex: public class FerrariF12011 extends Ferrari implements Car, Automobile {…} And this is under the assumption that Car and Automobile are interfaces. Here if you see, though you don't inherit concrete code from the Car or the Automobile interface, you do inherit skeleton methods that determine the way your class eventually behaves and hence this can be considered partial Multiple Inheritance.


What java interface must be implemented by all threads?

Runnable interface


Can you instantiate an interface?

According to a beginner's book on Java, an interface can't have constructors. Also, the interface itself can't contain the method implementation.

Related questions

Can you create a constructor for an interface in java?

NO, we cannot create a contructor for an interface in java.


Can you define variables in interface in java?

yes we can define a variable in an interface in java.


Can the processor of a computer system directly execute source programs written in Java?

No. Even using the Java Native Interface, code written in Java is always in a high-level language.


What is Full for of APIs in java?

API stands for Application programming interface. API's are pre-written Java code that can be used by other programmers to create their applications.


How do you use Java Native Interface to access Delphi code from Java classes?

The Delphi code would need to be compiled into a DLL, and the DLL is then called from java using the JNI. See http://home.pacifier.com/~mmead/jni/delphi/JavaToDPR/ to get started.


How interface supports indirect multiple inheritance in java?

Actually, java does not support multiple inheritance. You can achieve partial multiple inheritance using interfaces but java is not like C or C++ where you can do direct multiple inheritance. However, you can achieve partial multiple inheritance with the help of interfaces. Ex: public class FerrariF12011 extends Ferrari implements Car, Automobile {…} And this is under the assumption that Car and Automobile are interfaces. Here if you see, though you don't inherit concrete code from the Car or the Automobile interface, you do inherit skeleton methods that determine the way your class eventually behaves and hence this can be considered partial Multiple Inheritance


What is intrface in java?

interface is a list of methods which implements that interface


What is a tagging interface type?

A tagging interface type in Java is an interface that has not defined methods such as the java.io.Serializable interface.


Can we implement multiple inheritance in java using package and interface?

Yes. Java does not support full fledged/proper multiple inheritance. But, whatever partial inheritance that Java supports can be implemented using interfaces Actually, java does not support multiple inheritance. You can achieve partial multiple inheritance using interfaces but java is not like C or C++ where you can do direct multiple inheritance. However, you can achieve partial multiple inheritance with the help of interfaces. Ex: public class FerrariF12011 extends Ferrari implements Car, Automobile {…} And this is under the assumption that Car and Automobile are interfaces. Here if you see, though you don't inherit concrete code from the Car or the Automobile interface, you do inherit skeleton methods that determine the way your class eventually behaves and hence this can be considered partial Multiple Inheritance.


What java interface must be implemented by all threads?

Runnable interface


What is Re usability in java?

Reusability in Java means that once you write some code, you can use it over and over again - if you write it so that it can be reused. For example, if you write a Enumeration-Iterator adapter, or some code that lets you treat an Enumeration - Java legacy code - as an Iterator - shiny NEW Java code - then you can use it not only in your animal simulation program, but also in your Jacuzzi interface program.


Can you create an empty interface with no definitions?

Yes. This is a valid interface definition in Java: interface Useless {}