Showing posts with label हिंदी. Show all posts
Showing posts with label हिंदी. Show all posts

Java SE 8 or JDK1.8 Interface features

Added/upgraded default/static methods


What was the issue in java Interface in earlier version?

1  All abstract methods need to implement in implementation class.
        
   If we add any additional abstract method in existing interface then  need to implement in implementation classes, everywhere else  compilation issue
3   
   All binary and dependency need to recompile and again build for it 

All issue got resolved by adding the default/static methods in Interface


Will see only by one examples.

Writing on basis of below software's

1. JDK1.8
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.8.0_74

C:\>java -version

java version "1.8.0_74"

Now java installation got verified, as installed JDK1.8.0_74

Verify the Eclipse installation

Go the the installation directory of eclipse and click on the eclipse.exe
e.g C:\eclipse\eclipse-java-mars-1-win32\eclipse\eclipse.exe


Create the new java project in Eclipse


Open the eclipse

File
New
Java project
Type the project name: Interface_In_Java_Programming_JavaSE_8_JDK1.8
Next
Finish

Java project created in eclipse with directory structure:

 Interface_In_Java_Programming_JavaSE_8_JDK1.8
     - src
     - JRE System Library


How to create Interface in eclipse? 

Right click on the src folder under Interface_In_Java_Programming_JavaSE_8_JDK1.8
project
New
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 {
}

Calculator interface with default and static method with declaration and implementation.


Calculator.java


package com.atozexamples.jdk8;

public interface Calculator {

       // Abstract method, This method need to
       // implement in implementation calss
       void display();

       // Abstract method, This method need
       // to implement in implementation calss
       void print();

       /**
        * Default method in interface with implementation, 
        * not required to
        * implement in implementation class
        */
       default int addition(int a, int b) {
         System.out.println("Calculator interface Addition result "       + a + b);
              return a + b;
       }

       /**
        * static method in interface with implementation, 
        * not required to implement
        * in implementation class
        */

       static int sub(int a, int b) {
              int c = a - b;
            System.out.println("Calculator interface sub result " 
            + c);
              return c;
       }
}


SimpleCalculator class implements Calculator interface and override the display() and print() methods where as calls addition() default method and Calculator.sub() static method.

SimpleCalculator.java

package com.atozexamples.jdk8;

public class SimpleCalculator implements Calculator {

       public SimpleCalculator() {
       }

       @Override
       public void display() {
              System.out.println("Displaying the imp cls!");
       }

       @Override
       public void print() {
              System.out.println("Printing the impl cls");
       }

       public static void main(String[] args) {
              SimpleCalculator obj = new SimpleCalculator();
              // For calling the default method
              obj.addition(10, 20);
              obj.display();
              obj.print();

              // Calling the static method defined in interface by
              // interface name instead of object class
              Calculator.sub(10, 20);
              obj.display();
              obj.print();
       }
}

We can't call obj.sub(,) method wont be possible.


Video:

In English





In Hindi




Happy learning and implementation

First Java Project in Eclipse with Hello World Examples By Main Method

In this tutorial we will learn about how to create simple java project in eclipse and respective configuration.

What we need before start the code, I'm using as mention below and already installed in my system:
1. JDK1.6 or +
2. Eclipse
3. Windows OS
4. Basic knowledge of java

Let's start:


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
e.g C:\eclipse\eclipse-java-mars-1-win32\eclipse\eclipse.exe

Write the HelloWorld.java file in eclipse

Right click on the src folder
New
Class
Type the class file name: HelloWorld
Type the package name: com.atozexamples
Finish

package com.atozexamples;

public class HelloWorld {

public HelloWorld() {

}

public static void main(String[] args) {

System.out.println("Hello world examples!!!");

}
}



First Java Project in Eclipse with Hello World Examples By Main Method



Video:

In English


In Hindi

हिंदी




This is all about the, how to write the first java project in eclipse.