You must know about Java SE 8 Interface Default and Static ClickMe
Interface is a group of related methods with empty bodies along with below, And most impotent What to do, but don't know how to do?
Interface is a group of related methods with empty bodies along with below, And most impotent What to do, but don't know how to do?
Advantage
Members of a Java interface are public by
default
Variables declared in a Java interface is
by default final
Methods of a Java interface are implicitly
abstract and cannot have implementations
Java interface should be implemented using keyword “implements”
An interface can extend another Java interface only
A Java class can implement multiple interfaces
The purpose of interfaces is abstraction, or decoupling from
implementation
By interface, we can support the
functionality of multiple inheritance
It can be used to achieve loose coupling
Java Interface also represents IS-A relationship
Java Interface also represents IS-A relationship
Dis-Advantage
We can’t add additional method later in Interface, if so
then need to implement in all implementation class
Writing on basis of below software's:
1. JDK1.6 or +2. Eclipse
3. Windows OS
4. Basic knowledge of java
Verify the JDK installation
Open the command prompt and verify the java installation
Type the
C:\>javac -version
javac 1.7.0_25
C:\>java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) Client VM (build 23.25-b01, mixed mode, sharing)
Now java installation got verified, as installed JDK1.7.0_25
Verify the Eclipse installation
Go the the installation directory of eclipse and click on the eclipse.exe
Create the new java project in Eclipse
Open the eclipse
File
New
Java project
Type the project name: Interface_In_Java_Programming
Next
Finish
Java project created in eclipse with directory structure:
Interface_In_Java_Programming
- src
- JRE System Library
How to create Interface in eclipse?
Right click on the src folder under Interface_In_Java_Programming projectNew
Interface
Type the package name: com.atozexamples
Type the name: Calculator
Finish
Calculator.java
package com.atozexamples;
public interface Calculator {
}
Note: Above code are also called tag interface (also called a marker interface) in java, good examples is java.io.Serializable.
public interface Serializable {
}
More about Calculator:
Keyword: interface
We can't user interface as name or package name.
e.g:
1. com.interface.atzexample
if we put in package name then we will get error:
"Invalid package name. 'interface' is not a valid Java identifier"
2. public interface interface {}
if we put in interface name then we will get:
"Syntax error on token "interface", Identifier expected"
Added fields as a and b, and addition method.
package com.atozexamples;
public interface Calculator {
int a = 10;
int b = 20;
int addition(int a, int b);
}
Above Calculator and below Calculator interface is same.
package com.atozexamples;
public interface Calculator {
public static final int a = 10;
public static final int b = 20;
public abstract int addition(int a, int b);
}
if we not include the "public static final" even though javac (compiler) understand and consider filed in interface is public static final.
And method are in interface by default public abstract.
By default method in interface "public abstract".
By default field in interface "public static final".
package com.atozexamples;
public interface Calculator {
public static final int a = 10;
public static final int b = 20;
int c;
public abstract int addition(int a, int b);
}
if we not initialized the field in interface will get "The blank final field c may not have been initialized".
Multiple inheritance in Java by interface
Add two more interface in Interface_In_Java_Programming.
Printable.java
package com.atozexamples;
public interface Printable {
void print();
}
Showable.java
package com.atozexamples;
public interface Showable {
void display();
}
Now add the SimpleCalculator class.
Right click on the src
New
Class
class name: SimpleCalculator
Finish
SimpleCalculator.java
package com.atozexamples;
public class SimpleCalculator {
public SimpleCalculator() {
}
public static void main(String[] args) {
}
}
SimpleCalculator class implements Calculator, Printable, Showable interface
package com.atozexamples;
public class SimpleCalculator implements Calculator,
Printable, Showable {
public SimpleCalculator() {
}
@Override
public int addition(int a, int b) {
return a + b;
}
@Override
public void print() {
addition(10,
20);
}
@Override
public void display() {
addition(10,
20);
}
public static void main(String[] args) {
SimpleCalculator
obj = new SimpleCalculator();
obj.addition(10, 20);
obj.display();
obj.print();
}
}
Interface inheritance
Calculator interface extends Printable, Showable
package com.atozexamples;
public interface Calculator extends Printable, Showable
{
public static final int a = 10;
public static final int b = 20;
public abstract int addition(int a, int b);
}
Then SimpleCalculator will only implements Calculator interface
package com.atozexamples;
public class SimpleCalculator implements Calculator {
public SimpleCalculator() {
}
@Override
public int addition(int a, int b) {
return a + b;
}
@Override
public void print() {
addition(10,
20);
}
@Override
public void display() {
addition(10,
20);
}
public static void main(String[] args) {
SimpleCalculator
obj = new SimpleCalculator();
obj.addition(10, 20);
obj.display();
obj.print();
}
}
Nested Interface in Java
Calculator interface have Printable interface as nested and SimpleCalculator class implementing the both.
package com.atozexamples;
public class SimpleCalculator implements Calculator,
Calculator.Printable {
public SimpleCalculator() {
}
@Override
public void print() {
// Print
}
@Override
public int addition(int a, int b) {
return a + b;
}
public static void main(String[] args) {
SimpleCalculator
obj = new SimpleCalculator();
obj.addition(10, 20);
obj.print();
}
}
Happy learning and implementation!!!
World's #1 Repository Manager Single source of truth for all of your components, binaries, and build artifacts. Efficiently distribute parts and containers to developers. Deployed at more than 100,000 organizations globally. https://www.sonatype.com/product-nexu...
ReplyDelete1. About Nexus
2. System Requirements for Nexus
3. Nexus Installation in Linux Part 1
4. Nexus Installation in Linux Part 2
5. Nexus Installation in Windows 10 Part 1
6. Nexus Installation in Windows 10 Part 2
7. Post Nexus Installation checks
8. How to upload file from Nexus GUI?
9. How to upload file in Nexus by CURL?
10. Apache maven, Nexus integration and uploading jar file
11. Nexus Dashboard Tabs & Links in details
Python Programming Full Course Python Programming Full Course
Sonatype Nexus Repository Manager Full Course Sonatype Nexus Repository Manager Full Course
Apache Maven Full Course Apache Maven Full Course
Git Full Course Git Full Course