简体   繁体   中英

how can i not implement all methods of an abstract class to another one that inherits it

I am new in Java and I can't understand how can I not implement all methods of an abstract class A to class C.In my example I don't want to implement method 1 in class C.Thanks for helping!

public abstract Class A
{
   public abstract boolean method1(Sting _str);
   public abstract void method2();
   public abstract A getA(Sring str);
}

public abstract class B extends A
{
   \\ ..
}

public class C extends B
{
  public void method2()
  {  
      \\...
  }
  public A getA(String _str)
  {
        ....
  }

  //here I don't want to implement method 1
}

public Class D extends B
{
   public boolean method1(String _str)
   {
      //...
   }
   public void method2(String _str)
   {
      //...
   }
   public A getA()
   { 
        //... 
   }
 }

An abstract method must be implemented somewhere in the class hierarchy.

In your sample you have to implement it in either B or C. There is no way around it.

When using abstract keyword there are few points you need to know

  • when a class contains one or more abstract methods class should be declared abstract as well

  • While using abstract class you cannot create object of the abstract class because it is not fully defined

  • Any subclass of an abstract class must either implement all of the abstract methods in super class or itself be abstract

  • You cannot create abstract constructors or abstract static Methods

Many answers has been given and most of them are correct indeed. However, I can forsee that this question is more about fundamental knowledge than an code issue.

First of, you need to understand this:

  • Inherit from a superclass makes your code "tightgly coupled" so no doubts about which asbtract methods are needed or you end up with a big mess!

  • A sub concrete class that inherits from an abstract super class must implement all abstract methods! otherwise it must be an abstract class.

  • If you need a default behaviour you need an asbtract class over an interface.

Meaning that you have to plan AHEAD! Well, that doesn't mean that you can't have a more relaxing desing of classes where for example your code does not sufer so much from mechanisms such as inheritance.

You can provide the same results by using interfaces and or composition. If you need to change your design more than you think, make it an interface. However, you may have abstract classes that provide some default behavior as I said earlier...

Abstract classes can have concrete methods while interface CAN'T!

Another thing you need to understand is that you can only inherit from a class however you can implement as many interfaces as you whish! This a clearly a path to me... many programmers refer to Java as a bad programming language but many of those programmers need to understand that Java is not about inheritance! So my advice is stop using inheritance based on asbtract/concrete classes as an obligation!

At the end make use of simple technics to power your work; read about dependency injection and composition for example!

The beneath example only serves as a right way to implement inheritance based on asbtract classes.

I hope it helps you understand the basics... have fun programming!

Code example based on asbtract class:

public abstract class AShape {

    public abstract double calculateArea();

    public abstract double calculatePerimeter();

}

public class Circle extends AShape {

    private double radius;

    Circle(double radius){
        this.radius = radius;
    }

    @Override
    public double calculateArea() {
        return Math.PI * Math.pow(this.radius, 2);
    }

    @Override
    public double calculatePerimeter() {
        return Math.PI * (this.radius * 2);
    }

    //you can add more concrete methods here if you need

}


public class Rectangle extends AShape {

    private double side;

    Rectangle(double side){
        this.side = side;
    }

    @Override
    public double calculatePerimeter() {
        return 4*this.side;
    }

    @Override
    public double calculateArea() {
        return Math.pow(this.side, this.side);
    }

}

//Main Class

public class Main {

    public static void main(String[] args) {

        Rectangle box = new Rectangle(2);
        System.out.printf("Box area is: %.2f\n", box.calculateArea());
        System.out.printf("Box perimeter is: %.2f\n", box.calculatePerimeter());

        Circle disc = new Circle(2);
        System.out.printf("Disc area is: %.2f\n", disc.calculateArea());
        System.out.printf("Disc perimeter is: %.2f\n",disc.calculatePerimeter());
    }
}

Implement it and throw an UnsupportedOperationException in it :

public boolean method1(String _str){
  throw new UnsupportedOperationException("message...");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM