Results 1 to 6 of 6

Thread: Inheritance and polymorphism in static method

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Inheritance and polymorphism in static method

    Hello,
    I have a problem a bit special, in fact not a real problem but rather a concern that I design because I love solving perfection. I have a class called 'vegetable' which contains a static method named 'getFoo ()' that returns an integer. I now creates a carrot and cabbage class that inherits from vegetable and method overloading 'getFoo' by returning different values each. I have a method in another class completely independent of the schematic revision named 'processFoo' here is the prototype:
    Code:
    public void processFoo (Vegetable vetg);
    I'd like to access a static method of a class corresponding to an object instantiated. but it is not accessing it. If you have any solution for this then it would be great. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    335

    Re: Inheritance and polymorphism in static method

    Hello,
    I think you mix static and instance methods. A static method is independent bodies. You use an instance of an object, and you want to execute a method in this instance it is a dynamic execution. By calling via reflection a static method, you simulate the behavior of the dynamic execution in a static concept, and this must surely be far less "efficient" that uses a dynamic method (which is fine by optimizing the JVM ).

  3. #3
    Join Date
    Dec 2009
    Posts
    202

    Re: Inheritance and polymorphism in static method

    Hello,
    I think even I have a similar kind of a problem with my code. For, while programming object, many Java developers make the mistake of forgetting the concept of static element and is found with objects instantiated multiple times without any need! ... while the static concept is extremely useful to be really efficient OOP, including classes using the singleton or other pattern like that. I guess I am correct here, and if I am not then please guide me with correct.

  4. #4
    Join Date
    Nov 2009
    Posts
    333

    Re: Inheritance and polymorphism in static method

    Hello,
    I have nothing against static elements when necessarily. But in this case it looks more like a simulation of dynamic invocation ( call a static method differs depending on the actual type of an instance). In this case there is no reason to be static. What I meant is that the dynamic invocation is a cornerstone of OOP (especially Java), and if you want to do without it for X or Y because it is better to change the language.

  5. #5
    Join Date
    Nov 2009
    Posts
    356

    Re: Inheritance and polymorphism in static method

    Hello,
    I find that static methods are impractical in java, and I use them only for some sort of utility class. The inheritance and polymorphisms in particular are very badly managed, in my opinion. So for your case, or I need the inheritance, and then I would make the method getFoo instance method, benefiting the whole body mechanics, or I give them a special signature for each class, for example getFooDeClass1 () getFooDeClass2 (), etc.. This amounts to a little tone getFoo (Class1 fooType), except that it is more radical.

  6. #6
    Join Date
    Nov 2009
    Posts
    446

    Re: Inheritance and polymorphism in static method

    Hello,
    It is already possible with Generics for Java 5.0.
    The inheritance and polymorphisms in particular are very badly managed, in my opinion.
    At the same time static means we are not bound to an instance ... So you can not really talk about inheritance. Returning to the problem was what to serve it all about?

Similar Threads

  1. What is static method in java?
    By Vaibhav S in forum Software Development
    Replies: 5
    Last Post: 25-09-2011, 09:40 AM
  2. Dynamic and Static Polymorphism in Java
    By Ardent in forum Software Development
    Replies: 4
    Last Post: 19-08-2010, 03:37 PM
  3. Runtime polymorphism Vs Compile time polymorphism
    By Owen Fernandes in forum Software Development
    Replies: 4
    Last Post: 03-02-2010, 11:06 AM
  4. Polymorphism VS Inheritance
    By Taylor D in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 10:12 AM
  5. Run - Time polymorphism with Overriden method in JAVA
    By NAYASA in forum Software Development
    Replies: 1
    Last Post: 04-12-2008, 07:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,485,858.15149 seconds with 17 queries