Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Why abstract key word is needed infront of abstract class methods

 
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 72689
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72689
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks and am marking as resolved
 
Campbell Ritchie
Marshal
Posts: 72689
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic