Category Archives: Architecture Design

Actually, about polymorphism in OOP

polymorphism
What about polymorphism?
Did you know polymorphism works with interfaces only, but not with inheritance?
Look here.

BaseClass.java

public class BaseClass {

    private String var = "i'm in base!";

    public void method() {
        System.out.println("Hello from base.");
    }

    public String getVar() {
        return var;
    }
}

ExtClass.java

public class ExtClass extends BaseClass {

    private String var = "i'm in EXT!!!";

    @Override
    public void method() {
        System.out.println("Hello from EXT!!!");
    }

    @Override
    public String getVar() {
        return var;
    }
}

Test1.java

public class Test1 {

    public static void main(String[] args) {

        System.out.println("# BaseClass:");
        System.out.println("------------");
        BaseClass baseClass = new BaseClass();
        System.out.println("var = " + baseClass.getVar());
        baseClass.method();
        System.out.println("");

        System.out.println("# ExtClass:");
        System.out.println("-----------");
        ExtClass extClass = new ExtClass();
        System.out.println("var = " + extClass.getVar());
        extClass.method();
        System.out.println("");

        System.out.println("# Cast ExtClass to BaseClass:");
        System.out.println("-----------------------------");
        BaseClass baseClassCasted = (BaseClass) extClass;
        System.out.println("var = " + baseClassCasted.getVar());
        baseClassCasted.method();
        System.out.println("");
    }
}

The result:

# BaseClass:
------------
var = i'm in base!
Hello from base.

# ExtClass:
-----------
var = i'm in EXT!!!
Hello from EXT!!!

# Cast ExtClass to BaseClass:
-----------------------------
var = i'm in EXT!!!
Hello from EXT!!!

But how we can access to the base class?
And if you really have to invoke method from casted class, use implementation!
Meth.java

public interface Meth {

    void method();

    String getVar();
}

BaseClass.java

public class BaseClass implements Meth {

    private String var = "i'm in base!";

    @Override
    public void method() {
        System.out.println("Hello from base.");
    }

    @Override
    public String getVar() {
        return var;
    }
}

ExtClass.java

    private String var = "i'm in EXT!!!";

    @Override
    public void method() {
        System.out.println("Hello from EXT!!!");
    }

    @Override
    public String getVar() {
        return var;
    }

Test1.java

        System.out.println("# interface from BaseClass:");
        System.out.println("-----------");
        Meth baseMeth = new BaseClass();
        System.out.println("var = " + baseMeth.getVar());
        baseMeth.method();
        System.out.println("");

        System.out.println("# interface from ExtClass:");
        System.out.println("-----------");
        Meth extMeth = new ExtClass();
        System.out.println("var = " + extMeth.getVar());
        extMeth.method();
        System.out.println("");

And here we have:

# interface from BaseClass:
-----------
var = i'm in base!
Hello from base.

# interface from ExtClass:
-----------
var = i'm in EXT!!!
Hello from EXT!!!

And now all as you expect!

PS. All code in java. Have fun!

Digital Signature explanation

The security is very important concept today for all type of systems. So, we have to know how to protect your data from theft, hijacking, etc.
Encryption is reversible conversion of data to hide payload from other reading.

The are two types of encryption algorithms:
1. Symmetric Encryption (Private-key (or secret-key) cryptography).
priv-key
Uses the same key for encryption and decryption of message.

2. Asymmetric Encryption (Public-key cryptography).
pub-key
Uses public key for encryption and private key for decryption of message.

 

The payload messages usually use digital signature for protection. Explanation of how the digital signature works below in a few steps:

1. Angela wants to send a message to Hugo.
1

 

2. Angela request a certificate from Certification Center.
2

 

3. Certification Center sends a certificate with public-private key pair.
3

 

4. Angela calculates hash of message. Encrypts obtained hash with private key – this is a Digital Signature! And attach DS to message.
4

 

5. Angela sends DS, Message and certificate to Hugo.
5

 

6. Hugo decrypts digital signature with public key. Take a hash from Message. Then, he checks decrypted value and hash for equality. If values are equal – message is valid and it can be read, otherwise – message is invalid!
6

 

See also,
https://en.wikipedia.org/wiki/Symmetric-key_algorithm
https://en.wikipedia.org/wiki/Public-key_cryptography
http://en.wikipedia.org/wiki/Digital_signature
https://en.wikipedia.org/wiki/Hash_function

http://en.kioskea.net/contents/130-private-key-or-secret-key-cryptography
http://en.kioskea.net/contents/131-public-key-systems