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?
class FellowshipMember{
}
public class MainClass {
public static void main(String[] args) {
FellowshipMember gandalf = new FellowshipMember();
}
}
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:
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:
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?
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();
}
}
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()
}
6. How to use such interface in a polymorphic way?
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();
}
}
}
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()
}
}
No comments:
Post a Comment