Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract method

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
swapnil kataria
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to having an abstract method a class should be abstract
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic