• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

doubt in using abstract class

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

can anyone please clarify my doubt in using abstract class...


public abstract class TestAbstract {

public void getMethod()
{
System.out.println("testing concrete method");
}
public static void concreteMethod()
{
System.out.println("testing concrete method");
}
public abstract void abstractMethod();


}
here i have created one abstract class called TestAbstract which has abstract method , non-abstract method and one static non-abstract method.

For abstract class can't be instansitated.we can access static method using class name.now i want to access non-abstract method.i know one way is to extend that class and we can use the method.Please let me know any another way to access the non abstract method without extending that class.

Thanks and regards,

Vasiammal.p
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ruban raj:
Please let me know any another way to access the non abstract method without extending that class.



There is no way to do this.
 
Marshal
Posts: 73257
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there is, surely. You instantiate it as an anonymous class. It will work in this instance because it has a default constructor; it might be difficult with a constructor with arguments.That will work, as long as you have a CampbellsException class, surely?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
That will work, as long as you have a CampbellsException class, surely?



Never tried it. I'll take your word for it that it works.
 
Campbell Ritchie
Marshal
Posts: 73257
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I shall have to try it. Actually, I think I was wrong calling that an anonymous class.
 
Sheriff
Posts: 22256
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
it might be difficult with a constructor with arguments.


It isn't:

Anonymous classes are the only case where constructors are inherited. You can't create any extra constructors though, you are stuck with what the base class provides.
 
Campbell Ritchie
Marshal
Posts: 73257
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works with the name field, and still works if the name field is deleted and the constructor replaced by a default constructor. It doesn't however work if the name field has private access.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Yes, there is, surely.



Everything said about anonymous classes is correct, except for the implied statement that using an anonymous class is different from extending the abstract class. The anonymous class is a child of the parent class just as a named one would be; there's no difference.
 
Campbell Ritchie
Marshal
Posts: 73257
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's why the private access wouldn't work and I had to change it to protected. Have we got a lightbulb smilie?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic