• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why abstract key word is needed infront of abstract class methods  RSS feed

 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a simple classes here one is interface and another one is abstract class when i try to compile them abstract class is givving compilation error.



HelloWorld.java:11: error: missing method body, or declare abstract
public String getName();
^
1 error
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without the abstract modifier, the compiler expects the method to have a body and if it did not find one, it will complain. The abstract keyword is a clue to to compiler to not expect a body for the method.
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface has also same method structur and it also have same abstract methods. why the compile is not showing any error message in that case?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because methods in an interface are always abstract, it would just be unnecessary extra typing if you would have to specify abstract for every method in an interface.
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's a foolish question, i know all the methods in Interface are abstract that is why we don't have to mention JVM a method as abstract.
But in case of abstract class an un implimented method is by default abstract why we have to mention abstract infornt of those methods? Why JVM iss not recognizing those methods as abstract(same as interfcae)?
And we are alos suupplying an additional information to JVm by mentioning class as abstract
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mallikarjun dontamsetti wrote:I think it's a foolish question . . .
No, it isn't

But in case of abstract class an un implimented method is by default abstract . . .
No, it isn't
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:

But in case of abstract class an un implimented method is by default abstract . . .
No, it isn't


Is an un-implemented method can be not abstract?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, an unimplemented method might be a syntax errorIs the second part of that code the body of foo() which has a semicolon entered by mistake or is it an instance initialiser following the abstract method foo()?
∴ You do not allow unimplemented methods even in abstract classes, unless they have been marked abstract. That is how the compiler is programmed, and I presume that is how the Java® Language Specification defines an abstract method.
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks and am marking as resolved
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!