4

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?

  • 3
    Abstraction != abstract classes. Two different concepts – Mars Oct 02 '19 at 04:07
  • 1
  • Thank you, i'm totally confused when reading articles on the internet could you please help distinguish even more. thanks – Charlie Jackie Oct 02 '19 at 04:09
  • And on a different point, the class names here are really weird. A college wouldn't normally be thought of as an extension or sub-type of of student. A better example would be Student -> Person, College -> Student. Then you could also add Salaryman(name: String, age: Int): Person(name, age) – Mars Oct 02 '19 at 04:10
  • In essence, abstraction is the idea that you don't want to show more than you need to. It's the reason behind public vs private methods/properties – Mars Oct 02 '19 at 04:12
  • Abstract classes are different. Basically, it's a base class that lays out a lot of features--common things that every class *extending* it will also have – Mars Oct 02 '19 at 04:12
  • 2
    The usual example for abstract classes is abstract class Shape, class Circle:Shape, class Square:Shape. All shapes have an area and a perimeter, but the way you calculate each is different for circles and squares – Mars Oct 02 '19 at 04:14
  • So, you define an abstract class Shape that has two floats, area and perimeter. And you define abstract methods "CalcArea()" and "CalcPerim." These two functions are meaningless by themselves. If I tell you to calculate the area of a shape (and don't tell you what kind of shape), you won't know what to do – Mars Oct 02 '19 at 04:15
  • So, the child classes *inherit* the area and perimeter floats, and then they *override* the Calc functions – Mars Oct 02 '19 at 04:16
  • Possible duplicate of [Interface with default methods vs Abstract class in Java 8](https://stackoverflow.com/questions/19998454/interface-with-default-methods-vs-abstract-class-in-java-8) – Ashwin Subramanya Oct 02 '19 at 05:00
  • @Mars There are some standard examples like that which are supposed to show ideas behind abstraction and/or inheritance in general. While they **can** be OK to convey some basic ideas, concepts and last but not least: the syntax that is used to *implement* these concepts, one has to be careful: Even experienced programmers can argue about whether it's `Rectangle extends Square` or `Square extends Rectangle` (and in doubt, they have to say: Neither is correct... or ... it depends...). – Marco13 Oct 02 '19 at 12:50
  • @Marco13: That's easy, actually. An *immutable* square IS-AN immutable rectangle. A *write-only* rectangle IS-A write-only square. Mutable squares and rectangles are not subtypes of each other and vice versa. – Jörg W Mittag Oct 02 '19 at 13:23
  • @JörgWMittag Yes, immutabiltiy circumvents the problems that otherwise arise in this case from Liskov substitution (Although I'd have to become reeeally creative in order to make a case for something like a "write-only rectangle" ;-)) – Marco13 Oct 02 '19 at 13:32
  • @Marco13: Yeah, write-only rectangles are somewhat obscure. Write-only collections, OTOH are wide-spread, because that's essentially what an output stream or a logger is. – Jörg W Mittag Oct 02 '19 at 15:20

2 Answers2

2

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.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

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.

Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19