What is the "default" implementation of method defined in an Interface? - java

In the Collection Interface I found a method named removeIf() that contains its implementation.
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
I want to know if there is any way to define method body in an interface?
What is the default keyword and how does it work?

From https://dzone.com/articles/interface-default-methods-java
Java 8 introduces “Default Method” or (Defender methods) new feature, which allows developer to add new methods to the interfaces without breaking the existing implementation of these interface. It provides flexibility to allow interface define implementation which will use as default in the situation where a concrete class fails to provide an implementation for that method.
public interface A {
default void foo(){
System.out.println("Calling A.foo()");
}
}
public class ClassAB implements A {
}
There is one common question that people ask about default methods when they hear about the new feature for the first time:
What if the class implements two interfaces and both those interfaces define a default method with the same signature?
Example to illustrate this situation:
public interface A {
default void foo(){
System.out.println("Calling A.foo()");
}
}
public interface B {
default void foo(){
System.out.println("Calling B.foo()");
}
}
public class ClassAB implements A, B {
}
This code fails to compile with the following result:
java: class Clazz inherits unrelated defaults for foo() from types A and B
To fix that, in Clazz, we have to resolve it manually by overriding the conflicting method:
public class Clazz implements A, B {
public void foo(){}
}
But what if we would like to call the default implementation of method foo() from interface A instead of implementing our own.
It is possible to refer to A#foo() as follows:
public class Clazz implements A, B {
public void foo(){
A.super.foo();
}
}

Those methods are called default methods. Default method or Defender method is one of the newly added features in Java 8.
They will be used to allow an interface method to provide an implementation used as default in the event that a concrete class doesn't provide an implementation for that method.
So, if you have an interface, with a default method:
public interface Hello {
default void sayHello() {
System.out.println("Hello");
}
}
The following class is perfectly valid:
public class HelloImpl implements Hello {
}
If you create an instance of HelloImpl:
Hello hello = new HelloImpl();
hello.sayHello(); // This will invoke the default method in interface
Useful Links:
Updated Oracle Tutorial
Everything about Java 8
Defender Methods

I did a bit of research and i found the following. Hope this helps.
Existing problem
Normal interface methods are declared as abstract and must be defined in the class that implements the interface. This 'burdens' the class implementer with the responsibility to implement every declared method. More importantly, this also means that extending an interface is not possible after 'publication'. Otherwise, all implementers would have to adapt their implementation, breaking backwards source and binary compatibility.
Solution adopted in Java 8
To cope with these problems, one of the new features of JDK 8 is the possibility to extend existing interfaces with default methods. Default methods are not only declared, but also defined in the interface.
Important points to note
Implementers can choose not to implement default methods in
implementing class.
Implementers can still override default
methods, like regular non-final class methods can be overridden in
subclasses.
Abstract classes can even (re)declare default methods
as abstract, forcing subclasses to reimplement the method (sometimes
called 're-abstraction').

Related

default method in interfaces

For a while I have been puzzled as to why we need default methods in Interfaces, and today I was reading about it. Even though few of my questions are answered, I still have a few queries.
Let's take a simple example, I've an interface1. Class A and class B implement interface1. interface1 has a method X.
interface interface1{
void methodX();
}
class A implements interface1{
#override
public void methodX(){
//something
};
}
class B implements interface1{
#override
public void methodX(){
//something
};
}
From what I understood, since adding a new method Y to interface1, would have broken Class A and class B, default methods were introduced. This means that now I can add a default method, without the need for modifying Class A and Class B. This also means that the default method has to be generic enough to do whatever we expect it to do, for both class A and class B.
Now, let's consider that we normally add functions in interface so that we can provide a class specific implementation to them by overriding it. So basically, if I am adding a method Y in an interface as default, then I will expect class A and class B (or class A/ClassB) to override the method.
Which means, I will be modifying Class A/Class B or both.
This is what confuses me. This could have been handled by creating a new interface and modifying 1 (or both) class(es) to implement that new interface (interface 2 extending interface1), and then provide implementation for the same in the class.
interface interface2 extends interface1{
void methodY();
}
class A implements interface2{
#override
public void methodY(){
//something
}
}
class B implements interface1{
#override
public void methodX(){
//something
};
}
How does default method, actually help us in not modifying the classes which implements it.
Default methods provides "base" implementation of some behaviour which can be composed of existing methods at given interface. For example, you have an interface with add method. This is enough to provide default implementation for addAll behaviour:
default void addAll(Collection<? extends E> c) { for (E e : c) { add(e); } }
Another example can be sort method in List, which can be implemented by provided methods: get(int), set(int,E), size(). Subclass might override this default implementation in order to provide more efficient sorting based on specific list properties. In LinkedList you can take advantage of attaching/detaching nodes (no need to shift an elements on right when inserting or removing node), in ArrayList you can take advantage of super fast access of element at any index).
If you mandate all the implementing classes to override the new method, then it should (if there is no valid default implementation) not be a default method.
But, the approach you have said in which to create a new interface that extends the existing one and making the classes that wish to override the new method(s) by changing the interface type they implement will be problematic since the new method is part of the new interface, you cannot access it when you have the parent/base interface type.
Example:
Existing code:
interface Base {
void m1();
}
class A implements Base {
#Override
public void m1() {
....
}
}
class B implements Base {
#Override
public void m1() {
....
}
}
You create the following interface
interface ExtendedBase extends Base {
void m2();
}
Only class A wants to implement m2. So, it becomes
class A implements ExtendedBase {
#Override
public void m1() {
....
}
#Override
public void m2() {
....
}
}
All is good so far.
When you have a method that takes an object of type Base, you can only call m1 on it (irrespective of you pass object of type A or B)
void someMethod(Base base) {
base.m1();
//base.m2(); won't work
}
To actually make use of m2 elsewhere, you need to change Base to ExtendedBase which would mean that you can no longer pass a B to it. So, you have made all classes implement m2 anyway.
Since a default method is defined in an interface, it can only access instance methods also defined in the same interface. Hence, it can only invoke functionality that is actually provided by the implementing classes (in your example A and B).
The default method does not "change" the classes' behavior, it extends it by accessing existing, properly defined behavior.
So basically, if I am adding a method Y in an interface as default, then I will expect class A and class B (or class A/ClassB) to override the method.
This wrong.
The idea of default method is to introduce new method without break compatibility with old code.
Existing implementation classes do not need to be modified to work with new default method.
This could have been handled by creating a new interface and modifying 1 (or both) class(es) to implement that new interface (interface 2 extending interface1), and then provide implementation for the same in the class.
Then you have to touch / modify the implementation class. That's what default method try to avoid.

How to avoid multiple inheritance?

For a project, I have following classes:
SuperClass
Subclass 1
Subclass 2
The two subclasses extend the superclass.
Now, I need a third class with the EXACT behaviour (read, same overriden method implementations) of both SubClass 1 and Subclass 2.
Because Subclass 1 overrides only 1 method in SuperClass, and Subclass 2 doesn't override that method, I want to make the third class inherit Superclass and just implement it with the methods of Subclass 1 and Subclass 2. Now, is this good OO-design? I see no other solution because multiple inheritance in Java just isn't possible. Are there any alternatives?
Java8 introduced default and static methods for interfaces. To a certain degree, that allows for multiple inheritance. But most likely, the correct solution would be to rework your design.
You see, inheritance is not about code re-use. It is about creating useful abstractions; and make good use of polymorphism for example.
In your case: maybe those functionalities could/should be put into smaller interfaces; and then segregated into their own, independent classes. And then you use composition of objects instead of inheritance to build the thing you need.
Here is an example using Java 8's default methods as #GhostCat mentioned. I don't see anything wrong with this OO design per se. Whether or not it's appropriate to your use case depends on the details of the problem you're solving.
public class Main {
public static void main(String... args) {
SuperClass sc = new SubClass3();
sc.foo(); // overridden foo
sc.bar(); // overridden bar
}
interface SuperClass {
default void foo() {
System.out.println("default foo");
}
default void bar() {
System.out.println("default bar");
}
}
interface SubClass1 extends SuperClass {
#Override
default void foo() {
System.out.println("overridden foo");
}
}
interface SubClass2 extends SuperClass {
#Override
default void bar() {
System.out.println("overridden bar");
}
}
static class SubClass3 implements SubClass1, SubClass2 {}
}

