Is there any way in IntelliJ IDEA to search for classes that does not override a certain method?
Let's say I have these classes:
public abstract class BaseClass {
public void methodThatMightBeOverridden() {
}
}
public class ConcreteClassWithOverriddenMethod extends BaseClass {
#Override
public void methodThatMightBeOverridden() {
super.methodThatMightBeOverridden();
}
}
public class ConcreteClassWithoutOverriddenMethod extends BaseClass {
}
public class AnotherConcreteClassWithoutOverriddenMethod extends ConcreteClassWithoutOverriddenMethod {
}
Is it possible in IntelliJ to find all extensions of BaseClass that does not override methodThatMightBeOverridden()? Note that I would need to find classes even though they do not directly implement BaseClass. In the example above, that would be ConcreteClassWithoutOverriddenMethod and AnotherConcreteClassWithoutOverriddenMethod.
I know how to use type hierarchy to find classes where the method is overridden, but have not found a way to do it the other way around.
I have tried googling for it without any luck. Also, this is a simplified example. In the real code we have many implementations of the sub classes, of which some does not extend the class.
I found the support for this. By standing on the method and pressing Ctrl + Shift + H (or Navigate -> Method Hierarchy in the menu), you get a nice view of all overriding methods:
Here I put all four classes in a class called Test as below, hence the "in Test" text after the class names. The picture is pretty self explanatory, but the minus sign means that the class does not override the method.
package test;
public class Test {
public abstract class BaseClass {
public void methodThatMightBeOverridden() {
}
}
public class ConcreteClassWithOverriddenMethod extends BaseClass {
#Override
public void methodThatMightBeOverridden() {
super.methodThatMightBeOverridden();
}
}
public class ConcreteClassWithoutOverriddenMethod extends BaseClass {
}
public class AnotherConcreteClassWithoutOverriddenMethod extends ConcreteClassWithoutOverriddenMethod {
}
}
You could use Structural Search. Use a search template like this:
class $A$ extends $B$ {
void $m$();
}
Where under Edit Variables variable m has text/regexp methodThatMightBeOverridden and min+max occurrences count of 0. And variable B has text/regexp BaseClass and the Apply constraint within type hierarchy checkbox enabled.
Related
I have several children of a parent class. I want the parent to implement a method that returns data. However, that data can only ever be correctly populated by the children.
Is this the ideal way to do this in Java? Is there a way to make it so that only the children can call the method?
public class MyParent {
public MyData getData() {
}
}
public class MyChildA extends MyParent {
#override
public MyData getData() {
}
}
Try out, abstract classes.
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, then the class itself must be declared abstract, as in:
public abstract class GraphicObject {
// declare fields
// declare nonabstract methods
abstract void draw();
}
Hope it helps.
Make your parent class an Abstract class.
public abstract class MyParent {
public abstract void getData();
}
public class MyChildA extends MyParent {
#Override
public void getData() {
// statements
}
}
Your question was a bit ambiguous so I don't know if this is what you were looking for.
I have got multiple classes which each implement multiple different methods within each. Now the problem statement is that I wish to use the methods from all these (maybe around ~200 such different class files/methods) in another class file which all different methods from the above class files.
I thought that if I implement an interface which has all these various methods listed, then I just call/import/reference that single interface and can use all the methods? But I am stuck, as this solution does not seem to work.
The opposite of the above works (i.e. single class implements 2 interfaces: http://tutorials.jenkov.com/java/interfaces.html). Wish to check if the single interface can use multiple classes, without the overhead of declaring all the methods in each class that is being referenced inside the Interface?
As an example: Is there any way in which I can implement 2 different classes in the same interface, without each having the abstract class for each? As if the class is abstract, then I am unable to use the methods from it in the below example "Application" class:
Common commonClass = new ABC_FamilyGivenName();
The above is not allowed, if the ABC_FamilyGivenName class is an abstract class.
INTERFACE:
public interface Common {
void ABC_GivenNames();
void ABC_FamilyNames();
void ABC_Gender();
void ABC_BirthDay();
}
IMPLEMENTATION CLASSES:
public class ABC_FamilyGivenName extends Base implements Common {
public void ABC_GivenNames(){
// Implementation code
}
public void ABC_FamilyNames(){
// Implementation code
}
}
public class ABC_DOBGender extends Base implements Common {
public void ABC_Gender(){
// Implementation code
}
public void ABC_BirthDay(){
// Implementation code
}
}
USE IMPLEMENTED CLASS:
public class Application extends Base {
Common commonClass = new ABC_FamilyGivenName();
/* DO I NEED THIS? I THINK I DO, BUT CODE/JAVA SAYS I DO NOT
* Common commonClass = new ABC_DOBGender();
*/
public void ELP_C0050_PassportDetails(){
commonClass.ABC_GivenNames();
commonClass.ABC_FamilyNames();
commonClass.ABC_DOB();
commonClass.ABC_Gender();
}
}
I have 2 classes called ABC_FamilyGivenName & ABC_DOBGender.
I have created an interface Common.
I want to use the methods in both the above classes in another class called Application.
With the current implementation, Java wants me to add an #Override to both the ABC_FamilyGivenName & ABC_DOBGender:
IMPLEMENTATION CLASSES:
public class ABC_FamilyGivenName extends Base implements Common {
public void ABC_GivenNames(){
// Implementation code
}
public void ABC_FamilyNames(){
// Implementation code
}
#Override
public void ABC_BirthDay() {}
#Override
public void ABC_Gender() {}
}
public class ABC_DOBGender extends Base implements Common {
public void ABC_Gender(){
// Implementation code
}
public void ABC_BirthDay(){
// Implementation code
}
#Override
public void ABC_GivenName() { }
#Override
public void ABC_FamilyName() { }
}
Can I avoid the above #Override and just use the classes without these as given in the first example?
Object-oriented programming in Java requires to "override" all methods, if you are implementing a method, otherwise you may use inheritance, so not all methods must be overriden.
In your case you may put all four methods to parent class Base and then inherit them.
Then the interface class is not needed or make two different interfaces.
To implement Java interface, You should override all the abstract methods are declared into the interface. It is a basic concept of interface. Here interface Common all four methods are abstract, So you should override them. Otherwise, Java compiler will throw a compilation error. So better way can be splitting the interface into 2 parts.
It is a contractual nature of an interface the subclass who implement the interface should have all the activities of the interface. It is the main purpose of using an interface.
If you don't wanna override all the method of interface but you need to use the interface as a reference of every class, then you can use a concrete class instead of interface and inherit the concrete class to every class
To implement the below code change please make sure you use java8
public interface Common {
default public void ABC_GivenNames() {
}
default public void ABC_FamilyNames() {
}
default public void ABC_Gender() {
}
default public void ABC_BirthDay() {
}
}
IMPLEMENTATION CLASSES:
public class ABC_FamilyGivenName extends Base implements Common {
public void ABC_GivenNames(){
// Implementation code
}
public void ABC_FamilyNames(){
// Implementation code
}
}
public class ABC_DOBGender extends Base implements Common {
public void ABC_Gender(){
// Implementation code
}
public void ABC_BirthDay(){
// Implementation code
}
}
Can I avoid the above #Override and just use the classes without these
as given in the first example?
No, in java you have to implement all methods of interface unless its abstract class
as suggestion you can create two separate interfaces,
for more detail see : not implementing all of the methods of interface. is it possible?
You can provide an empty implementation for all the methods of an interface in other class called Adaptor class. And you can extend that adaptor class in ABC_FamilyGivenName class and ABC_DOBGender class.
class Adaptor implements common
{
public void ABC_GivenNames() {
}
public void ABC_FamilyNames() {
}
public void ABC_Gender() {
}
public void ABC_BirthDay() {
}
}
IMPLEMENTATION CLASSES :
public class ABC_FamilyGivenName extends Adaptor{
public void ABC_GivenNames(){
// Implementation code
}
public void ABC_FamilyNames(){
// Implementation code
}
}
public class ABC_DOBGender extends Adaptor {
public void ABC_Gender(){
// Implementation code
}
public void ABC_BirthDay(){
// Implementation code
}
}
interface Icalculate{ //interface
calculate(operand1:number,operand2:number):number
}
class Add implements Icalculate{ //addition
calculate(operand1: number, operand2: number): number{
return (operand1 + operand2);
}
}
class Sub implements Icalculate{ //subtraction
calculate(operand1: number, operand2: number): number{
return (operand1 - operand2);
}
}
class Mul implements Icalculate{ //multiplicationn
calculate(operand1: number, operand2: number): number{
return(operand1*operand2);
}
}
class Div implements Icalculate{ //Division
calculate(operand1: number, operand2: number): number{
return(operand1/operand2);
}
}
let a = new Add;
let b = new Sub;
let c = new Mul;
let d = new Div;
class Calculator { //main class
operator: Icalculate;
operand1: number;
operand2: number;
constructor(a: number, b: number, operator: Icalculate) {
this.operand1 = a;
this.operand2 = b;
this.operator = operator;
let op = this.operator;
console.log(op.calculate(this.operand1, this.operand2));
}
}
const cal=new Calculator(1,1,a);
I have an abstract class where I want to create a method that can be passed either of the child instances that extend it.
For example:
abstract class base
{
public void doSomething(Child1 or Child2){
}
}
class Child1 extends base
{
public void somefunc(){
doSomething(Child1);
}
}
class Child2 extends base
{
public void somefunc(){
doSomething(Child2);
}
}
I feel like there are multiple ways to get around it. Is it possible to get an idea of what are the possible ways? I definitely dont want to create the same function in all the child classes.
I'm not sure if the parameter passed to doSomething is supposed to be this, or a separate object of the same type. In the first case, peter.petrov's answer is what you want. In the second case, the standard way around this is to make Base generic and to use a recursive bound.
abstract class Base<T extends Base<T>> {
public void doSomething(T t) {}
}
class Child1 extends Base<Child1> {
public void someFunc() {
Child1 child1 = new Child1();
doSomething(child1);
}
}
class Child2 extends Base<Child2> {
public void someFunc() {
Child2 child2 = new Child2();
doSomething(child2);
}
}
Seems like this is what you're looking for.
abstract class base
{
public void doSomething(base obj){
}
}
class Child1 extends base
{
public void somefunc(){
doSomething(this);
}
}
class Child2 extends base
{
public void somefunc(){
doSomething(this);
}
}
Then, if you want override doSomething in your child classes.
In fact, you don't even need the base parameter and to pass this, you already have an implicit reference to this.
One of the ideas behind inheritance is to share behaviour amongst classes of the same abstract type.
So if you want the same behaviour for all subtypes of your base class, implement the method in your base class. You don't need to pass the object reference, use the this keyword.
If you want different behaviours in your sub classes, declare the method abstract in your base class and implement each of the behaviours in the various sub classes.
I have an interface called Relation, implemented by a class BasicRelation, and extended by subclasses (e.g. ParentChild, Sibling, Spouse). While developing my code, I realized that I often need a method which takes a String representation of a relation to create it. For example:
public class ParentChild implements Relation extends BasicRelation {
// e.g. "Jack is Emily's father. Jill is her mother." will return the list
// <ParentChild(Jack, Emily), ParentChild(Jill, Emily)>
static List<ParentChild> fromSentence(String s) {
...
}
}
Now, since I find myself needing this method (fromSentence(String)) in every class, except perhaps in BasicRelation, I would like to move it up the hierarchy. The problem is that the internal details of the method is subclass-dependent, so I can't have it as a static method in the interface Relation or the superclass BasicRelation.
Unfortunately, in Java, it is also not possible to have a static abstract method.
Is there any way to ensure that every subclass of BasicRelation (or every class implementing Relation) implements fromSentence(String)? If no, should I be designing this in a completely different way? I guess this last question is more of a request for design-advice than a question.
Why does the static method need to be in the interface? What's stopping you from having a 'Utility' class and having the method in there?
public class RelationUtility {
public static BasicRelation relationFactory(String asString) {
....
}
}
As a static method, there is no reason other than access to private members, which can also be accomplished by by 'default' permissions on those members....
You can try making the BasicRelation class an abstract class and use an abstract fromSentence(..) method. This would require the ParentChild class to override and implement the fromSentence method because you can't create an object for ParentChild without implementing fromSentence()
public abstract class BasicRelation extends Relation(){
public abstract List<..> fromSentence(String s);
}
public class ParentChild implements Relation extends BasicRelation {
fromSentence(){
//parentChild class's implementation
}
}
If I understood right... you can try an approach like this
public class BasicRelation {
public abstract List<ParentChild> fromSentenceInSubclass(s);
public List<ParentChild> fromSentence(String s){
fromSentenceInSubclass(s);
}
}
And then you could have:
public class SubclassRelation extends BasicRelation {
public List<ParentChild> fromSentenceInSubclass(s){
// do subclass relation stuff
}
}
You will probably need to change the code a bit and add some Generics around to make it happen the way you want.
Sotirios Delimanolis Factory suggestion might also be an option.
You can have the abstract class BasicRelation include the static method which throws an Exception. That way you will be forced to override (shadow) the static method in the subclasses when you use it.
Something like:
public abstract class BasicRelation {
public static List<..> fromSentence(String s) {
throw new RuntimeException();
}
}
I wonder if it makes any difference if a method is public or package protected in a class that is package protected.
class Example {
public void test() {}
}
instead of
class Example {
void test() {}
}
I guess the maximum visibility is given by the class. And a method can only reduce the visibility and increasing the visibility has no effect.
But it's valid syntax, so perhaps I've overseen something?
If we subclass Example to a public class , then code outside the package can access test() method using the subclass instance if it is public .
Example:
package A;
class Example {
public void test() {}
}
package A;
public class SubExample extends Example {
}
package B;
import A.SubExample;
class OutsidePackage {
public void some method(SubExample e){
// Had test been defined with default access in class Example
// the below line would be a compilation error.
e.test();
}
}
If Example implemented an interface of some kind you'd have to make them public, because you can't reduce access in that case. All interface methods are, by default, public.
As written, it does nothing. If it's a subclass or interface implementation, then it may be implementing or overriding methods that are declared public elsewhere.