Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Interfaces

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does interfaces inherit from Object class?? (Interfaces methods should be public and abstract by default)
 
Ranch Foreman
Posts: 56
1
IntelliJ IDE Spring Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All classes inherit from the Object superclass in the end. However, an interface is not a class.

Regarding the interface methods: All interface methods are implicitly public (if I am not mistaken), but they are not all abstract methods. Since Java8, interfaces can also contain default methods and static interface methods. Both provide an implementation and can therefore not be abstract.
 
Saloon Keeper
Posts: 10308
217
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brecht Geeraerts wrote:All interface methods are implicitly public (if I am not mistaken)


If you don't use an access modifier the method will be public. Since Java 9, you can also declare private methods in an interface which like default and static methods can not be abstract.
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Brecht Geeraerts wrote:All interface methods are implicitly public (if I am not mistaken)


If you don't use an access modifier the method will be public. Since Java 9, you can also declare private methods in an interface which like default and static methods can not be abstract.



So does interface inherit from class Object or not since Interface is not a class??
 
Stephan van Hulst
Saloon Keeper
Posts: 10308
217
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brecht already answered that. Interfaces don't derive from classes, so they can't inherit from them.
 
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, an interface does NOT inherit from Object, or more correctly, it is not a subtype of the Object class. Think of an interface as a description of some kind of capability. You don't instantiate interfaces; you instantiate objects.

Try running this code:
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:No, an interface does NOT inherit from Object, or more correctly, it is not a subtype of the Object class. Think of an interface as a description of some kind of capability. You don't instantiate interfaces; you instantiate objects.

Try running this code:



Thanks for your reply.
It is showing null for Foo.class.getSuperclass() but
java. lang. Object for Bar.class.getSuperclass()

Which means Interfaces do not inherit class Object.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is the correct answer.

(That also shows that it's often possible to just write some code to find the answer to some question about Java.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!