Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why a class declared as Abstract, cannot have any abstract method declaration?

 
Muhammad Yasir Ch
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why a class declared as Abstract, cannot have any abstract method declaration?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes can declare abstract methods. That's what they are for after all Do you have any concrete example of your problem ?
 
Mai Vala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mai Vala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad Yasir Ch wrote:Why a class declared as Abstract, cannot have any abstract method declaration?


Couldn't understand the OP's question!
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic