-2
public class Write
 {
   private void writesomething()
    {
     System.out.println("Title");
    }
}

public class WriteOther extends Write
{
    private void writesomething()
     {
       System.out.println("paragraph");
     }
}

As the example I want to use same method but should write "title" and "paragraph" together. how implement that using java. its like hardware control fist method do controlling hardware. second method user extend and command hardware.

  • It would be nice to see an interface which you defined as well. – Tim Biegeleisen Oct 14 '18 at 14:28
  • 2
    Sounds like you want to call the inherited `writeSomething` before you print "paragraph". – lurker Oct 14 '18 at 14:32
  • Have you considered using super? – Mad Physicist Oct 14 '18 at 14:32
  • Yes, look at doing a `super.writesomething()` call. Best though to read a tutorial on inheriting methods as this sort of question is best answered by the full understanding that a tutorial can bring. – Hovercraft Full Of Eels Oct 14 '18 at 14:32
  • 1
    Possible duplicate of [In Java, how do I call a base class's method from the overriding method in a derived class?](https://stackoverflow.com/questions/268929/in-java-how-do-i-call-a-base-classs-method-from-the-overriding-method-in-a-der) – lurker Oct 14 '18 at 14:33

1 Answers1

1

The super keyword gives you access to the parent's implementation:

public class WriteOther extends Write
{
    protected void writesomething()
    {
        super.writesomething();
        System.out.println("paragraph");
    }
}

However, as written, you won't have direct access to the method unless you change its access to protected or higher:

public class Write
{
    protected void writesomething()
    {
        System.out.println("Title");
    }
}
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264