w3resource

Kotlin Object-Oriented Programming: Exercises, Practice, Solutions

Kotlin Object-oriented programming Exercises [ 7 exercises with solution ]

[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

1. Write a Kotlin object-oriented program that creates a base class Shape and derives subclasses Circle, Rectangle, and Triangle. Implement appropriate methods in each class and demonstrate polymorphism.

Click me to see the solution

2. Write a Kotlin object-oriented program that implements a singleton pattern for a class Logger to provide logging functionality throughout the application.

Click me to see the solution

3. Write a Kotlin object-oriented program that creates a sealed class Result with subclasses Success and Error to represent the result of an operation. Use pattern matching to handle different result types.

Click me to see the solution

4. Write a Kotlin object-oriented program that implements the decorator pattern by creating a base class Component and decorator classes BoldDecorator and ItalicDecorator to modify the behavior of the component.

Click me to see the solution

5. Write a Kotlin object-oriented program that implements the factory method pattern by creating an abstract class Animal with subclasses Tiger and Lion. Use a factory class to create instances of animals based on user input.

Click me to see the solution

6. Write a Kotlin object-oriented program that creates an interface Observable with methods subscribe and unsubscribe. Implement it in a class Publisher to allow objects to subscribe and unsubscribe from events.

Click me to see the solution

7. Write a Kotlin object-oriented program that implements the composite pattern by creating a base class Component and composite classes CompositeComponent and BranchComponent to represent a hierarchical structure.

Click me to see the solution

More to Come !

* To run the code mouse over on Result panel and click on 'RERUN' button.*

Kotlin Editor:


Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/kotlin-exercises/oop/index.php