|
| 1 | +You cannot create an instance of an abstract class in Java. |
| 2 | + |
| 3 | +Abstract classes are designed to be incomplete and serve as a base for other classes. |
| 4 | + |
| 5 | +They may contain abstract methods, which have no implementation, making it impossible to instantiate them directly. |
| 6 | + |
| 7 | +However, you can create instances of concrete subclasses that extend the abstract class and provide implementations |
| 8 | +for all abstract methods. |
| 9 | + |
| 10 | +What happens if a subclass of an abstract class doesn't implement all the abstract methods? |
| 11 | +Ans: |
| 12 | +If a subclass of an abstract class doesn't implement all the abstract methods, the subclass itself becomes abstract. |
| 13 | +This means that the subclass cannot be instantiated either. To create a concrete class, all abstract methods |
| 14 | +from the superclass must be implemented. |
| 15 | + |
| 16 | +If you want to leave some methods unimplemented, you must declare the subclass as abstract as well: |
| 17 | + |
| 18 | +```java |
| 19 | +abstract class Shape { |
| 20 | + abstract double calculateArea(); |
| 21 | +} |
| 22 | + |
| 23 | +abstract class Rectangle extends Shape { |
| 24 | + // calculateArea() is not implemented, so Rectangle is also abstract |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +Yes, an abstract class can have a constructor in Java. Although abstract classes cannot be instantiated directly, |
| 29 | +they can have constructors. |
| 30 | + |
| 31 | +These constructors are called when an instance of a concrete subclass is created. |
| 32 | + |
| 33 | +Constructors in abstract classes are useful for initializing common fields or performing common setup operations for |
| 34 | +all subclasses. |
| 35 | + |
| 36 | +For example: |
| 37 | + |
| 38 | +```java |
| 39 | +abstract class Animal { |
| 40 | + protected String name; |
| 41 | + |
| 42 | + public Animal(String name) { |
| 43 | + this.name = name; |
| 44 | + } |
| 45 | + |
| 46 | + abstract void makeSound(); |
| 47 | +} |
| 48 | + |
| 49 | +class Dog extends Animal { |
| 50 | + public Dog(String name) { |
| 51 | + super(name); // Calls the constructor of the abstract class |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + void makeSound() { |
| 56 | + System.out.println(name + " barks"); |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +A key difference between abstract classes and interfaces in Java is that abstract classes can have constructors, |
| 62 | +while interfaces cannot. |
| 63 | + |
| 64 | +Abstract classes can have instance variables, constructors, and both abstract and non-abstract methods. |
| 65 | + |
| 66 | +Interfaces, on the other hand, can only have constants (public static final fields), abstract methods, default methods, |
| 67 | +and static methods. |
| 68 | + |
| 69 | +They cannot have constructors or instance variables. |
| 70 | +This difference allows abstract classes to initialize state and provide common implementation, |
| 71 | +while interfaces focus on defining a contract for implementing classes. |
| 72 | + |
| 73 | +When overriding an abstract method in a subclass, you can use the same access modifier as the abstract method or a less |
| 74 | +restrictive one. |
| 75 | + |
| 76 | +This follows the general rule for method overriding in Java. For example, if an abstract method is declared as protected, |
| 77 | +the overriding method in the subclass can be declared as protected or public, but not private. |
| 78 | + |
| 79 | +This allows for greater flexibility in the subclass while maintaining the contract defined by the abstract class. |
| 80 | + |
| 81 | +For instance: |
| 82 | + |
| 83 | +```java |
| 84 | +abstract class Parent { |
| 85 | + protected abstract void method(); |
| 86 | +} |
| 87 | + |
| 88 | +class Child extends Parent { |
| 89 | + @Override |
| 90 | + public void method() { // Less restrictive, so it's allowed |
| 91 | + // implementation |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
0 commit comments