0% found this document useful (0 votes)
6 views

In Java

Uploaded by

aishorjoshuchi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

In Java

Uploaded by

aishorjoshuchi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

In Java, abstract is a keyword used primarily with classes and methods, enabling the creation

of abstract classes and abstract methods. It plays a critical role in object-oriented programming
(OOP) by facilitating the creation of base classes and enforcing method implementation in
subclasses. Below, I'll cover everything about the abstract concept in Java in detail.

1. Abstract Classes

An abstract class in Java is a class that cannot be instantiated on its own. It is meant to be
subclassed, and its abstract methods (if any) must be implemented by its subclasses. The primary
purpose of an abstract class is to provide a common base and some default behavior for other
classes to build upon.

Key Characteristics of Abstract Classes:

• Cannot be instantiated: You cannot create objects directly from an abstract class using
the new keyword.
• May have abstract methods: An abstract class can have abstract methods, which are
methods without implementation. These methods must be implemented by the subclasses.
• May have concrete (non-abstract) methods: An abstract class can also contain methods
with an implementation. This allows the abstract class to provide default functionality to
its subclasses.
• Can have instance variables: Abstract classes can have fields, including both static and
instance variables.
• Can have constructors: Abstract classes can have constructors, which can be called by
subclasses during instantiation.

Example of Abstract Class:

java
Copy code
abstract class Animal {
// Abstract method (no implementation)
abstract void sound();

// Concrete method (with implementation)


void sleep() {
System.out.println("This animal is sleeping.");
}
}

class Dog extends Animal {


// Implementing the abstract method
void sound() {
System.out.println("Woof! Woof!");
}
}

public class Main {


public static void main(String[] args) {
// Animal animal = new Animal(); // Error: Cannot instantiate the
abstract class Animal

Dog dog = new Dog();


dog.sound(); // Woof! Woof!
dog.sleep(); // This animal is sleeping.
}
}

In the above example:

• Animal is an abstract class with an abstract method sound() and a concrete method
sleep().
• The Dog class extends Animal and provides an implementation for the sound() method.
• Animal cannot be instantiated, but Dog can be.

2. Abstract Methods

An abstract method is a method that is declared in an abstract class but has no implementation
(i.e., it ends with a semicolon rather than curly braces).

• Syntax:

java
Copy code
abstract returnType methodName(parameters);

• Abstract methods must be implemented by subclasses: Any non-abstract subclass


must provide its own implementation for all the abstract methods inherited from the
abstract class.

Example of Abstract Method:

java
Copy code
abstract class Vehicle {
abstract void startEngine();
}

class Car extends Vehicle {


@Override
void startEngine() {
System.out.println("Car engine started.");
}
}

public class Main {


public static void main(String[] args) {
// Vehicle v = new Vehicle(); // Error: Cannot instantiate the
abstract class Vehicle
Car car = new Car();
car.startEngine(); // Output: Car engine started.
}
}

Here:

• Vehicle has an abstract method startEngine().


• Car provides its implementation of startEngine().

3. Abstract Classes vs Interfaces

Java has both abstract classes and interfaces, and they can sometimes serve similar purposes.
However, there are key differences between the two:

• Abstract Class: Can have both abstract methods (without implementation) and concrete
methods (with implementation). A class can extend only one abstract class (single
inheritance).
• Interface: All methods in an interface are abstract by default (though starting from Java
8, interfaces can have default and static methods with implementations). A class can
implement multiple interfaces (multiple inheritance).

Key Differences:

Feature Abstract Class Interface


Can have both abstract and concrete Can have abstract, default, and
Method types
methods static methods
Can only have static final fields
Fields Can have instance fields
(constants)
Constructors Can have constructors Cannot have constructors
Multiple A class can inherit from only one A class can implement multiple
Inheritance abstract class interfaces
Can have any access modifiers (public,
Access Modifiers Methods are implicitly public
private, etc.)

4. Why Use Abstract Classes?

Abstract classes are particularly useful when:

• You want to provide a common interface for all subclasses: You can define common
methods in the abstract class and force subclasses to implement abstract methods.
• You want to share common functionality: Abstract classes can have implemented
methods that can be shared across multiple subclasses.
• You want to create a base class that cannot be instantiated: An abstract class serves
as a blueprint for other classes.
5. Abstract Classes with Abstract Methods

If a class contains abstract methods, that class must be declared abstract. However, if an abstract
class contains only concrete methods (with implementations), it can technically be instantiated
(unless it is abstract for other reasons).

Example of Abstract Class with Only Abstract Methods:

java
Copy code
abstract class Shape {
abstract void draw();
}

class Circle extends Shape {


void draw() {
System.out.println("Drawing a circle");
}
}

class Square extends Shape {


void draw() {
System.out.println("Drawing a square");
}
}

public class Main {


public static void main(String[] args) {
Shape shape1 = new Circle();
shape1.draw(); // Drawing a circle

Shape shape2 = new Square();


shape2.draw(); // Drawing a square
}
}

6. Abstract Classes and Polymorphism

Abstract classes are often used in combination with polymorphism. For example, you can use an
abstract class type to refer to objects of subclasses, allowing for dynamic method calls based on
the actual object type.

Example:

java
Copy code
abstract class Animal {
abstract void makeSound();
}

class Dog extends Animal {


void makeSound() {
System.out.println("Bark");
}
}

class Cat extends Animal {


void makeSound() {
System.out.println("Meow");
}
}

public class Main {


public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();

myDog.makeSound(); // Bark
myCat.makeSound(); // Meow
}
}

7. Key Points to Remember About Abstract Classes in Java:

• You cannot instantiate an abstract class directly.


• Abstract methods must be implemented by the subclass unless the subclass is also
abstract.
• Abstract classes can have constructors, instance variables, and fully implemented
methods.
• Abstract classes allow for creating a common structure or behavior across subclasses,
while still allowing flexibility for the subclasses to implement certain behavior.

Conclusion:

Abstract classes in Java are a foundational concept of object-oriented programming, providing a


way to define a common interface for a group of related classes while allowing each subclass to
implement its own specific behavior. By using abstract methods and concrete methods together,
abstract classes help promote code reuse and enforce consistency in subclass design.

4o mini

You might also like