Results 1 to 4 of 4
  1. #1
    java_no1 is offline Member
    Join Date
    May 2011
    Location
    Bengaluru
    Posts
    14
    Rep Power
    0

    Default How to invoke method present in abstract class?

    Hellooo every one.
    I am new to this forum. plz help me,
    i want to invoke method[ m1() ] from abstract class [ Test1 ] by creating object for Test2 class, where i ll be overriding the m1() method in Test2 class. I WANT OUTPUT : "hi" :-)
    here is the program:
    abstract class Test1
    {
    public void m1()
    {
    System.out.println("hi");
    }
    public abstract void m2();
    }
    class Test2 extends Test1
    {
    public void m1()
    {
    System.out.println("hello");
    }
    public void m2()
    {
    System.out.println("how are u");
    }

    }
    class Test
    {
    public static void main(String args[])
    {
    Test2 t1=new Test2();
    t1.m1();
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You can't.

    I would say you have a design flaw. Either the m1 method in the abstract class should also by abstract, thus the concrete class provides the implementation. Or the m1 method in the abstract class is a non-abstract method and then your concrete class should not override the method.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by Junky View Post
    I would say you have a design flaw.
    Absolutely!

    OP, if you are still eager to see that "hi" then you can use super.m1(); in your overridden methods.

    But the better approach would be to follow what Junky has mentioned above.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    java_no1 is offline Member
    Join Date
    May 2011
    Location
    Bengaluru
    Posts
    14
    Rep Power
    0

Similar Threads

  1. abstract method-abstract class
    By durgaprasad1407 in forum New To Java
    Replies: 1
    Last Post: 04-29-2011, 06:58 PM
  2. Replies: 4
    Last Post: 04-14-2011, 11:42 AM
  3. Replies: 4
    Last Post: 02-19-2011, 04:32 AM
  4. Replies: 1
    Last Post: 12-17-2010, 09:24 AM
  5. Replies: 6
    Last Post: 02-11-2009, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •