Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 50

MODERN JAVA

LECTURE 2
Today’s Agenda

Default And Static Methods

• Interfaces Before Java 8

• Enhancements Done To Interface

• Default Methods In Interface

• Static Methods In Interface


Interfaces Before Java 8

 Before Java 8, we had 2 rules for an interface with


respect to it’s members.

 RULES:
1. Every method present inside interface is always
public and abstract (whether we use these
keywords or not) .

2. And every variable inside the interface is public


static and final (whether we are declaring or not).
Enhancements Done By Java 8

 Java 8 unlocked the interfaces and allowed them to be


more flexible and extendable.

 Starting with Java 8 edition, we can add concrete


implementations to an interface.

 We can have fully defined and implemented methods


in an interface.
Enhancements Done By Java 8

 There are two categories of methods that we could define


on an interface–

 a default method and

 a static method
What Is A Default Method ?

• Default methods are the concrete code bodies


provided in the interface itself.

• The only difference is that we have a prefix called


default in front of the method name

• These methods are also known as defender method


or virtual extension method.
What Is A Default Method ?

• Syntax:

default public <method_name>(<arg_list>)


{
// method body
}

• Example:
interface Greet
{
default public void message()
{
System.out.println(“Good Morning”);
}
}
What Is A Default Method ?

 Default methods are by default available to all


implementation classes.

 Further based on requirement an implementation class


can use these default methods directly or can also
override it.
Example

• Suppose we have an interface called


SampleInterface with 2 methods as shown below:

• Code:
package defaultmethodexample;
public interface SampleInterface
{
public void method1(); // this is abstract method
default public void method2() // this is default method
{
System.out.println(“SampleInterface.method2”);
}
}
Example

• Now if a class implements this interface it has to


compulsorily override the abstract method
method1() while it can directly use the default
method method2() without any compulsion of
overriding as shown below:
• Code:
package defaultmethodexample;
public class SampleImpl implements SampleInterface{

@Override
public void method1() {
System.out.println("In SampleImpl.method1");
}
}
Example

• Now , when we will call these methods from driver


class the implementation of method1() done by the
class SampleImpl will run while in case of
method2() , the concrete body given by the interface
SampleInterface will run.
Example

• Code

package defaultmethodexample;
public class UseSample {
public static void main(String[] args) {
SampleInterface obj=new SampleImpl();
obj.method1();
obj.method2();
}
}

In SampleImpl.method1
• Output:
In SampleInterface.method2
Overriding Default Method

• As mentioned previously , an implementation class


can override the default methods ,as shown below.

package defaultmethodexample;
public class SampleImpl implements SampleInterface{
@Override
public void method1() {
System.out.println("In SampleImpl.method1");
}
@Override
public void method2() {
System.out.println("In SampleImpl.method2");
}
}
Overriding Default Method

• So now both method calls will execute SampleImpl’s


implementation
• Code
package defaultmethodexample;
public class UseSample {
public static void main(String[] args) {
SampleInterface obj=new SampleImpl();
obj.method1();
obj.method2();
}
}

In SampleImpl.method1
• Output:
In SampleImpl.method2
Extending Interfaces That
Contain Default Method

 When we extend an interface that contains a default


method, there are 3 possibilities:

 Not mention the default method at all, which lets our


extended interface inherit the default method.

 Re declare the default method, which makes it abstract.

 Redefine the default method, which overrides it.


Example (Case 1)

• Suppose we have an interface called


SampleInterface1 with a default method called
method1() as shown below:

• Code:
package defaultmethodexample;
public interface SampleInterface1 {
default public void method1(){
System.out.println("In SampleInterface1.method1");
}
}
Example(Case 1)

• Now we extend this interface in another interface


called SampleInterface2 and leave the default
method method1() unmentioned as shown below:

• Code:
package defaultmethodexample;
public interface SampleInterface2 extends SampleInterface1{

}
Example(Case 1)

• Then we create a class called SampleImpl which


implements SampleInterface2 as shown below:

• Code:

package defaultmethodexample;
public class SampleImpl implements SampleInterface2{

}
Example(Case 1)

• Finally we define the driver class and call the default


method method1() which will run the method
implementation defined in SampleInterface1 as shown
below:
• Code:
package defaultmethodexample;
public class UseSample {
public static void main(String[] args) {
SampleInterface2 obj=new SampleImpl();
obj.method1();
}
}

In SampleInterface1.method1
• Output:
Example (Case 2)

• Suppose we have an interface called


SampleInterface1 with a default method called
method1() as shown below:

• Code:
package defaultmethodexample;
public interface SampleInterface1 {
default public void method1(){
System.out.println("In SampleInterface1.method1");
}
}
Example(Case 2)

• Now we extend this interface in another interface


called SampleInterface2 and re declare the default
method method1() as shown below:

• Code:
package defaultmethodexample;
public interface SampleInterface2 extends SampleInterface1{
public void method1(); // this method now becomes abstract
}
Example(Case 2)

• Then we create a class called SampleImpl which


implements SampleInterface2 and leaves the method
method1() unmentioned.

• Now since method1() has become abstract , the compiler


will give error
• Code:
package defaultmethodexample;
public class SampleImpl implements SampleInterface2{
}

Error: SampleImpl is not abstract and does not


• Output: override abstract method method1() in
SampleInterface2
Example (Case 3)

• Suppose we have an interface called


SampleInterface1 with a default method called
method1() as shown below:

• Code:
package defaultmethodexample;
public interface SampleInterface1 {
default public void method1(){
System.out.println("In SampleInterface1.method1");
}
}
Example(Case 3)

• Now we extend this interface in another interface


called SampleInterface2 and override the default
method method1() as shown below:

• Code:
package defaultmethodexample;
public interface SampleInterface2 extends SampleInterface1{
default public void method1(){
System.out.println("In SampleInterface2.method1");
}

}
Example(Case 3)

• Then we create a class called SampleImpl which


implements SampleInterface2 as shown below:

• Code:

package defaultmethodexample;
public class SampleImpl implements SampleInterface2{

}
Example(Case 3)

• Finally we define the driver class and call the default


method method1() which will run the method
implementation defined in SampleInterface2 as shown
below:
• Code:
package defaultmethodexample;
public class UseSample {
public static void main(String[] args) {
SampleInterface2 obj=new SampleImpl();
obj.method1();
}
}

In SampleInterface2.method1
• Output:
Default Methods And
Multiple Inheritance

 In java, a class can implement N number of interfaces.

 Now, suppose two interfaces which are implemented by a


single class contain the same default method . Then
can you tell what will happen ?

 In this case, the code simply won't compile, as


there's a conflict caused by multiple interface
inheritance (a.k.a the Diamond Problem)
Example

public interface Greet1 {


default public void greet(){
System.out.println(“Good Morning");
}
}

public interface Greet2 {


default public void greet(){
System.out.println(“Good Afternoon");
}
}

public class Greetings implements Greet1,Greet2{

}
Error: class Greetings inherits unrelated defaults for
Output: greet() from types Greet1 and Greet2
Solution

 The solution to this problem is that the implementing


class has to compulsorily override the default method
inherited from both the interfaces.

 However the implementing class can call a particular


interface’s version of the method by using the syntax :

 <interface_name>.super.<method_name>();
Example

public interface Greet1 {


default public void greet(){
System.out.println(“Good Morning");
}
}

public interface Greet2 {


default public void greet(){
System.out.println(“Good Afernoon");
}
}

public class Greetings implements Greet1,Greet2{


@Override
public void greet(){
System.out.println(“Good Evening");
}
}
Example

public class UseGreetings{


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

Output:
Good Evening
Calling Interface’s Method

public class Greetings implements Greet1,Greet2{


@Override
default public void greet(){
Greet1.super.greet();
Greet2.super.greet();
}
}
public class UseGreetings{
public static void main(String[] args) {
Greetings obj=new Greetings();
obj.greet();
}
}

Good Morning
Output: Good Afternoon
Overriding Object Class
Methods

 A default method cannot override a method from the


class java.lang.Object.

