File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract method implementation

 
arivarasan manivasagam
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please any one answer it if the question is right....

There are three classes say A,B and C.
A is the abstract class containing atleast one abstract method.
B is abstract subclass of A, but it provides implementation for the abstract method in A(which it needn't but assume it provides).
Now, C is the concrete subclass of B.
Does C need to provide implementation for the abstract method of A, for which implementation is provided already in class B?

Thanks all,
arivarasan
 
Karthikeyan Balasubramanian
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hI it doesn't need to implement that method once again..since it is implemented in the above class itself..

it will compile and run fine.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arivarasan Welcome to Ranch,

Regarding your question It's not necessary to do so, If you are doing that then you are overriding the the method again.

Ps:- Please have a look at Ranch naming policy and change your display name accordingly.
[ October 17, 2005: Message edited by: Srinivasa Raghavan ]
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi arivarasan,
Welcome to the Ranch !

The Java Language Specification says,
If a class that is not abstract contains an abstract method, then a compile-time error occurs. A class C has abstract methods if any of the following is true:
  • C explicitly contains a declaration of an abstract method (�8.4.3).
  • Any of C's superclasses declares an abstract method that has not been implemented (�8.4.6.1) in C or any of its superclasses.
  • A direct superinterface (�8.1.4) of C declares or inherits a method (which is therefore necessarily abstract) and C neither declares nor inherits a method that implements it.


  • Simplifing the above points we have that if a class is declared abstract, than it must have an abstract method.

    Now, looking at second point shown bold, clearly says that C should implement any abstract methods that have not been implemented in C or any of its superclasses. Note the sentence,
    has not been implemented (�8.4.6.1) in C or any of its superclasses.

    Meaning that if the abstract method has been implemented in any of C's superclass than C is not required to implement the abstarct method, though C can implement that method.
    I deduced C can implement the method because JLS does not put any restriction on doing so. Correct me if I am wrong.

    The best way to know would be to write a small program for the same,


    The comipler does not complain.
     
    A Kumar
    Ranch Hand
    Posts: 980
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    You can implement the method that you have implemented in the second abstract class B again in the concrete class C...this is not necessary though..But if you do..it is equal to overriding...





    Output:

    Concrete class
    Abstract class


    Regards
     
    Kshz Rn
    Greenhorn
    Posts: 3
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    abstract class A
    { abstract public void show();
    public void test(){
    System.out.println("A");
    }
    }
    abstract class B extends A
    {
    public void display(){
    show();
    }
    }
    class C extends B
    {
    public void show(){
    System.out.println("C");
    }
    }
    class Test
    {
    public static void main(String S[])
    {
    B ob=new C();
    ob.display();
    }
    }

    Output: C

    can anyone tell, what is a flow of execution for above code?
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic