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

Difference between Abstract and protected?

List the rules of abstraction if possible.

25th Dec 2016, 5:09 PM
Deep chand
Deep chand - avatar
10 Answers
+ 7
Think of a Zoo. There are many different animals. So we define an abstract class animal. This contains an attribute "legs" and a method "action" which prints out the sound the animal does. Cause animals have a different number of legs and do different noises you dont wanna implement them hard. So you define a class Lion which inherits Animal. Then you can override legs with a value of 4 and your action method by printing "Roar" in it. This you can do for all kind of animal.
25th Dec 2016, 5:43 PM
Andreas K
Andreas K - avatar
+ 5
Abstract classes cant be instantiated so they cant create any kind of methods or variables they are a blueprint for other classes
25th Dec 2016, 5:26 PM
Andreas K
Andreas K - avatar
+ 3
Abstract class are useful if you want to use an Array of Animal (regardless what type of animal) (to use @AKC's zoo) for example You can also use them for the use of someone else. For example you code a library of graphic display (yeah your good) and you want to create a pattern so that programmers will be able to create their own object, you can use an abstract class for it
31st Dec 2016, 9:54 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
I dont list rules of abstraction, because your question targets two completely different things. Abstract is a programming concept to create a pattern for other classes. protected is an access modifier like public and private. protected variables can be accessed from all classes in your package.
25th Dec 2016, 5:22 PM
Andreas K
Andreas K - avatar
+ 2
what's the point of using abstract in real life coding? Just for setting a pattern? why?
25th Dec 2016, 5:29 PM
Deep chand
Deep chand - avatar
+ 2
Abstract is used for polimorfism (polimorfismo I dont know english, sorry). Protected is a encapsulation (cláusula de encapsulación, de acceso) or access clause.. the a class
31st Dec 2016, 6:53 PM
Luis Fabricio Tano
Luis Fabricio Tano - avatar
+ 1
@Baptiste I like that you built on others answers to expand yours. :)
31st Dec 2016, 10:44 AM
Andreas K
Andreas K - avatar
+ 1
abstract and protected are two different things abstact class or method can't be instanciated however a protected class or method is visible just in the same package
31st Dec 2016, 6:09 PM
BENNACER Family
BENNACER Family - avatar
+ 1
an abstract class is interface
31st Dec 2016, 6:17 PM
BENNACER Family
BENNACER Family - avatar
- 7
Hvfhnbv
31st Dec 2016, 10:47 AM
hapan murmu