Start learning free

LESSON 180 OF 348 · OBJECT-ORIENTED PROGRAMMING

Defining a Class in Kotlin

Lesson Read the full explanation

TL ; DR:

  • A class is a blueprint for creating objects. It groups together related data and behavior under a single name.


  • In Java, you define a class with the class keyword, followed by the class name and a pair of curly braces:

    class Dog {
    
    }
    

    The body of the class can be empty — that's still a valid class. We'll add data and behavior to it in the next lessons.



Full lesson:

So far, you've worked with built-in types like int, String, and arrays. But what if you want to model something the language doesn't have a type for — like a Dog, a BankAccount, or a Game?

That's what classes are for. A class lets you define your own type by listing what data it holds and what actions it can perform.


Why classes?

Imagine you're writing a program about dogs. Each dog has a name, an age, and can bark. Without classes, you'd have to track each dog's name in one variable, age in another, and write a separate bark() function for each. That gets messy fast.

With a class, you bundle all of that into one neat package called Dog:

class Dog {
    // We'll add fields (data) and methods (behavior) here in upcoming lessons.
}

The syntax

Defining an empty class in Java looks like this:

class ClassName {
}

Three things to notice:

  • The keyword class tells Java you're defining a new type.

  • ClassName is the name you choose. By convention, Java class names use PascalCase — capitalized words with no underscores: Dog, BankAccount, HighScore.

  • The curly braces { } hold the body of the class. They can be empty for now.


Your task

Define an empty class called Dog. The pre-filled code in mainFunction will create an instance of your class and print a confirmation message. If you define the class correctly, the program will compile and print:

Defined the Dog class!
STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · OBJECT-ORIENTED PROGRAMMING

    Defining A Class in Kotlin, explained

    What a class is

    Kotlin comes with types such as Int, String and Array. A class lets you add your own: a Dog, a BankAccount, a Game. A class is a blueprint. It describes what data a thing holds and what it can do, and later lessons make real objects from it.

    Without classes, a program about dogs would need one variable per dog name, another per age, and loose functions for every action. A class bundles all of that under a single name.

    The syntax

    The smallest class is a keyword, a name and a pair of braces:

    class Dog {
    }
    
    • class starts a class declaration.
    • Dog is the name you choose. By convention Kotlin class names use PascalCase: each word capitalized, no underscores, as in Dog, BankAccount or HighScore.
    • The braces hold the class body. An empty body is still a valid class, and Kotlin lets you drop the braces entirely when there is nothing inside: class Dog means the same thing.

    What goes inside a class

    An empty class is a starting point. A class body can hold properties, the data each object carries, such as a name and an age, and functions, the actions an object can perform, such as barking. It can also have constructors that set up a new object. Later lessons add each of these to Dog. For now the class is only a name for a new kind of thing, and an object made from it carries no data and has no behavior of its own.

    Where the class goes

    A class can be declared at the top level of a file, next to other classes and functions, which is where the lesson puts Dog. Kotlin does not require a class to appear before the code that uses it, so Dog could equally sit below Solution. Placing it above is simply easier to read.

    A class is also final by default: no other class can extend it unless you mark it open. That does not matter for this lesson, but it is a Kotlin habit worth knowing early.

    The exercise

    The starter code already has a Solution class whose mainFunction uses a Dog:

    class Solution {
        fun mainFunction() {
            val dog = Dog()
            println("Defined the Dog class!")
        }
    }
    

    The line Dog() creates an instance of Dog. It works only if a class with exactly that name exists. Without it the code fails to compile with an error such as unresolved reference 'Dog', and nothing runs.

    The single step is to declare the empty class above Solution. An empty class has a default constructor with no parameters, so Dog() is valid with no extra code. The finished lesson:

    class Dog {
    }
    
    class Solution {
        fun mainFunction() {
            val dog = Dog()
            println("Defined the Dog class!")
        }
    }
    

    Running mainFunction prints Defined the Dog class!. The message is printed by the lesson's own println; the class is what makes the line above it compile.

    Common mistakes

    • Wrong capitalization. Kotlin names are case-sensitive. A class called dog or DOG does not satisfy Dog(), which then fails with unresolved reference.
    • Forgetting class. Without the keyword, Dog { } is not a declaration, and the code fails to compile.
    • A missing closing brace. The compiler reaches the end of the file while still inside the body and reports an error.
    • Expecting the class to do something by itself. Declaring a class prints nothing and creates no dog. Only a call such as Dog() creates an object.

    Key takeaways

    • A class is a blueprint that defines your own type.
    • Declare it with class, a PascalCase name and braces; the body may be empty or omitted.
    • Every class gets a default no-argument constructor unless you write your own, so Dog() works right away.
    • Declaration order at the top level does not matter in Kotlin, but placing the class first reads best.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free