What's the purpose of abstraction?

Viewed 545

Hello I'm a freshman in college. And just did a research about object oriented programming and the language that i'm studying is Kotlin, But i couldn't find a real reason why we need to use abstract class or methods at all.

For example :

abstract class Student(name: String, age: Int) {

    init {
        println("Student name is: $name")
        println("Student age is: $age")
    }

    //non-abstract function
    fun demo() {
        println("Non-abstract function of abstract class")
    }

    //abstract function
    abstract fun func(message: String)
}

class College(name: String, age: Int): Student(name, age) {

    override fun func(message: String) {
        println(message)
    }
}

fun main(args: Array<String>) {
    val obj = College("Chaitanya", 31)
    obj.func("I'm a Blogger")
    obj.demo()
}

reference : https://beginnersbook.com/2019/03/kotlin-abstract-class/

How is this showing only the essential data?

2 Answers

The purpose of abstraction is so you can ignore details that aren't relevant to you. When I'm working with a list of objects, I normally want to add, remove, and iterate. I don't want to worry about resizing an underlying array or adding and removing linked nodes. By using the abstract interface List, I can put those details inside a box and not have to think about them.

Abstract classes are only one kind of abstraction and are usually less important than interfaces. Their main usefulness is to allow you to collect common characteristics in one shared place to reduce duplication. For example, if I have a List, I always need a way to iterate over it, and there's no more efficient way to implement the contains method than to iterate over the list to look for the item. I can put this method in an abstract class, and then the actual implementations of the list (array, linked, something else) only have to provide a way to iterate.

Simply

Actually purpose of abstraction is selecting data from a larger pool to show only the relevant details to the object. It helps to reduce programming complexity and effort

Abstract class is that it allows you to group several related classes as siblings and it helps to reduce the complexity of the design and implementation process of software.

And abstract classes help to describe generic types of behaviors and object-oriented programming class hierarchy. Also it describes sub classes to offer implementation details of the abstract class.

Abstract methods are mostly declared where two or more sub classes are also doing the same thing in different ways through different implementations.Also it extends the same Abstract class and offers different implementations of the abstract methods.

Related