Covariance and Contravariance

In C#, covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments. Covariance preserves assignment compatibility and contravariance reverses it.

A generic interface or delegate is called variant if its generic parameters are declared covariant or contravariant. C# enables you to create your own variant interfaces and delegates.

In .NET Framework 4 or newer C# supports covariance and contravariance in generic interfaces and delegates and allows for implicit conversion of generic type parameters.


Continue reading

JavaScript Prototype Inheritance with IIFE in ES5

I try demonstrate inheritance with immediately invokable function expression(IIFE) and prototype in the following code.

This sample code is written in different concepts you should read the following posts as well.

The following code demonstrate classic inheritance with JavaScript.

Same code as shown in the above, but I added a new function named inherit in global Function.prototype so that we don't need to write same code for inheritance. It looks like Employee.inherits(Person); or Manager.inherits(Employee);

Notice that although Employee and Manager functions doesn't have inherits function, it works due to global Function.prototype.inherits

JavaScript Class, Inheritance, Override in ES6

ES6, also known as ECMAScript2015, introduced classes. Notice that classes are used in order to use inheritance.

This sample code is written in different concepts you should read the following posts as well.

Presumably this demo looks more friendly to you 🙂 actually I think so. But don't forget that no magic here still, this code is also transpiled(or compiled) to ES5 for browser compatibility.

JavaScript Prototype Inheritance with function declaration and expression in ES5

I hope that somebody who forgets this kinds of basic but very important information will find this post very useful. I have demonstrated different cases of inheritance(extension) in JavaScript so that JavaScript developers can achieve OOP.

This sample code is written in different concepts you should read the following posts as well.

I try to demonstrate inheritance with ES5 in this demo. Notice that I have used function declaration and function expression in the following code examples.

After a function is created, it looks like as following picture in the run-time.

These three pictures below show that every function created like Vehicle has common fields. Notice that screenshots show only function fields.

Inheritance with function expression

functions like function Person(), function Employee() and function Manager() don't have any parameters but we can read value of parameters from arguments. arguments is a built-in function parameter.

In order to pass arguments other functions I have used function apply method.

I updated above example code as shown below. The following code demonstrate parameterised functions with function call method.

The only difference between function call and apply is that call only accepts parameters one by one but apply accepts parameters as an array.

Person.call(this, personId, firstName, lastName, age, gender);
Person.apply(this, arguments);

Inheritance with function declaration(statement)

JavaScript Simple Http Server with Pipeline using Node.js

This demo is similar to https://kenanhancer.com/2018/01/18/javascript-pipeline-example-with-es6/
The difference between demo of previous post and this post is the object passed to the pipeline is builded by a request that client made. So that every http request will have its own middlewares pipeline.

https://gist.github.com/kenanhancer/ba63562d3171fe3fa4aa021df2663c3e

JavaScript Pipeline Demo with ES6

I want to demonstrate Chain of Responsibility Behavioral Pattern in this demo. Notice that I have three methods named as middleware1(), middleware2() and middleware3() which includes business logic. Modern web frameworks like Express for Node.js work in this way. The reason we need this development is to seperate concerns so that we can implement seperation of concerns(SoC) design principle.

Java – Difference between equals() method and == operator

"==" operator compares 2 objects memory reference. Now, lets have a look at the simple example below:

Example 1:

String str1 = "test";

String str2 = "test";

if (str1 == str2) {
System.out.println("str1 == str2 is TRUE");
} else {
System.out.println("str1 == str2 is FALSE");
}

Output:

str1 == str2 is TRUE

Example 2:

String str1 = new String("test");

String str2 = new String("test");

if (str1 == str2) {
System.out.println("str1 == str2 is TRUE");
} else {
System.out.println("str1 == str2 is FALSE");
}

Output:

str1 == str2 is FALSE

In the code above, Outputs of Example 1 and Example 2 are different. Because, when you create String, JVM searches that literal in String pool, if it matches, same reference will be given to that new String. So, Example 1 output is TRUE.

In Example 2, the output is FALSE. Because, 2 objects refer to different memory location.

Continue reading

Java Method Reference

Method reference is supported in Java 8. A method reference is described using :: (double colon) symbol.

Types of Method Reference
There are four types of method references:

  1. A method reference to a static method.
  2. A method reference to an instance method of an object of a particular type.
  3. A method reference to an instance method of an existing object.
  4. A method reference to a constructor.

Static Method Reference

Notice that below code between System.out and println, the :: operator is used instead of the . operator. And we don't pass arguments to the method reference. So, you can refer to static method defined in the class with interfaces that contain only one abstract method in addition to one or more default or static methods.

Consumer<?> is a functional interface. Lambda expression and Static method references that implement Consumer<String> functional interface are passed to the accept() method to be executed. Actually, Lambda expression  can be replaced with Method reference.

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class Program {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("name1", "name2", "name3", "name4");


        //1. way: using Anonymous Inner Class
        Consumer<String> action1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

        names.forEach(action1);

        System.out.println("*******************");

        //2. way: using Lambda Expression
        Consumer<String> action2 = (x) -> System.out.println(x);

        names.forEach(action2);

        System.out.println("*******************");


        //3. way: using Static Method Reference
        Consumer<String> action3 = System.out::println;

        names.forEach(action3);

        System.out.println("*******************");


        //4. way: pass Lambda Expression as argument
        names.forEach((x) -> System.out.println(x));


        System.out.println("*******************");


        //5. way: pass Static Method Reference as argument
        names.forEach(System.out::println);

    }
}

Output:

name1
name2
name3
name4
*******************
name1
name2
name3
name4
*******************
name1
name2
name3
name4
*******************
name1
name2
name3
name4

Continue reading