0% found this document useful (0 votes)
4 views9 pages

Hlo 2

The document discusses the introduction of static, default, and private concrete methods in Java interfaces from versions 8 to 9. It explains how static methods are accessible at the interface level, default methods at the implementation object level, and private methods are only accessible within the interface. Additionally, it covers the rules regarding interfaces, such as the inability to declare blocks and constructors, and the use of abstract classes in Java.

Uploaded by

MONICA NAHAK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Hlo 2

The document discusses the introduction of static, default, and private concrete methods in Java interfaces from versions 8 to 9. It explains how static methods are accessible at the interface level, default methods at the implementation object level, and private methods are only accessible within the interface. Additionally, it covers the rules regarding interfaces, such as the inability to declare blocks and constructors, and the use of abstract classes in Java.

Uploaded by

MONICA NAHAK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Dt : 24/8/2023

(a)static concrete methods(Java8 - 2014):

=>From Java8 version onwards the interfaces can be declared with

static Concrete methods.

=>These static concrete methods will get the memory within the interface

ii
while interface loading and can be accessed with Interface_name.

ath
Coding Rule:

aip
=>Static concrete methods of Interface are not available to implementation

classes,which means Implementation classes cannot access Static concrete


hM
methods of Interfaces.

(b)default concrete methods(Java8 - 2014)


tes

=>From Java8 version omwards the interfaces canbe declared with default

concrete methods.
a
nk

=>default concrete methods means the methods declared with "default"

keyword and which are NonStatic methods.


Ve

Coding Rule:

=>default concrete methods are available to implementation classes,which

means default concrete methods can be accessed with implementation object

name.
ProjectName : Interface_App4

packages,

p1 : ITest.java
package p1;
public interface ITest {

ii
public abstract void m1(int a);
public static void m2(int b) {

ath
System.out.println("****static m2(b)****");
System.out.println("The value b:"+b);
}
public default void m3(int c) {

aip
System.out.println("****default m3(c)****");
System.out.println("The value c:"+c);
}
hM
}

p1 : IClass.java
tes

package p1;
public class IClass implements ITest{
public void m1(int a) {
System.out.println("****Implemented m1(a)****");
a

System.out.println("The value a:"+a);


nk

}
}
Ve

p2 : DemoInterface4.java(MainClass)
package p2;
import p1.*;
public class DemoInterface4 {
public static void main(String[] args) {
ITest.m2(123);//Static method call
IClass ob = new IClass();//Implementation Object
ob.m1(124);//Implemented method call
//IClass.m2(125);//Static method Call
ob.m3(126);//default method call
}
}

o/p:

****static m2(b)****

ii
ath
The value b:123

****Implemented m1(a)****

The value a:124

****default m3(c)****

The value c:126


aip
hM
---------------------------------------------------------------

Note:
tes

=>Static Concrete methods will be at Interface level and default

concrete methods are at implementation Object level.


a

=>Static concrete methods are used in multiple inheritance applications


nk

and default concrete methods are used in NonMultiple inheritance


Ve

applications.

------------------------------------------------------------------

(c)private concrete methods(Java9 - 2017)

=>From Java9 version onwards the interfaces can be declared with

private concrete methods.


=>private concrete methods means the concrete methods which are declared

with private keyword and which are available in two types:

(i)static private concrete methods

(ii)NonStatic private concrete methods

ii
Coding Rule:

ath
=>private concrete methods are accessed only inside the interface,which

means accessed by the NonPrivate concrete methods of Interface.

ProjectName : Interface_App5

aip
hM
packages,

p1 : ITest.java
package p1;
tes

public interface ITest {


public abstract void m1(int a);
private static void m2(int b) {
System.out.println("****static private
a

m2(b)****");
nk

System.out.println("The value b:"+b);


}
private void m3(int c) {
Ve

System.out.println("****NonStatic private
m3(c)****");
System.out.println("The value c:"+c);
}
public default void access(int b,int c) {
ITest.m2(b);
this.m3(c);
}
}
p1 : IClass.java
package p1;
public class IClass implements ITest{
public void m1(int a) {
System.out.println("****Implemented m1(a)****");
System.out.println("The value a:"+a);
}

ii
}

ath
p2 : DemoInterface5.java(MainClass)
package p2;

aip
import p1.*;
public class DemoInterface5 {
public static void main(String[] args) {
hM
IClass ob = new IClass();
ob.m1(11);
ob.access(13, 14);
}
}
tes

o/p:
a

****Implemented m1(a)****
nk

The value a:11


Ve

****static private m2(b)****

The value b:13

****NonStatic private m3(c)****

The value c:14

===================================================================
Rule-12 : Interfaces cannot be declared with Blocks and Constructors

Rule-13 : Interface can take the features from another interface using

"extends" keyword.

ii
Diagram:

ath
aip
hM
a tes
nk

ProjectName : Interface_App6
Ve

packages,

p1 : ITest1.java
package p1;
public interface ITest1 {
public abstract void m1(int a);
}
p1 : ITest2.java
package p1;
public interface ITest2 extends ITest1{
public abstract void m2(int b);
}

p1 : IClass.java

ii
package p1;
public class IClass implements ITest2{

ath
public void m1(int a) {
System.out.println("===m1(a)===");
System.out.println("The value a:"+a);
}

aip
public void m2(int b) {
System.out.println("===m2(b)===");
System.out.println("The value b:"+b);
hM
}
}

p2 : DemoInterface6.java(MainClass)
tes

package p2;
import p1.*;
public class DemoInterface6 {
a

public static void main(String[] args) {


nk

IClass ob = new IClass();


ob.m1(11);
ob.m2(12);
Ve

}
}

o/p:

===m1(a)===
The value a:11

===m2(b)===

The value b:12

=================================================================

*imp

ii
Abstract Classes in Java:

ath
=>The classes which are declared with "abstract" keywork are known as

"abstract classes"

aip
=>Abstract Classes are collection of Variables,concrete methods,abstract

methods,Blocks and Constructors


hM
=>Abstract methods in abstract classes must be declared with "abstract"

keyword.

=>Abstract classes cannot be instantiated,which means we cannot create


tes

object for abstract classes

=>These abstract classes are extended to classes using "extends" keyword


a
nk

and the classes are known as extention classes or implementation classes.

=>These extention classes must construct body for all abstract methods
Ve

of abstract classes.

Diagram:
ii
ath
aip
===================================================================
====
hM
a tes
nk
Ve

You might also like