Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface extending a an abstract class

 
Krishna Chaitanya Reddy Kuninti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Syntactically , can an interface extend(inherits) a pure abstract class (that is an abstract class containing all abstract methods)?.

regards,
Krishna
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!!
you if want to check it syntacticlly if we can extend or not? than why dont you try this,please let me know as well....
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, thats a good question.

Lets just think before we ink..

If we had understood the concepts behind the interfaces and abstract classes and what they are actually, lets have some questions here..

  • Can a pure abstract class (as OP[Original Poster] quoted) be treated as an interface? (though they both mean to represent the same)
  • If at all, under what circumstances we can have it? We should have some clear separation right!


  • If the answer for question 1 above is "Yes", then we should have had a clear distinction on the scenarios of how and when it can be treated as such.

    But, actually they are NOT equal. The reasons behind them are :

  • The structure wise they both are treated different.
  • The interface does not come under the inheritance hierarchy whereas an abstract class does


  • Now, can you come to a conclusion? or the result expected out of compiling such a program?


    The compilation of above program gives the following error



    Hope this helps!
    [ July 30, 2007: Message edited by: Raghavan Muthu ]
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey Raghavan

    You have mentioned that an interface cannot
    be instantiated but an abstract class can be.....


    To what i can say is abstract class can never
    be instantiated as per i know,we can just inherit the
    methods of abstract class that is for it is abstract
    isnt it?
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for correcting dhwani. I was just intended to say the "inheritance hierarchy" but while the way i was typing, it got a typo that "instantiate". Its edited now.

    Thank you.
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are Welcome Raghavan!!!
     
    Krishna Chaitanya Reddy Kuninti
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi all,

    I tried to write an interface that extends(inherits) a pure abstract class and find some facts.

    1) An Interface can extend(inherits) only another interface.

    2)An Interface can not extend(inherits) any other class, abstract class with non-abstract methods and pure abstract class(abstract class having all abstract methods).

    In other way, there can be a super interface for an interface but there can not be a superclass for an interface.
     
    Krishna Chaitanya Reddy Kuninti
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi all,
    Hi all,

    When I tried to write an interface that extends(inherits) a pure abstract class, I have found the following facts.

    1) An Interface can extend(inherits) only another interface.

    2)An Interface can not extend(inherits) any other class, abstract class with non-abstract methods and pure abstract class(abstract class having all abstract methods).

    In other way, there can be a super interface for an interface but there can not be a superclass for an interface.

    regrads,
    Krishna
     
    Peter Chase
    Ranch Hand
    Posts: 1970
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Trial and error is one way to find out the answer to these questions, but it can lead to misunderstandings and bad assumptions.

    The best way to find out what is allowed in the language is to read the Java Language Specification, which is available on-line for free. For instance, here is the section on interfaces, which tells you what is and is not legal.

    By the way, while what you called a "pure abstract class" (a class with no concrete methods) is legal in Java, it is rarely useful. Why would you not want to use an interface? Perhaps you have come from C++, where interfaces are not a separate kind of thing and are instead represented as "pure-virtual classes".
    [ July 31, 2007: Message edited by: Peter Chase ]
     
    Krishna Chaitanya Reddy Kuninti
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi all,

    Peter, thanks for your advice and information.

    regards,
    Krishna
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic