Abstract class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Abstract class

Abstract class. After learning in solo I can't understand what is abstract, can u explain me about it? Maybe some examples where im nessesary to use abstract class.

3rd Jun 2017, 9:46 PM
Mr. Nobody
Mr. Nobody - avatar
5 Answers
+ 4
An abstract class cannot be implemented and it works like a "kind class". For example. There is a lot kinds of birds, and if anyone asks you to draw a bird, you probably will draw one of this kinds of birds. That's because bird is not an individual, but a group with same caracteristcs. So, in OOP, bird will be an abstract class and duck a class that inherit atributes and methods from bird. Now you can create a object duck with atributes you want, like weight, color, and so on. You will probably use more abstract class when you are creating a "group class". Do I answered your question?
3rd Jun 2017, 11:21 PM
Ivan Luiz
Ivan Luiz - avatar
+ 3
Let's say you're working on a project and you think of some functions or methods that you would need, but haven't thought about exactly what code would go in it. So a simple solution would be to create a separate class that contains all these methods but doesn't specify any code inside the method itself. These methods are called abstract methods; and the class that contains more than 1 abstract method is called an abstract class
4th Jun 2017, 4:27 AM
Nihar Raote
Nihar Raote - avatar
+ 2
Let's say you have an animal class. Every animal class inherits from it. If you want to create an dog you will make "class dog extends animal" and then "dog dog1 = new dog();", but don't you want to prevent creating an object from the animal class? Which weird living thing would be an object from the animal class? Would it have 5 heads and 20 legs? Could it spit fire? You don't want " animal animal1 = new animal();". You just want classes that inherit from it. That's one example I can think of right now. That's where thr keyword abstract comes in handy.
4th Jun 2017, 1:56 AM
Domsu
Domsu - avatar
0
thanks!
4th Jun 2017, 11:06 AM
Mr. Nobody
Mr. Nobody - avatar