[Logo]
Forums Register Login
Why a class declared as Abstract, cannot have any abstract method declaration?
Why a class declared as Abstract, cannot have any abstract method declaration?
Abstract classes can declare abstract methods. That's what they are for after all Do you have any concrete example of your problem ?
Yes,abstarct claases can declare both abstract and non abstract methods.
In fact you can only declare methods in abstract classes but you can't define them in abstract classes.
 

Mai Vala wrote:In fact you can only declare methods in abstract classes but you can't define them in abstract classes.


only, if the method is abstract . as you said abstract class can have non-abstract method[which has the body] right?
 

Muhammad Yasir Ch wrote:Why a class declared as Abstract, cannot have any abstract method declaration?



If my understanding is correct are you referring to the following scenario ?



The class above is abstract but does not define any abstract methods in it.
 

Seetharaman Venkatasamy wrote:only, if the method is abstract . as you said abstract class can have non-abstract method[which has the body] right?



Yes, if the method is non abstract then you have to follow the method declaration with curly braces.
 

Muhammad Yasir Ch wrote:Why a class declared as Abstract, cannot have any abstract method declaration?



Couldn't understand the OP's question!
 

Muhammad Yasir Ch wrote:Why a class declared as Abstract, cannot have any abstract method declaration?


What i understand from your question: Why a class declare as abstract if it does not have any abstract method? right?

Well, we declare a class abstract when we want to give some ready made behavior to a child.

The child will have choice to override their own behavior or just simply inherit from father, this is something that we cannot achieve with *Interfaces*.

Lets say the: We have a Father class Animal for all the animals following listed two behaviors of an animal.
1. fly.
2. sound.

Each Animal can sound, but only bird animals can fly...

Check out the following example.


[Edit: more example]

Abstract class is just like a concept where its child classes are the real things. Similarly Animal itself is not something that exist in real, the Animal cat,duck, dog and so on are real objects...

Therefore, Animal class in above example is an abstract because animal is no more than a concept no one can create an object of Animal in reality Cats, Dogs, Ducks, and etc exists...


Hope this helps

Minhaj
 

Muhammad Yasir Ch wrote:Why a class declared as Abstract, cannot have any abstract method declaration?



i think you are talking about this.......



the above code will compile fine....as the abstract class can have both abstract and non abstract methods.
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature


This thread has been viewed 1512 times.

All times above are in ranch (not your local) time.
The current ranch time is
Apr 24, 2018 02:44:06.