 So the following code will give syntax error.

public interface Greet1 {


default public String toString(){
return "Good Morning";
}
}
Output:
Error: default method toString in interface Greet1 overrides a
member of java.lang.Object
Why Is It So ?

 The reasoning is very simple, it’s because Object is the


base class for all the Java classes.

 So even if we have Object class methods defined as


default methods in interfaces, it will be useless
because Object class method will always be used.

 That’s why to avoid confusion, we can’t have default


methods that are overriding Object class methods.
Benefits Of Default Method

 Keeps the code backward compatible.

 To add the forEach() and stream() support easily.


What Is A Static Method ?

• Static Methods in interface are those methods,


which are defined in the interface with the keyword
static.

• Unlike other methods in interface, these static


methods contain the complete definition of the
method within the interface body.
What Is A Static Method ?

• Syntax:

static public <method_name>(<arg_list>)


{
// method body
}

• Example:
interface Greet
{
static public void message()
{
System.out.println(“Good Morning”);
}
}
Important Points

• Since static methods don't belong to a particular


object, they are not part of the classes implementing
the interface.

• Thus they have to be called by using the interface


name preceding the method name.
Example

• Suppose we have an interface called


SampleInterface with 2 methods as shown below:

• Code:
package staticmethodexample;
public interface SampleInterface
{
public void method1(); // this is abstract method
public static void method2() // this is static method
{
System.out.println(“In SampleInterface.method2”);
}
}
Example

• Now if a class implements this interface it has to


compulsorily override the abstract method
method1() as shown below:

• Code:
package staticmethodexample;
public class SampleImpl implements SampleInterface{

@Override
public void method1() {
System.out.println("In SampleImpl.method1");
}
}
Example

• Now , when we will call these methods from driver


class the call to method1() will be done by object
reference while the call to method2() can only be
done by using the name of the interface
SampleInterface.

• We cannot call static method of an interface using


object of implementation class nor we can call it
using the name of the implementation class.
Example

• Code

package staticmethodexample;
public class UseSample {
public static void main(String[] args) {
SampleInterface obj=new SampleImpl();
obj.method1();
SampleInterface.method2();
//if code below is uncommented , it will show
error
//obj.method2();
}
}

In SampleImpl.method1
In SampleInterface.method2
• Output:
Important Points

• Another point to remembers about interface static


methods is that we can’t override them in the
implementation classes.

• This feature helps us in avoiding undesired results in


case of poor implementation in implementation classes
Important Points

• If we try to override them using the @Override


annotation then compiler will generate syntax error.

• If we try to override a static method of an


interface by defining a similar method in the
implementing interface, it will be considered as another
(static) method of the class.
Example

public interface Greet1 {


public static void greet(){
System.out.println(“Good Morning");
}
}

public class Greetings implements Greet1 {


public static void greet(){
System.out.println(“Good Evening");
}
}
Example

public class UseGreetings{


public static void main(String[] args) {
Greet1.greet();
Greetings.greet();
}
}

Output:
Good Morning
Good Evening
Benefits Of Static Method

 Grouping of utility methods.

 Additional level of security.


Can We Have main() Method In
Interface ?

 From Java 8 onwards it is perfectly legal to have main()


method defined in an interface .

 Moreover we also can run our code directly using the


interface even if there is no class defined in the code.
Example

• Code

public interface Greet {


public static void main(String[] args) {
System.out.println(“Good Morning”);
}
}

• Compilation: javac Greet.java

• Execution: java Greet

• Output: Good Morning


Interface V/s Abstract Class

Java 8 Interface Abstract Class


In interface every variable is always In abstract class we can declare
public static final and there is no instance variable which can be used in
chance to declare instance variable. their child class.

We can’t declare Constructors in We can declare Constructors in


interface. abstract class.

We can’t declare static-blocks in We can declare static-blocks in


interface. abstract class.

We can’t override Object class We can override Object class


methods inside interface. methods inside abstract class.

Interface never talks about state of Abstract class can talk about state of
Object. Object.

Functional Interface with default Abstract class can’t refer lambda


Methods can refer lambda expressions.
expression.

You might also like