File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract  method implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract  method implementation" Watch "abstract  method implementation" New topic
Author

abstract method implementation

arivarasan manivasagam
Greenhorn

Joined: Oct 17, 2005
Posts: 2
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

Joined: Apr 20, 2005
Posts: 89
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.


SCJP 1.4--93%<br />SCWCD 1.4--86%<br />Next--???
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
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 ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
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

    Joined: Jul 04, 2004
    Posts: 979
    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

    Joined: Apr 04, 2014
    Posts: 3
    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
     
    subject: abstract method implementation