What's the benefit (if there's any) of using interface only with static method? [duplicate]

I was learning through interfaces when I noticed that you can now define static and default methods in an interface.
public interface interfacesample2 {
public static void method() {
System.out.println("hello world");
}
public default void menthod3() {
System.out.println("default print");
}
}
Kindly explain the difference of the two and also if there's an example of when we would use this would be nice. A little confused on Interfaces.
Differences between static and default methods in Java 8:
1) Default methods can be overriden in implementing class, while static cannot.
2) Static method belongs only to Interface class, so you can only invoke static method on Interface class, not on class implementing this Interface, see:
public interface MyInterface {
default void defaultMethod(){
System.out.println("Default");
}
static void staticMethod(){
System.out.println("Static");
}
}
public class MyClass implements MyInterface {
public static void main(String[] args) {
MyClass.staticMethod(); //not valid - static method may be invoked on containing interface class only
MyInterface.staticMethod(); //valid
}
}
3) Both class and interface can have static methods with same names, and neither overrides other!
public class MyClass implements MyInterface {
public static void main(String[] args) {
//both are valid and have different behaviour
MyClass.staticMethod();
MyInterface.staticMethod();
}
static void staticMethod(){
System.out.println("another static..");
}
}
A static method is a method that applies to the class 'namespace', so to speak. So a static method foo of interface Interface is accessed by Interface.foo(). Note that the function call does not apply to any particular instance of the interface.
A default implementation bar on the other hand, is called by
Interface x = new ConcreteClass();
x.bar();
A static interface method cannot know about the this variable, but a default implementation can.
1. explain the difference of the two
Static interface methods are like static class methods(here they belong to Interface only). Where as the default interface methods provide default implementation of interface methods (which implementing classes may override)
But remember in case a class is implementing more than one interface with same default method signature then the implementing class needs to override the default method
You can find a simple example below (can DIY for different cases)
public class Test {
public static void main(String[] args) {
// Accessing the static member
I1.hello();
// Anonymous class Not overriding the default method
I1 t = new I1() {
#Override
public void test() {
System.out.println("Anonymous test");
}
};
t.test();
t.hello("uvw");
// Referring to class instance with overridden default method
I1 t1 = new Test2();
t1.test();
t1.hello("xyz");
}
}
interface I1 {
void test();
//static method
static void hello() {
System.out.println("hello from Interface I1");
}
// default need not to be implemented by implementing class
default void hello(String name) {
System.out.println("Hello " + name);
}
}
class Test2 implements I1 {
#Override
public void test() {
System.out.println("testing 1234...");
}
#Override
public void hello(String name) {
System.out.println("bonjour" + name);
}
}
2. when we would use this would be nice.
That depends on your problem statement. I would say Default methods are useful, if you need same implementation for a method in your specification in all the classes in that contract, Or it may be used like Adapter classes.
here is a good read: https://softwareengineering.stackexchange.com/questions/233053/why-were-default-and-static-methods-added-to-interfaces-in-java-8-when-we-alread
also below oracle doc explains default & static methods for evolving existing interfaces:
Users who have classes that implement interfaces enhanced with new
default or static methods do not have to modify or recompile them to
accommodate the additional methods.
http://docs.oracle.com/javase/tutorial/java/IandI/nogrow.html
Here is my view:
static method in interface:
You can call it directly (InterfacetA.staticMethod())
Sub-class will not be able to override.
Sub-class may have method with same name as staticMethod
default method in interface:
You can not call it directly.
Sub-class will be able to override it
Advantage:
static Method: You don't need to create separate class for utility method.
default Method: Provide the common functionality in default method.
This link has some useful insights, have listed few of them here.
default & static methods have bridged down the differences between interfaces and abstract classes.
Interface default methods:
It helps in avoiding utility classes, such as all the Collections class method can be provided in the interfaces itself.
It helps in extending interfaces without having the fear of breaking implementation classes.
Interface static methods:
They are part of interface, we can’t use it for implementation class objects.
It helps in providing security by not allowing implementation classes to override them.
Like to quote another useful reference.
As per Java14 JLS doc:
Default Method:
It is an instance method declared in an interface with the default
modifier
It can be accessed only by the instance of the implementing class
only
Its body is always represented by a block, which provides a default
implementation or behaviour for any implementing class without
overriding the method
It can never be static or private
Static Method:
It can be invoked by interface without reference to a particular
object, just like class static methods
Static method can be private
The implementing class can not access static method
Lets understand it with the help of below example code:
public interface MyInterface {
private void privateMethod() {
System.out.println("Hi, this is privateMethod");
}
private static void staticPrivateMethod() {
System.out.println("Hi, this is staticPrivateMethod");
}
static void staticMethod() {
//privateMethod(); // Non-static method cannot be referenced from a static contex
System.out.println("Hi, this is staticMethod");
staticPrivateMethod();
}
default void defaultMethod() {
System.out.println("Hi, this is defaultMethod");
}
}
public class MyInterfaceImpl implements MyInterface{
public static void main(String[] args) {
MyInterface.staticMethod();
// myInterface.staticMethod(); // Not allowed
MyInterface myInterface = new MyInterfaceImpl();
myInterface.defaultMethod();
// MyInterface.defaultMethod(); // Not allowed
}
}
According to Oracle's Javadocs: http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.
A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.
Normally, static method in interface is used as Helper methods while default method are used as a default implementation for classes that implements that interface.
Example:
interface IDemo {
//this method can be called directly from anywhere this interface is visible
static int convertStrToInt(String numStr) {
return Integer.parseInt(numStr);
}
//getNum will be implemented in a class
int getNum();
default String numAsStr() {
//this.getNum will call the class's implementation
return Integer.toString(this.getNum());
}
}
Interface default methods:
It helps in avoiding utility classes, such as all the Collections class method can be provided in the interfaces itself.
It helps in extending interfaces without having the fear of breaking implementation classes.
Interface static methods:
They are part of interface, we can’t use it for implementation class objects.
It helps in providing security by not allowing implementation classes to override them.
Now how static method providing security. Let's see an example.
interface MyInterface {
/*
* This is a default method so we need not to implement this method in the implementation classes
*/
default void newMethod() {
System.out.println("Newly added default method in Interface");
}
/*
* This is a static method. Static method in interface is similar to default method except that we cannot override them in the implementation classes. Similar to default methods, we need to implement these methods in implementation classes so we can safely add them to the existing interfaces.
*/
static void anotherNewMethod() {
System.out.println("Newly added static method in Interface");
}
/*
* Already existing public and abstract method We must need to implement this method in implementation classes.
*/
void existingMethod(String str);
}
public class Example implements MyInterface {
// implementing abstract method
public void existingMethod(String str) {
System.out.println("String is: " + str);
}
public void newMethod() {
System.out.println("Newly added default method in Class");
}
static void anotherNewMethod() {
System.out.println("Newly added static method in Class");
}
public static void main(String[] args) {
Example obj = new Example();
// calling the default method of class
obj.newMethod();
// calling the static method of class
obj.anotherNewMethod();
// calling the static method of interface
MyInterface.anotherNewMethod();
// calling the abstract method of interface
obj.existingMethod("Java 8 is easy to learn");
}
}
Here obj.newMethod(); printing class implementation logic, means we can change the logic of that method inside implementation class.
But obj.anotherNewMethod(); printing class implementation logic ,but not changed interface implementation. So if any encryption-decryption logic written inside that method you can't change.
we cannot execute Interfacesample2.menthod3(); because it is not static method. In order to execute method3() we need an instance of Interfacesample2 interface.
Please find the following practical example:
public class Java8Tester {
public static void main(String args[]){
// Interfacesample2.menthod3(); Cannot make a static reference to the non-static method menthod3 from the type Interfacesample2
new Interfacesample2(){ }.menthod3();// so in order to call default method we need an instance of interface
Interfacesample2.method(); // it
}
}
interface Interfacesample2 {
public static void method() {
System.out.println("hello world");
}
public default void menthod3() {
System.out.println("default print");
}
}
Starting Java 8 interface can also have static method. Like static method of a class, static method of an interface can be called using Interface name.
Example
public interface Calculator {
int add(int a, int b);
int subtract(int a, int b);
default int multiply(int a, int b) {
throw new RuntimeException("Operation not supported. Upgrade to UltimateCalculator");
}
static void display(String value) {
System.out.println(value);
}
}
Difference between static and default method of interface is default method supports inheritance but static method does not. Default method can be overridden in inheriting interface.
Here is good read about interface default method and static method. Interface Default Method in Java 8
All good answers here. I would like to add another practical usage of the static function in the interface. The tip is coming from the book - Effective Java, 3rd Edition by Joshua Bloch in Chapter2: Creating and Destroying Object.
Static functions can be used for static factory methods.
Static factory method are methods which return an object. They work like constructor. In specific cases, static factory method provides more readable code than using constructor.
Quoting from the book - Effective Java, 3rd Edition by Joshua Bloch
Prior to Java 8, interfaces couldn’t have static methods. By
convention, static factory methods for an interface named Type were
put in a noninstantiable companion class (Item 4) named Types.
Author gives an example of Collections where such static factory method is implemented. Checking on the code, Josh Bloch can be seen as first author of Collections class. Although Collections is a class and not interface. But the concept still applies.
For example, the Java Collections Framework has forty-five utility
implementations of its interfaces, providing unmodifiable collections,
synchronized collections, and the like. Nearly all of these
implementations are exported via static factory methods in one
noninstantiable class (java.util.Collections). The classes of the
returned objects are all nonpublic.
Further he explains that API is not only smaller, it helps with the code readability and API ease..
It is not just the bulk of the API that is reduced but the conceptual
weight: the number and difficulty of the concepts that programmers
must master in order to use the API. The programmer knows that the
returned object has precisely the API specified by its interface, so
there is no need to read additional class documentation for the
implementation class.
Here is one of the static method from java.util.Collections class:
public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
return new UnmodifiableCollection<>(c);
}
Static Interface Method:
It is a static method which belongs to the interface only. We can
write implementation of this method in interface itself.
Static method can invoke only on interface class not on class.
Interface and implementing class , both can have static method with the same name without overriding each other.
It can be used as a utility method
Default Method:
It is a method with default keyword and class can override this method.
It can be invoked on interface as well as class.
We can override the default method in implementing class.
It can be used to provide common functionality in all implementing classes.
There is a link with detailed explanation. For detailed example: Default method vs static method in an interface in Java?

Should a default implementation of an interface with stub methods be abstract?

Suppose I have an interface
interface MyInterface {
void doSomething();
void doSomethingElse();
/* way more methods */
}
and maybe a default implementation like this:
class StubbedMyInterface implements MyInterace {
void doSomething() {
throw new UnsupportedOperationException();
}
void doSomethingElse() {
throw new UnsupportedOperationException();
}
/* all the other methods */
}
The stubbed class simply exists so that implementers of the interface don't need to implement all the methods they do not care about.
Should the class StubbedMyInterface be declared abstract? Or should it have a protected default constructor (so that e.g. a unit test for StubbedMyInterface which checks that all methods throw the desired exception can instantiate the class without creating a needless subclass)? Or should the class just be a normal public class?
UPDATE:
OK, I see I should provide more context. This problem occured while implementing a visitor pattern for an Abstract Syntax Tree. The interface defines one method per node type in the tree and these are quite a lot of types.
I have several of these Stub implementation:
One like the one above which is useful e.g. when processing the children of a certain node - in this case I know that only node type A,B and C can possibly be a child of the node whose children I process. And if I got this wrong I want some error message to point me to the problem.
One which just has empty implementations for each method - which is useful if I want to process only nodes I know what to do with and ignore the others.
One which by default traverses the children of each node which is useful if I want to traverse a full tree but care only about certain node types.
To me, this comes down to Booch. "A class should be minimalistic and complete." Is this class complete? If not, it should be abstract.
The nature of the structure isn't compelling. Is this class, on it's own, useful for something is the key factor. If not, it should be abstract.
If some of the interface method definitions are not supposed to be implemented, then I would prefer spreading them accross several interfaces.
Then I would implement only the exact interface, which holds the method definitions I'm interested in.
For example:
interface MyFirstInterface {
void doSomething();
}
interface MySecondInterface {
void doSomethingElse();
}
public class MyClass implement MySecondInterface {
void doSomethingElse() {
}
}
With Java 8, you can define a default implementation in the interface itself, which gives much more flexibility than an abstract class (you can inherit multiple interfaces) - so it could look like:
interface MyInterface {
default void doSomething() {
throw new UnsupportedOperationException();
}
default void doSomethingElse() {
throw new UnsupportedOperationException();
}
/* all the other methods */
}
With Java 7 and earlier, using an abstract class would make sense to provide a default basic implementation.
If implementers of the interface don't need to implement all the methods, they don't need implement this interface, they need only one interface in the hierarchy with needed method set.
I would use inheritance like this
interface MyFirstInterface {
void doSomething();
}
interface MySecondInterface extends MyFirstInterface {
void doSomethingElse();
}

not implementing all of the methods of interface. is it possible?

Is there any way to NOT implement all of the methods of an interface in an inheriting class?
The only way around this is to declare your class as abstract and leave it to a subclass to implement the missing methods. But ultimately, someone in the chain has to implement it to meet the interface contract. If you truly do not need a particular method, you can implement it and then either return or throw some variety of NotImplementedException, whichever is more appropriate in your case.
The Interface could also specify some methods as 'default' and provide the corresponding method implementation within the Interface definition (https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html). These 'default' methods need not be mentioned while implementing the Interface.
The point of an interface is to guarantee that an object will outwardly behave as the interface specifies that it will
If you don't implement all methods of your interface, than you destroy the entire purpose of an interface.
We can override all the interface methods in abstract parent class and in child class override those methods only which is required by that particular child class.
Interface
public interface MyInterface{
void method1();
void method2();
void method3();
}
Abstract Parent class
public abstract class Parent implements MyInterface{
#Override
public void method1(){
}
#Override
public void method2(){
}
#Override
public void method3(){
}
}
In your Child classes
public class Child1 extends Parent{
#Override
public void method1(){
}
}
public class Child2 extends Parent{
#Override
public void method2(){
}
}
I asked myself the same question, and then learned about Adapters. It solved my problem, maybe it can solve yours. This explains it very well : https://blogs.oracle.com/CoreJavaTechTips/entry/listeners_vs_adapters
You can do that in Java8. Java 8 introduces “Default Method” or (Defender methods) new feature, which allows a developer to add new methods to the Interfaces without breaking the existing implementation of these interfaces.
It provides flexibility to allow Interface define implementation which will use as default in the situation where a concrete Class fails to provide an implementation for that method.
interface OldInterface {
public void existingMethod();
default public void DefaultMethod() {
System.out.println("New default method" + " is added in interface");
}
}
//following class compiles successfully in JDK 8
public class ClassImpl implements OldInterface {
#Override
public void existingMethod() {
System.out.println("normal method");
}
public static void main(String[] args) {
ClassImpl obj = new ClassImpl ();
// print “New default method add in interface”
obj.DefaultMethod();
}
}
Define that class as an abstract class. However, you must implement those unimplemented methods when you want to create an instance of it (either by using a subclass or an anonymous class).
It is possible and it is easy. I coded an example.
All you have to do is inherit from a class that does implement the method. If you don't mind a class that is not instantiable, then you can also define an abstract class.
If you want an instantiable class, it is not possible. You may try to define an abstract class, though.
If you try to implement an interface and you find yourself in a situation where there is no need to implement all of them then, this is a code smell. It indicates a bad design and it violates Liskov substitution principle. Often this happens because of using fat interface.
Also sometimes this happens because you are trying to implement an interface from an external dependency. In this case, I always look inside the source code to see if there is any implementation of that interface which I can either use it directly or subclass it and override methods to my needs.
We can use Adapter classes ,which reduces complexcity by not making mandatory to implement all the methods present in the interface
Adapter class is a simple java class that implements an interface with only EMPTY implementation .
Instead of implementing interface if we extends Adapter class ,we provide implementation only for require method
ex--- instead of implementing Servlet(I) if we extends GenericServlet(AC) then we provide implementation for Service()method we are not require to provide implementation for remaining meyhod..
Generic class Acts as ADAPTER class for Servlet(I).
yes possible below shown is the way
interface Test {
void m() throws NullPointerException;
}
class Parent {
// Parent class doesn't implements Test interface
public void m() {
System.out.println("Inside Parent m()");
}
}
class Child extends Parent implements Test {
}
public class Program {
public static void main(String args[]) {
Child s = new Child();
s.m();
}
}

Categories