Sunday 11 July 2021

Android Java to Kotlin migration by example part 5: OOP basics

Today I'll compare Java and Kotlin object-oriented code, with some basic examples of typical things you can do in an object-oriented language.

We'll use the "How to? -> answer in Java -> answer in Kotlin" format.

Like this (let's begin!):


1. How to create an empty class and instantiate it?

Java:
class FellowshipMember{
}

public class MainClass {
public static void main(String[] args) {
FellowshipMember gandalf = new FellowshipMember();
}
}
Kotlin:
package com.mypackage

open class FellowshipMember

fun main(args: Array<String>) {
var gandalf = FellowshipMember()
}

2. How to create a class with one field, and assign something to that field after instantiating?

Java:

class FellowshipMember{
String name;
}

public class MainClass {
public static void main(String[] args){
FellowshipMember gandalf = new FellowshipMember();
gandalf.name = "Gandalf the Grey";
}
}

Kotlin:

package com.mypackage

class FellowshipMember{
var name: String = ""
}

fun main(args: Array<String>) {
var gandalf = FellowshipMember()
gandalf.name = "Gandalf the Grey"
}

3. How to define a public method?

Java:

class FellowshipMember{
String name;

public void presentHimself(){
System.out.println(name);
}
}

public class MainClass {
public static void main(String[] args){
FellowshipMember gandalf = new FellowshipMember();
gandalf.name = "Gandalf the Grey";

gandalf.presentHimself();
}
}

Kotlin:

package com.mypackage

class FellowshipMember{
var name: String = ""

fun presentHimself(){
println(name)
}
}

fun main(args: Array<String>) {
var gandalf = FellowshipMember()
gandalf.name = "Gandalf the Grey"

gandalf.presentHimself();
}

Code execution result:

Gandalf the Grey

Notes:

While the method in Java is package-private by default, the default visibility in Kotlin is public.

4. How to implement simple inheritance (create a subclass?)

Java:

class FellowshipMember{
String name;

public void presentHimself(){
System.out.println(name);
}
}

class RingBearer extends FellowshipMember{
public void putOnTheRing(){
System.out.println("Bad idea");
}
}

public class MainClass {
public static void main(String[] args){
FellowshipMember gandalf = new FellowshipMember();
gandalf.name = "Gandalf the Grey";

gandalf.presentHimself();

RingBearer frodo = new RingBearer();
frodo.name = "Frodo Baggins";

frodo.presentHimself();
frodo.putOnTheRing();
}
}

Kotlin:

package com.mypackage

open class FellowshipMember{
var name: String = ""

fun presentHimself(){
println(name)
}
}

class RingBearer: FellowshipMember() {
fun putOnTheRing(){
println("Bad idea")
}
}

fun main(args: Array<String>) {
var gandalf = FellowshipMember()
gandalf.name = "Gandalf the Grey"

gandalf.presentHimself()

var frodo = RingBearer()
frodo.name = "Frodo Baggins"

frodo.presentHimself()
frodo.putOnTheRing()
}

Code execution result:

Gandalf the Grey
Frodo Baggins
Bad idea

Notes:

Please note the the Java class is non-final by default, so it can be extended just-like-that. Kotlin class, on the other side, is final by default, which means it cannot be extended (inherited from). In order to inherit from it, we have to add the modifier open before its declaration (note that it has appeared in the example above, and was not present in the case no. 2).

5. How to define and implement simple interface?

Java:
import java.util.ArrayList;

interface Fighter{
void drawASword();
}

class FellowshipMember implements Fighter{
@Override
public void drawASword() {
System.out.println("Drawing a sword to protect the Ring");
}
}

class SimpleSoldier implements Fighter{
@Override
public void drawASword() {
System.out.println("Drawing a sword for some reason");
}
}

public class MainClass {
public static void main(String[] args){
FellowshipMember gandalf = new FellowshipMember();
gandalf.drawASword();

SimpleSoldier faramir = new SimpleSoldier();
faramir.drawASword();
}
}
Kotlin:
package com.mypackage


interface Fighter{
fun drawASword()
}

class FellowshipMember: Fighter{
override fun drawASword() {
println("Drawing a sword to protect the Ring")
}
}

class SimpleSoldier: Fighter{
override fun drawASword() {
println("Drawing a sword for some reason")
}
}

fun main(args: Array<String>) {
var gandalf = FellowshipMember()
gandalf.drawASword()

var faramir = SimpleSoldier()
faramir.drawASword()

}
Result:

Drawing a sword to protect the Ring
Drawing a sword for some reason

6. How to use such interface in a polymorphic way?


Please take a look at the main method in codes below:

Java:
import java.util.ArrayList;

interface Fighter{
void drawASword();
}

class FellowshipMember implements Fighter{
@Override
public void drawASword() {
System.out.println("Drawing a sword to protect the Ring");
}
}

class SimpleSoldier implements Fighter{
@Override
public void drawASword() {
System.out.println("Drawing a sword for some reason");
}
}

public class MainClass {
public static void main(String[] args){
FellowshipMember gandalf = new FellowshipMember();
SimpleSoldier faramir = new SimpleSoldier();

ArrayList<Fighter> list = new ArrayList<>();
list.add(gandalf);
list.add(faramir);

for(Fighter guy: list){
guy.drawASword();
}
}
}
Kotlin:
package com.mypackage


interface Fighter{
fun drawASword()
}

class FellowshipMember: Fighter{
override fun drawASword() {
println("Drawing a sword to protect the Ring")
}
}

class SimpleSoldier: Fighter{
override fun drawASword() {
println("Drawing a sword for some reason")
}
}


fun main() {
var gandalf = FellowshipMember()
var faramir = SimpleSoldier()

val list = mutableListOf<Fighter>()

list.add(gandalf)
list.add(faramir)

for(guy in list){
guy.drawASword()
}
}

Result:

Drawing a sword to protect the Ring
Drawing a sword for some reason

That's it for now. Of course I skipped some important concepts (for instance - as far as I know, there are no static methods/fields in Kotlin and I'm not sure I understand what replaces them)... mostly because I'm learning them myself ;)

I guess I'll stick to this question-answer format for some time, as I usually don't have much time (and energy...) to elaborate, yet it should be still quite clear.

No comments:

Post a Comment

Python crash course part 10: inheritance and polymorphism

In the last part we've shown how to create and use a class in Python. Today we're going to talk about inheritance: wchich means cre...