Flat Preloader Icon

Abstract Classes

Note: Abstract classes are a fundamental concept in object-oriented programming (OOP) languages like Java, C#, and Python. They are used to create classes that cannot be instantiated on their own but serve as blueprints for other classes. Abstract classes are designed to be subclassed, and they often contain one or more abstract methods that must be implemented by any concrete (i.e., non-abstract) subclasses. Here are some key points about abstract classes:

What are abstract classes?

  • abstract adjective ‘abstrakt/ “existing in thought or as an idea but not having a physical or concrete existence”
  • A class which is declared abstract cannot be instantiated, but only subclassed.
  • It may of may not contain the abstract methods.
  • Useful in cases where super class needs to provide a combination of implemented methods, as well as abstract methods.

Creating and Using abstract classes

				
					<Modifier> abstract class <ClassName> 
extends <Super 
ClassName> implements <InterfaceNames>
//statement block
 } 
class <ClassName> extends < 
SuperClassName >{
//statement block
}

				
			

Here are some key points about abstract classes

  • Cannot be instantiated: You cannot create objects directly from an abstract class. Instead, you must create concrete subclasses that inherit from the abstract class and provide implementations for its abstract methods.
  • Blueprint for subclasses: Abstract classes define a common interface and some default behavior that should be shared among multiple related classes. Subclasses can inherit this interface and extend or customize it to suit their specific needs.
  • Abstract methods: Abstract classes often contain one or more abstract methods, which are declared in the abstract class but have no implementation. Subclasses must provide concrete implementations for these abstract methods, ensuring that they adhere to the interface defined by the abstract class.
  • Partial implementation: Abstract classes can also include concrete (non-abstract) methods that provide some default behavior. Subclasses can choose to override these methods or use the provided implementation.