This week's book giveaway is in the Functional programming forum.
We're giving away four copies of A Functional Approach to Java: Augmenting Object-Oriented Java Code with Functional Principles and have Ben Weidig on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

///abstract///

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract class can have static methods. True or False ?
True
Why true??How could we use the static method in abstract class?for example..
abstract class Test{
abstract void a();
static void b(){};
}
//compile OK
class A{ Test.b(); }
//compile error
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I create two classes to make sure I got this right, they are in two files, and it seems I got it right. Correct me if I am wrong.
Parent class, an abstract class with a static method.

child class that overrides one abstract method from the parent class and use the static method from the parent class.

output after running test2.class file.

I think what happened is that the abstract method has to be extended by the child class before inherintance occures. After that, when the child class is instanciated into an object, all the methods in the parent/abstract class is treated as if it is part of the child class and/or child object; test2 extends test, therefor the test2 class has the static method in test, and so that static method can be used as if it is written inside of the test2 class.
Am I rignt? Correct me if I am wrong.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a method is declares abstract the class must be declared abstract
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello chao-long
Your code doesn't compile because the method call is not placed within a method. Place it within one and it will compile. abstract means that the class cannot be instantiated. static means that you don't need an object of that class to call it, thus there is no contradiction.
 
reply
    Bookmark Topic Watch Topic
  • New Topic