+ 1

Purpose of Abstract?

If a user is defining the constructor in the definition of the class, what is the purpose of abstract. For example why use the abstract Component? abstract class Component(width: Int, height: Int) { abstract fun doStuff() } class Button(width: Int, height: Int): Component(width, height) { fun tap() { println("Button was tapped.") } override fun doStuff() { println("Thank you for pushing the button.") } }

17th Dec 2022, 9:55 PM
William Owens
William Owens - avatar
5 Respostas
+ 5
Abstract class is all about inheritance. But in this special situation, the abstract parent class cannot be instantiated, because it does not contain all the details required for the object to work. The subclasses finish the implementation and make it specialized in some way, keeping the commonalities across all subclasses.
18th Dec 2022, 2:19 PM
Tibor Santa
Tibor Santa - avatar
+ 4
This is one of the nicest buttons that I have ever seen. :) The abstract class is like an instruction: It tells the developer what all derived classes must implement at minimum to be a Component. If you want to make different component classes they will share some similarities. An abstract class as parent class (Component) may help to organize and keep these similarities – which makes it easier for the developer as well as the user.
17th Dec 2022, 10:05 PM
Lisa
Lisa - avatar
+ 2
William Owens maybe a practical example will spark some reason for using abstract classes. Suppose we design a game of Tic Tac Toe. We have a playing field with 3 X 3 squares. Each square can be empty or X or O. But how can we represent the whole grid, which is a collection of 9 squares? We can create an abstract class Tile. The different types of tiles are inheriting the general properties (size, shape) from this class, but the EmptyTile will have no symbol drawn on it, XTile and OTile will have different symbols. So the playing field can be an Array<Array<Tile>> type. But each element would be a specific subtype of Tile, initially EmptyTile, and as the game progresses, the instances inside the array can change to a different subtype. There are of course other ways to represent the same concept, especially in Kotlin it might be better to use a sealed class, or you can use enum, or just a single class with a different constructor argument to mark the differences. But this is another story.
18th Dec 2022, 3:05 AM
Tibor Santa
Tibor Santa - avatar
+ 1
@Lisa Thank you, I guess I just do not understand the reason for double work it which is seems to me. abstract class Thingy(property1: int, property 2: int) { abstract fun doStuff() } class typeThingy(property1: int, property2: int): Thingy(property1, property2) { override fun doStuff(){ println("Doing stuff.") } } in stead of ---- class typeThingy(property1: int, propert2: int) { fun doStuff() { println("Doing Stuff") } } I cannot take credit for the button it belongs to sololearn... :-)
18th Dec 2022, 12:36 AM
William Owens
William Owens - avatar
0
Tibor Santa & Lisa Thank you for looking at the question and giving great input. Would inheritance not work in both cases better?
18th Dec 2022, 1:13 PM
William Owens
William Owens - avatar