| Author |
abstract method
|
nirjari patel
Ranch Hand
Joined: Apr 23, 2009
Posts: 240
|
|
When a class is declared as abstract , is it required that it must contain an abstract method ?
I tried it in eclipse and I did not get any error. Does it mean that a class can be declared abstract without any of its method declared as abstract or any of its methids undefined ? If thats the case, then why do we need abstract modifier for a method ? We can simply not define the method and just declare class as an abstract.
I tried other way also. I kept class declaration as non-abstract and declared a method as abstract, but it gave error. So why do we need abstract modifier for method ? I dont get any error even if I dont use that modifier or define all methods in abstract class ?
Thanks
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
nirjari patel wrote:When a class is declared as abstract , is it required that it must contain an abstract method ?
Nope.
nirjari patel wrote:I tried it in eclipse and I did not get any error. Does it mean that a class can be declared abstract without any of its method declared as abstract or any of its methids undefined ? If thats the case, then why do we need abstract modifier for a method ? We can simply not define the method and just declare class as an abstract.
No because an abstract class can contain concrete methods.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
pawan chopra
Ranch Hand
Joined: Jan 23, 2008
Posts: 362
|
|
Not sure what is the query in this. But if you want to make a class Abstract.
(1) You need to declare abstract keyword with the class declaration. Now your class is an abstract class. By declaring your class abstract you make sure that this class can not be instantiated. Below can not work:
(2) You can have abstract methods in that class but they also need to have a abstract keyword in the method signature.
(3) There is no restriction that an abstract class must have abstract methods.
(5) Abstract class can have concrete methods.
(4) You can not have an abstract method in a non-abstract class.
I hope this will clear things a bit. You can go through this link for more information.
|
Pawan Chopra
SCJP - DuMmIeS mInD
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
abstract for a method indicates that the method doesn't have a method-body. And a class that has this method has to be declared as abstract because the other class extending this class have to provide the method body for the abstract method.
And suppose the class has abstract method and the class is not declared as abstract- then if we create an instance of that class and invoke the abstract method- what method body will it execute?
|
Mohamed Sanaulla | My Blog
|
 |
swapnil kataria
Ranch Hand
Joined: Feb 26, 2011
Posts: 64
|
|
|
to having an abstract method a class should be abstract
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12928
|
|
swapnil kataria wrote:to having an abstract method a class should be abstract
Yes, but not the other way around, which is what Nirjari asked.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
nirjari patel
Ranch Hand
Joined: Apr 23, 2009
Posts: 240
|
|
public abstract class AbstractClass{
public void method1(){}
public void method2(){}
//public String method3(){}
}
In this code none of the methods are abstract. If AbstractSubClass extends AbstractClass, it is not required to implement any of these methods as all the methods are already implemented. Commented line, will give an error if its not implemented as it returns String. If I want to use this method in the class, do I need to declare it as abstract ?
Thanks
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
nirjari patel wrote:public abstract class AbstractClass{
public void method1(){}
public void method2(){}
//public String method3(){}
}
In this code none of the methods are abstract. If AbstractSubClass extends AbstractClass, it is not required to implement any of these methods as all the methods are already implemented.
Yes not required to implement.
nirjari patel wrote:
Commented line, will give an error if its not implemented as it returns String. If I want to use this method in the class, do I need to declare it as abstract ?
Thanks
It gives error because its implementation is wrong. You have a method which returns something, but in the implementation it doesn't return anything. If you want it to work still you would have to declare that method as:
and then make the sub class to implement this method- Remember that the implementation should return a value of type String, otherwise it will be a compiler error.
|
 |
 |
|
|
subject: abstract method
|
|
|