+ 1
What's the point of having abstract class if anyway we have to declare and define the method again in the inheriting class ?
2 Antworten
+ 1
Abstract classes and interfaces define types. Every class extending them is of that type. Putting in abstract methods proves that all inheriting classes will do something like this. That is important, so the compiler can check whether your program is correct.
Example: Lions and tigers have fur color.
You say, you saw a lion.
I ask what color did it have?
You see a tiger.
I ask, what color is it.
Both work nicely.
Now you say, you saw some tigers and lions.
In programming that is a list holding tigers and lions.
Now the compiler will try to find the lowest common super type. It asks: What do tigers and lions have in common?
They are cats. So this is a list of cat.
Now unless cat already mentions fur color, probably as an abstract field, you cannot ask about the colors of the toes and lions. Because once you put them in a mixed list, they are just cats.
Now unless cat already mentions fur color, probably as an abstract field, you cannot ask about the colors of the toes and lions. Because once you put them in a mixed list, they are just cats.
The compiler forgets that they ever were anything but cats. So if you do not say explicitly that cats have color, you cannot ask for that.
There are ways to get "forgotten" types back later.
0
key word is ABSTRACT. Abstraction itself is an important part of OOP. when we deal with complicated programm structure, in some cases we should definitely need some levels of abstraction to make our app more flexible, especially in future.
of course, in simple apps abstract classes seems unnecessary.
check Interfaces though. it's important to understand difference between abstract class and interface, their pros and cons