Java Covariant Return Type = Method Overriding

What Is Java Covariant Type?

Howdy Mowdy People? Have you ever override a method by changing its return type? Well for y’all that is already a pro in Coding may know this type of overriding, but for those who is still  a green horn in coding, just like me :’D maybe. This kind of method overriding is called Java Covariant Return Type, which is not available until Java5 came out.

Here ok, let me just show you a simple presentation of this Covariant Type :

Continue reading Java Covariant Return Type = Method Overriding

Java SE 8 Lambda Expressions

What Is Java SE 8 Lambda expressions?

There is a lot of new and important feature of Java and one of them is Java SE 8 Lambda Expressions.  This feature (lambda Expressions) will provide one clear and concise way to represent interface method using an expression.

The method is very useful in the collection library in so much way. Before the lambda expression, using an anonymous inner class was the only way and option for a coder to implement the method.

The new Java SE 8 feature Lambda expression provides coders an implementation of a functional interface. Functional Interface is an interface that has only one abstract method.

Java provides an annotations @FunctionalInterface, which is used to declare an interface as a functional interface. 

Reason To Lambda Expressions:

  1. To do less coding.
  2. To have the implementation of Functional Interface 

Lambda Expression Syntax :

(argument-list) -> {body}  

Below I will tell you the components that built Lambda Expression:

  1. Body: Body Components contains statements  .etc for the lambda expression.
  2. Argument-list: An Argument-list you could fill it empty or filled as well.
  3. Arrow-token: The arrow token functionality is to link the arguments-list and body of statements.


Here is an Example without Lambda Expression in Interface:

    interface Colourable{
public void colour();
}
public class LambdaExpressionExample {
public static void main(String[] args) {
int width=7;

//without lambda we used anonymous inner class
            Colourable c=new Colourable(){
public void draw(){System.out.println(“Drawing “+width);}
};
c.colour();
}
}  


Then compare it to the one that uses lambda Expression:

@FunctionalInterface  //It’s optional
interface Colourable{
public void colour();
}

public class LambdaExpressionExample {
public static void main(String[] args) {
int width=7;

//with lambda  (less coding)
Colourable c2=()->{
System.out.println(“Drawing “+width);
};
c2.colour();
}
}
 
Conclusions :

  •