• 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
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

finalize method question!!!

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone:

I know we cannot use a super keyword for an abstract implemented method. But below is my class hirearchy. As you know finalize has to be explictily chained, how can i achieve this with my below code?

class A {
public void finalize(){
System.out.println("Inside the finaliaze method");
}

}

abstract class B extends A {
public abstract void finalize();
}

class C extends B {

public void finalize(){
System.out.println("Inside the class B");
//super.finalize(); How can I call the finalize method in Class A
}
}

Thanks in advance.

Rajesh Shiggaon
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about just super()?

Mark
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We cannot call the abstract method in the immediate super class using super nor can use just super(). So in this case we cannot chain the finalize method from class C. super() alone should always be used as the first statement in the construtor but not in chaining the methods.
 
Rajesh Shiggaon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the calling of finalize() in the Class A is not possible (I knew it), why the complier cant prevent this kind of code.

Thanks
Rajesh S
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why the complier cant prevent this kind of code?



Rajesh,
can you give one single reason, why compiler should prevent such code?



In this piece of code, second class could have overridden the method "finalize()", but instead it made it abstract, so indeed by doing this, it eliminated existence of method "finalize()" of class A.


Since the calling of finalize() in the Class A is not possible (I knew it),


You are 100% correct. vis-a-vis that, compiler is also behaving 100% correct way because it does not matter design of any application as long as it is correct by all the rules.

Ranchers, please correct me if i am wrong.

Regards,
--Harshil
 
Rajesh Shiggaon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Harshil:

Thanks for your response.

Why i want to public abstract void finalize() in class B, so that i can force all the subclasses to enforce of providing there own implementation of finalize method. If we try to do this i will not be able to call finalize method in class A. Then why to allow such kind of code? Does it make sense?

Do you have any other way that you can force that all subclasses should write the finalize method? Still we can do it use hook and cook. But it will not be elegant way of programming.

Thanks,
Rajesh S
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic