Menu

Showing posts with label Java Programming. Show all posts
Showing posts with label Java Programming. Show all posts

Java Interface examples

In this blog we will see different ways of implement interface in Java.

For example we have two interfaces, IntOne and IntTwo as below.

interface IntOne {
    public String getSrc();
}

interface IntTwo {
    public String getName();
}

1. Simple one and one implements example of interface

class IntOneImpl implements IntOne {
    private String src = "Source Interface One";
    
    public String getSrc() {
        return src;        
    } 
}

class IntTwoImpl implements IntTwo {
    private String name = "Java Interface Examples";
    
    public String getName(){
        return name;
    }
    
}

public class Main
{
public static void main(String[] args) {
IntTwoImpl in = new IntTwoImpl();
IntOneImpl one = new IntOneImpl();
System.out.println(one.getSrc());
System.out.println(in.getName());
}
}

This will work fine and give you output 
Source Interface One
Java Interface Examples

2. Lets see if we implement both the interfaces in a single class and access their methods.


class IntOneImpl implements IntOne {
    private String src = "Source Interface One";
    
    public String getSrc() {
        return src;        
    } 
}

class IntTwoImpl implements IntOne, IntTwo {
    private String name = "Java Interface Examples";
    IntOneImpl intOne = new IntOneImpl();
    @Override
    public String getSrc() {
        return intOne.getSrc();
    }
    
    @Override
    public String getName(){
        return name;
    }
    
}

public class Main
{
public static void main(String[] args) {
IntTwoImpl in = new IntTwoImpl();
System.out.println(in.getSrc());
System.out.println(in.getName());
}
}

Lets see what is happeing in this code.

a. IntOneImpl implements the IntOne and provide definition of getSrc method.

b. IntTwoImpl implements IntOne and IntTwo interfaces and override methods from both the interfaces. 

 


What skills do a person need to learn Java?

Java is one of most usable computer programming lanaguge in the world. A person should have basic analytical and problem solving skills to learn the coding.

We have a very good Java book with name "Getting skilled with Java" that will teach everything about Java progamming, and this book will be good to start for a person whom doesn't have any idea about computer progamming or programming language.  

Getting Skilled with Java book demonstrates setting up the development environment and environment variables, installing JDK, writing programs utilizing Java's key capabilities, troubleshooting, deploying the applications, and bundling them. Using Getting Skilled with Java, a person can easily learn java programming from scratch with realistic applications and problem solving programmes. Below are the links from where you could but a Java Book.


BPB get extra discount: https://in.bpbonline.com/products/getting-skilled-with-java

Amazon: https://www.amazon.in/Getting-Skilled-Java-Programming-Applications/dp/9391392490

Flipkart: https://www.flipkart.com/getting-skilled-java/p/itmfc6f8b196897b?pid=9789391392499&lid=LSTBOK97893913924991JRNDH

Why Java doesn't support multiple inheritance?

Java doesn't support multiple inheritance because of deadlock and ambiguity situation.

A class cannot extend two classes, but a class can implement multiple interfaces. So, we use interfaces to achieve multiple inheritance in Java programming.

e.g. Class C extends A, B { //statements }

The above class declaration is invalid in Java programming. but possible in various other programming languages.

e.g. Class C extends A implements D, F { //codes }

The above class declaration is valid in Java. In the above class declaration, we have inherited class A and implementing two interfaces D and F. This way we can implement multiple interfaces in Java.

Example Class C extends Class A and Class B and both classes A and B have evaluate() method. When class C tries to inherit evaluate method then the compiler will get confused and unable to decide which evaluate method to inherit, either from class A or B. So to remove this ambiguity from the program Java doesn't support multiple inheritance.

Can we create an interface without any method?

Yes, we can create an interface without any method. The interface which doesn't have any method or field declaration is called Marker Interface.

Java has few maker interfaces to handling the special scenarios and let the compiler know to treat those classes in a special way. Below are the example of some marker interfaces.