1

The following series of classes and interfaces compil. But the following class, after this code, does not.

public abstract class ClassA {
    public void methodOne() {
        System.out.println("Hello from methodOne.");
    }

    public int methodTwo(int a, int b) {
        return a + b;
    }

    public abstract int methodThree(int a, int b);
}

public interface InterfaceA {
    int methodOne(int a, int b);
}

public class ClassB extends ClassA {
    public int methodOne(int a) {
        return 5 * a;
    }

    public int methodTwo(int a, int b) {
        return 2 * super.methodTwo(a, b);
    }

    public int methodThree(int a, int b) {
        return a * b;
    }
}

public class ClassC extends ClassB implements InterfaceA {

    public int methodOne(int a, int b) {
        return a % b;
    }

    public int methodThree(int a, int b) {
    return a ‐ b;
}

    public void methodFour(int a) {
        System.out.println("Here is number: " + a);
    }
}

public class ClassD extends ClassC {
    public void methodFour(int a, int b) {
        System.out.println("Here is the sum: " + (a + b));
    }

}

The following outputs, in the following class, give errors during compilation, but why? Also, what exactly does it mean when, for example, ClassA_Type bla = new ClassB()?

public class SomeClass {
    public static void main(String[] args) {

        ClassA oneA = new ClassB();
        System.out.println(oneA.methodOne(5));

        InterfaceA oneIA = new ClassD();
        oneIA.methodFour(6, 7);


   } 
}
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
galal27
  • 51
  • 1
  • 8
  • 3
    Consider adding the exact error messages. – Arnaud May 04 '17 at 15:50
  • @Berger The problem was in a past assignment, where the problem asked to state the output of SomeClass and write "error" where appropriate. – galal27 May 04 '17 at 15:52
  • 1
    "Also, what exactly does it mean when, for example, ClassA_Type bla = new ClassB()?" <- While Class A is abstract in your case and not an interface, this should explain it anyway (same principle): [What does it mean to “program to an interface”?](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – OH GOD SPIDERS May 04 '17 at 15:53
  • 1
    And where you are getting the problem, please specify clearly, your use of "following" is ambiguous and further information is not given properly and clearly. – hagrawal7777 May 04 '17 at 15:53

3 Answers3

2

Have a look at oracle documentation page on abstract classes and overriding

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.

Due to above reason, below statement throws compilation error ( since ClassA is abstract class and can't be instantiated)

ClassA twoA = new ClassA(); 

Regarding your query of

Also, what exactly does it mean when, for example, ClassA_Type bla = new ClassB()?

It means that concrete class ClassB , a sub-class of ClassA has been created.

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. In this case, abstract method methodThree has been implemented in ClassB.

The advantage of using

ClassA_Type bla = new ClassB()

instead of

ClassB_Type bla = new ClassB()

is:

In future, if ClassC extends ClassA, you can simply change

 ClassA_Type bla = new ClassC() 

The base class can be instantiated with any concrete implementation

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • Thanks! Also, if bla is of ClassA_Type, or of InterfaceA_Type, what does that mean/what exactly is it bound by based on the class type? – galal27 May 04 '17 at 16:07
  • 1
    If ClassC extends ClassB implements InterfaceA then Class C can be 1) ClassB type 2) Class A type (Since Class B extends Class A) 3) InterfaceA type – Ravindra babu May 04 '17 at 16:10
1
ClassA twoA = new ClassA(); 

you cant do this because ClassA is abstract, meaning only subclasses can be instance of, for example

ClassA twoA = new ClassB();
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
0

oneIA.methodFour(6, 7); won't compile because InterfaceA doesn't have a method methodFour. The compiler can only recognize​ members of the declared type of the variable.

The assignment you asked about is an example of a "widening conversion", which the JLS explains in detail. Read about it there.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10