Git Product home page Git Product logo

arv's Introduction

arv

Inheritance

This is how you declare that a class in kotlin kan be Inheritantet from with the "open" modifier

    override fun toString(): String {
        return "ID: "+id.toString() +" Name: "+ name+ " description: "+description+" Living Status: "+alive+ "\n"
    }
    open val description: String
        get() = "This animal is called a $name It's age is $age"
    open fun kill() { alive = false }
    }

Overriding Methods

the way to override methods from the superclass is also with the open modifier as seen above. If the superclass methods does not have a open modifier the subclass can not override the given method.

Calling from the superclass

a way you can call methods from the superclass in the subclass is with the keyword "super" as seen in the code below

open class Herbivore(val age:Int) {
    open fun eat(){println ("i eat plants")}

}

interface Carnivore{
    fun eat() { println("i eat meat") }

}

open class Omnivore(age:Int,name: String) : Herbivore(age), Carnivore {
    override fun eat() {
        super<Herbivore>.eat() // call to Herbivore.eat()
        super<Carnivore>.eat() // call to Carnivore.eat()
    }
}

Overriding with inheritance and interfaces

it's fine to inherit from both inheritance and a interface but the problem comes when both of them have a method with the same name this can be fixed with the useing super qualified by the supertype name in angle brackets "super<>" as seen above.

Abstract Class

Abstract classes may contain both abstract and non-abstract properties and methods. An abstract class is useless on its own because you cannot create objects from it. Any subclass that extends the abstract class must implement all of its abstract methods and properties, or the subclass should also be declared as abstract Abstract classes are also open by default.

arv's People

Contributors

zenzus avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.