Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

How to user polymophism??  RSS feed

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I would like to have an abstract method in my base class. And I would like it to be a static method. But when I compile I get the error message "illegal combinatio of the accessor". Is it true that static can't be with abstract? Can someone explain to me why?
********My code******************
public abstract class superClass
{
public superClass()
{
}
//super abstract method
abstract public static String getName();
}
public class subClass
{
public subClass()
{
}
public static String getName()
{
return "SubClass";
}
}

Thanks, qionghua
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
As I understand it:
Abstract -- subclasses will provide implementation of method
Static -- can not be overridden only hidden by subclasses
Therefore, since we can't override the abstract method Java will not let us place both static and abstract together.
For example:

If static methods could be overridden then we would have expected the output of "Inside Class B" twice.
Regards,
Manfred.
 
qionghua yang
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
So I got it now. If a method is a static method, it can't be overriden by derived class. But then in this case, how can I implement the polymorphism?
qionghua
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!