1

Duplicate of Disabling inherited method on derived class

This is my class codes:

class Parent{
    public bool sum(int a){return true;}

    public int mul(int a){return a*a;}

}

and two another classes that extends parent class:

class derived extend parent{
    //here this child can see two methods of parent class and
    //I want just see sum method in here and don't see mul method
}
class derivedb extend parent{
//here this child can see two methods of parent class and
//I want here just see mul method.
}
Community
  • 1
  • 1
  • 1
    IMHO U can put private for mul function if u r not using this parent in any other place – Sundar Rajan Nov 19 '14 at 11:51
  • 1
    i do not understand the question. do u want to hide mul function of do you want the sum function to be in derived class? – No Idea For Name Nov 19 '14 at 11:52
  • Please note that Java has nothing like 'functions' but only 'Methods'. Somebody please edit the question. – Anirudh Nov 19 '14 at 11:53
  • If you want the `mul` method to be externally visible but prevent any subclass from redefining it, then you're going to need to make the `mul` method `final`. – JonK Nov 19 '14 at 11:54
  • Also `bool` is not a specific primitive type of java. Use `boolean` if that's what you mean or make it capital like `Bool` if it's user-specific class – Eypros Nov 19 '14 at 11:56
  • 1
    Damn.. wrong duplicate. It should have been http://stackoverflow.com/questions/5486402/disabling-inherited-method-on-derived-class – Aaron Digulla Nov 19 '14 at 11:56

1 Answers1

2

You can do it by making mul as private method in Parent class. So the mul method has visibility only inside Parent class

class Parent{
    public bool sum(int a){return true;}

    private int mul(int a){return a*a;}

}
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105