Understanding the purpose of Abstract Classes in Java

前端 未结 8 1648
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 06:15

Suppose I have two Classes, A and B. The A class is defined as abstract, while B extends this abstract class, and finally i test the result and both classes are part of same

相关标签:
8条回答
  • 2020-12-01 06:23

    Abstract Classes allow you to store base level implementations (inheritance), while also producing a contract that guarantees inherited classes will implement specific functionality (interface) on their own...

    While there will never be a instance of an Abstract Class, you can store shared functionality between inheritors...

    I've always thought of them as Interfaces that allow base level implementations of certain methods...

    0 讨论(0)
  • 2020-12-01 06:24

    What is the purpose of @Override keyword, because if i omit it, it still works the same.

    Legibility.

    If i don't implement the abstract method, i get compilation error. So what is the difference to implementing an interface?

    Interfaces don't allow you to include code.

    Also, i can implement method2() in B as well. Then the output changes to what is use in B. Isn't this also overriding the parent class method. Then what is the purpose of explicitly Defining a method abstract in Class A?

    Yes it is overriding the parent method. You usually define a method as abstract to leave the implementation for the child (which MUST implement it or be abstract). If it's not abstract, you have to implement it.

    0 讨论(0)
  • 2020-12-01 06:30
    1. @Override is not a keyword, it is an optional annotation that helps the compiler check that you indeed are overriding a method. If you say @Override but there is no method to override, the compiler will tell you that you've probably made a typo. Rename method1 to method12 to see the effect.
    2. Interface cannot have any implementations, while abstract class may optionally provide implementations for some of its methods. In addition, interfaces cannot have data members.
    3. Defining a method as abstract means that the derived class must provide an implementation. Not declaring it abstract says that derived classes simply can provide their own implementation, but they do not need to.
    0 讨论(0)
  • 2020-12-01 06:31

    What is the purpose of @Override keyword

    It's later Java addition, which is for weak self-control. It's annotation, i.e. library class, not keyword.

    So what is the difference to implementing an interface?

    Little. Iterface is a class where all methods are abstract. Also, only one class parent is allowed, while it can be multiple interfaces. Also you can put members into class, but not into interface.

    Then what is the purpose of explicitly Defining a method abstract in Class A?

    You should declare it abstract only if it has no body. It is required for JVM to know that all descendants will have it.

    0 讨论(0)
  • 2020-12-01 06:32

    The real usage comes into picture when you have multiple classes implementing and method1 is being invoked from method2. In this case, after executing method2, method1 in corresponding class will be executed.

    public abstract class A {
    
        protected abstract void method1(int val); 
    
        protected void method2() { 
            System.out.println("This is Class A's method");
            //Let us I calculated something here, may be sum of two ints 
            method1(//pass above calculated int);
        } 
    }
    
    public class B extends A {
    
        @Override
        protected void method1(int val) {
            System.out.println("This is B's implementaiton of A's method");
            //Here you may do this class specific manipulation on val.
        } 
    }  
    

    Like that in class C you can manipulate common calculated some.

    0 讨论(0)
  • 2020-12-01 06:33

    The main difference between abstract classes and interfaces, is that interfaces only define an interface and abstract classes can also have an implementation and fields/properties.

    An abstract class can be seen as a IS-A relation (e.g. a horse is an animal). For interface it mostly is some feature a class can have (e.g. INavigable, ISortable).

    Also abstract methods cannot be instantiated (neither to interfaces).

    Overriding functions are to show that a function has a base class implementation.

    Also multiple inheritance is not possible/adviced with classes, therefore max. use only one base class and inherit from interfaces for other features.

    0 讨论(0)
提交回复
热议问题