Skip to content

Commit 2f47959

Browse files
committed
abstract added.
Signed-off-by: Someshdiwan <[email protected]>
1 parent fe07e69 commit 2f47959

File tree

3 files changed

+198
-23
lines changed

3 files changed

+198
-23
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
An abstract class in Java can contain both abstract and concrete methods.
2+
This is one of the key features that distinguishes abstract classes from interfaces (prior to Java 8).
3+
4+
An abstract class can have any of the following:
5+
6+
1. Abstract methods (methods without a body)
7+
2. Concrete methods (methods with a body)
8+
3. Instance variables
9+
4. Constructors
10+
5. Static methods and variables
11+
12+
This flexibility allows abstract classes to provide a common structure and some implemented functionality while
13+
leaving other methods to be implemented by subclasses.
14+
15+
For example:
16+
17+
```java
18+
abstract class Shape {
19+
protected String color;
20+
21+
public Shape(String color) {
22+
this.color = color;
23+
}
24+
25+
abstract double calculateArea(); // abstract method
26+
27+
public void displayColor() { // concrete method
28+
System.out.println("Color: " + color);
29+
}
30+
}
31+
```
32+
33+
34+
Yes, an abstract class can contain final methods.
35+
36+
Final methods in Java are methods that cannot be overridden by subclasses.
37+
38+
Including final methods in an abstract class can be useful when you want to provide a method implementation that
39+
should not be changed by any subclass.
40+
41+
This allows you to enforce certain behavior across all subclasses of the abstract class.
42+
43+
Here's an example:
44+
45+
```java
46+
abstract class Vehicle {
47+
abstract void startEngine();
48+
49+
final void stopEngine() {
50+
System.out.println("Engine stopped");
51+
}
52+
}
53+
54+
class Car extends Vehicle {
55+
@Override
56+
void startEngine() {
57+
System.out.println("Car engine started");
58+
}
59+
60+
// Cannot override stopEngine() as it's final in the superclass
61+
}
62+
```
63+
64+
Yes, an abstract class can extend another abstract class.
65+
66+
This is perfectly valid in Java and can be useful for creating a hierarchy of abstract classes,
67+
68+
each adding or refining abstract or concrete methods.
69+
70+
When an abstract class extends another abstract class, it is not required to implement the abstract methods of its
71+
superclass.
72+
73+
It can choose to implement some, all, or none of the abstract methods,
74+
and can also add new abstract or concrete methods.
75+
76+
For example:
77+
78+
```java
79+
abstract class Shape {
80+
abstract double calculateArea();
81+
}
82+
83+
abstract class TwoDimensionalShape extends Shape {
84+
abstract double calculatePerimeter();
85+
}
86+
87+
class Circle extends TwoDimensionalShape {
88+
private double radius;
89+
90+
public Circle(double radius) {
91+
this.radius = radius;
92+
}
93+
94+
@Override
95+
double calculateArea() {
96+
return Math.PI * radius * radius;
97+
}
98+
99+
@Override
100+
double calculatePerimeter() {
101+
return 2 * Math.PI * radius;
102+
}
103+
}
104+
```
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
```

Section13AbstractClasses/src/abstract.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)