SlideShare a Scribd company logo
Abstract and Final Classes in Java 8
1. Abstract Class
An abstract class in Java is a class that cannot be instantiated directly and may contain
abstract methods (methods without implementation). It is used as a base class for other
classes to provide a common structure.
 Key Features of Abstract Class:
o Can have both abstract (without body) and concrete (with body) methods.
o Cannot be instantiated directly.
o Must be inherited by a subclass.
o Can have constructors, fields, and static methods.
Abstract Class Example: Employee & Salary Structure
Program: Abstract Class Demonstration
// Abstract class
abstract class Employee {
String name;
int empId;
// Constructor
Employee(String name, int empId) {
this.name = name;
this.empId = empId;
}
// Abstract method (must be implemented by subclasses)
abstract double calculateSalary();
// Concrete method
void displayEmployee() {
System.out.println("Employee ID: " + empId + ", Name: " +
name);
}
}
// Concrete subclass
class Salary extends Employee {
double basicSalary, bonus;
Salary(String name, int empId, double basicSalary, double bonus)
{
super(name, empId);
this.basicSalary = basicSalary;
this.bonus = bonus;
}
// Implementing abstract method
@Override
double calculateSalary() {
return basicSalary + bonus;
}
}
// Main class
public class AbstractClassExample {
public static void main(String[] args) {
// Creating an object of Salary (concrete class)
Salary emp1 = new Salary("John Doe", 101, 50000, 5000);
// Displaying details
emp1.displayEmployee();
System.out.println("Total Salary: " +
emp1.calculateSalary());
}
}
Step-by-Step Explanation of Abstract Class Example
1. Define an Abstract Class (Employee)
 Has common attributes (name, empId).
 Has an abstract method calculateSalary(), which will be implemented in subclasses.
 Provides a concrete method displayEmployee() to show employee details.
2. Create a Subclass (Salary)
 Extends Employee and provides an implementation for calculateSalary().
 Adds basicSalary and bonus fields.
3. Create an Object of Subclass (Salary)
 An object of Salary is created in main() as abstract classes cannot be instantiated.
 Employee details are displayed using displayEmployee().
 Salary is calculated using calculateSalary().
2. Final Class
A final class in Java is a class that cannot be inherited. It is used when we want to restrict
modification of a class.
 Key Features of Final Class:
o Cannot be subclassed (inherited).
o Can have concrete methods but no abstract methods.
o Ensures the security and integrity of critical classes.
Final Class Example: Employee & Salary Structure
Program: Final Class Demonstration
// Final class
final class Employee {
private String name;
private int empId;
private double basicSalary, bonus;
// Constructor
Employee(String name, int empId, double basicSalary, double
bonus) {
this.name = name;
this.empId = empId;
this.basicSalary = basicSalary;
this.bonus = bonus;
}
// Method to calculate salary
public double calculateSalary() {
return basicSalary + bonus;
}
// Display Employee details
public void displayEmployee() {
System.out.println("Employee ID: " + empId + ", Name: " +
name);
System.out.println("Total Salary: " + calculateSalary());
}
}
// Main class
public class FinalClassExample {
public static void main(String[] args) {
// Creating an object of the final class Employee
Employee emp1 = new Employee("Alice Brown", 202, 60000,
8000);
// Display Employee details
emp1.displayEmployee();
}
}
Step-by-Step Explanation of Final Class Example
1. Define a Final Class (Employee)
o Declared as final, so it cannot be extended.
o Contains private attributes (name, empId, basicSalary, bonus).
o Provides methods to calculate salary (calculateSalary()) and display
details (displayEmployee()).
2. Create an Object of Employee
o Since Employee is final, it can be used as is but cannot be inherited.
o The main() method creates an object and calls displayEmployee() to show
details.
Advantages and Disadvantages
Advantages of Abstract Class
 Encapsulation & Code Reusability: Common features are defined in a base
class, reducing redundancy.
 Flexible Implementation: Allows mix of concrete and abstract methods.
 Polymorphism: Supports method overriding in subclasses.
Disadvantages of Abstract Class
 Limited Multiple Inheritance: Java does not support multiple inheritance with
classes (only interfaces).
 Performance Overhead: Requires subclassing, which may increase complexity.
Advantages of Final Class
o Security & Integrity: Prevents modification by subclassing.
o Thread Safety: Ensures consistency in multi-threaded applications.
o Optimization: The compiler can optimize final classes better.
Disadvantages of Final Class
o No Flexibility: Cannot be extended, limiting reuse.
o Not Suitable for Large Systems: Makes code less adaptable for changes.
Key Differences between Abstract and Final Class
Feature Abstract Class Final Class
Purpose Acts as a base for subclasses Prevents further extension
Instantiation Cannot be instantiated Can be instantiated
Methods Can have abstract & concrete methods Only concrete methods
Inheritance Must be inherited Cannot be inherited
Use Case Used for polymorphism & extensibility Used for security & stability
 Use an abstract class when you want a base class with common functionality and enforce
method implementation in child classes.
 Use a final class when you want to prevent inheritance and ensure a class remains
unchanged.

More Related Content

DOCX
Core java notes with examples
PDF
Exception handling and packages.pdf
PDF
Object-oriented programming (OOP) with Complete understanding modules
PDF
PPTX
Inheritance & interface ppt Inheritance
PPTX
Introduction to Java -unit-1
PPTX
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
PPT
oops with java modules i & ii.ppt
Core java notes with examples
Exception handling and packages.pdf
Object-oriented programming (OOP) with Complete understanding modules
Inheritance & interface ppt Inheritance
Introduction to Java -unit-1
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
oops with java modules i & ii.ppt

Similar to Java Abstract and Final Class for BCA students (20)

PDF
OOPs Interview Questions PDF By ScholarHat
PDF
Classes, objects, methods, constructors, this keyword in java
PDF
4 pillars of OOPS CONCEPT
DOCX
Introduction to object oriented programming concepts
PPTX
ITTutor Advanced Java (1).pptx
PPTX
Abstraction
PPTX
PPTX
iOS development introduction
PPT
Core JAVA Training.ppt
PPT
Object Oriented Programming (Advanced )
PPTX
Interface and abstraction
PPTX
it is about the abstract classes in java
PDF
11.Object Oriented Programming.pdf
PPT
Lecture 9
PDF
Intro to iOS Development • Made by Many
PDF
polymorphismpresentation-160825122725.pdf
PDF
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
PPTX
Polymorphism in C# Function overloading in C#
DOCX
Java oop concepts
PPT
Abstract_descrption
OOPs Interview Questions PDF By ScholarHat
Classes, objects, methods, constructors, this keyword in java
4 pillars of OOPS CONCEPT
Introduction to object oriented programming concepts
ITTutor Advanced Java (1).pptx
Abstraction
iOS development introduction
Core JAVA Training.ppt
Object Oriented Programming (Advanced )
Interface and abstraction
it is about the abstract classes in java
11.Object Oriented Programming.pdf
Lecture 9
Intro to iOS Development • Made by Many
polymorphismpresentation-160825122725.pdf
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
Polymorphism in C# Function overloading in C#
Java oop concepts
Abstract_descrption
Ad

More from Jainul Musani (20)

PDF
Core Java Interface Concepts for BCA Studetns
PDF
Java Collection Framework for BCA Students
PDF
Simple Calculator using JavaFx a part of Advance Java
PDF
JavaFx Introduction, Basic JavaFx Architecture
PDF
ASP.NET 2010, WebServices Full Example for BCA Students
PDF
Palindrome Programme in PHP for BCA students
PDF
Leap Year Program in PHP for BCA students
PDF
"PHP and MySQL CRUD Operations for Student Management System"
PDF
Python: The Versatile Programming Language - Introduction
PPTX
Python a Versatile Programming Language - Introduction
PDF
React js t8 - inlinecss
PDF
React js t7 - forms-events
PDF
React js t6 -lifecycle
PDF
React js t5 - state
PDF
React js t4 - components
PDF
React js t3 - es6
PDF
React js t2 - jsx
PDF
React js t1 - introduction
PPTX
ExpressJs Session01
PPTX
NodeJs Session03
Core Java Interface Concepts for BCA Studetns
Java Collection Framework for BCA Students
Simple Calculator using JavaFx a part of Advance Java
JavaFx Introduction, Basic JavaFx Architecture
ASP.NET 2010, WebServices Full Example for BCA Students
Palindrome Programme in PHP for BCA students
Leap Year Program in PHP for BCA students
"PHP and MySQL CRUD Operations for Student Management System"
Python: The Versatile Programming Language - Introduction
Python a Versatile Programming Language - Introduction
React js t8 - inlinecss
React js t7 - forms-events
React js t6 -lifecycle
React js t5 - state
React js t4 - components
React js t3 - es6
React js t2 - jsx
React js t1 - introduction
ExpressJs Session01
NodeJs Session03
Ad

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Spectroscopy.pptx food analysis technology
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation theory and applications.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Spectral efficient network and resource selection model in 5G networks
Network Security Unit 5.pdf for BCA BBA.
Reach Out and Touch Someone: Haptics and Empathic Computing
Spectroscopy.pptx food analysis technology
Assigned Numbers - 2025 - Bluetooth® Document
A comparative analysis of optical character recognition models for extracting...
Machine learning based COVID-19 study performance prediction
Encapsulation theory and applications.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
SOPHOS-XG Firewall Administrator PPT.pptx
Electronic commerce courselecture one. Pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf

Java Abstract and Final Class for BCA students

  • 1. Abstract and Final Classes in Java 8 1. Abstract Class An abstract class in Java is a class that cannot be instantiated directly and may contain abstract methods (methods without implementation). It is used as a base class for other classes to provide a common structure.  Key Features of Abstract Class: o Can have both abstract (without body) and concrete (with body) methods. o Cannot be instantiated directly. o Must be inherited by a subclass. o Can have constructors, fields, and static methods. Abstract Class Example: Employee & Salary Structure Program: Abstract Class Demonstration // Abstract class abstract class Employee { String name; int empId; // Constructor Employee(String name, int empId) { this.name = name; this.empId = empId; } // Abstract method (must be implemented by subclasses) abstract double calculateSalary(); // Concrete method void displayEmployee() { System.out.println("Employee ID: " + empId + ", Name: " + name); } } // Concrete subclass class Salary extends Employee { double basicSalary, bonus; Salary(String name, int empId, double basicSalary, double bonus) { super(name, empId); this.basicSalary = basicSalary;
  • 2. this.bonus = bonus; } // Implementing abstract method @Override double calculateSalary() { return basicSalary + bonus; } } // Main class public class AbstractClassExample { public static void main(String[] args) { // Creating an object of Salary (concrete class) Salary emp1 = new Salary("John Doe", 101, 50000, 5000); // Displaying details emp1.displayEmployee(); System.out.println("Total Salary: " + emp1.calculateSalary()); } } Step-by-Step Explanation of Abstract Class Example 1. Define an Abstract Class (Employee)  Has common attributes (name, empId).  Has an abstract method calculateSalary(), which will be implemented in subclasses.  Provides a concrete method displayEmployee() to show employee details. 2. Create a Subclass (Salary)  Extends Employee and provides an implementation for calculateSalary().  Adds basicSalary and bonus fields. 3. Create an Object of Subclass (Salary)  An object of Salary is created in main() as abstract classes cannot be instantiated.  Employee details are displayed using displayEmployee().  Salary is calculated using calculateSalary().
  • 3. 2. Final Class A final class in Java is a class that cannot be inherited. It is used when we want to restrict modification of a class.  Key Features of Final Class: o Cannot be subclassed (inherited). o Can have concrete methods but no abstract methods. o Ensures the security and integrity of critical classes. Final Class Example: Employee & Salary Structure Program: Final Class Demonstration // Final class final class Employee { private String name; private int empId; private double basicSalary, bonus; // Constructor Employee(String name, int empId, double basicSalary, double bonus) { this.name = name; this.empId = empId; this.basicSalary = basicSalary; this.bonus = bonus; } // Method to calculate salary public double calculateSalary() { return basicSalary + bonus; } // Display Employee details public void displayEmployee() { System.out.println("Employee ID: " + empId + ", Name: " + name); System.out.println("Total Salary: " + calculateSalary()); } } // Main class public class FinalClassExample { public static void main(String[] args) { // Creating an object of the final class Employee Employee emp1 = new Employee("Alice Brown", 202, 60000, 8000); // Display Employee details emp1.displayEmployee(); } }
  • 4. Step-by-Step Explanation of Final Class Example 1. Define a Final Class (Employee) o Declared as final, so it cannot be extended. o Contains private attributes (name, empId, basicSalary, bonus). o Provides methods to calculate salary (calculateSalary()) and display details (displayEmployee()). 2. Create an Object of Employee o Since Employee is final, it can be used as is but cannot be inherited. o The main() method creates an object and calls displayEmployee() to show details. Advantages and Disadvantages Advantages of Abstract Class  Encapsulation & Code Reusability: Common features are defined in a base class, reducing redundancy.  Flexible Implementation: Allows mix of concrete and abstract methods.  Polymorphism: Supports method overriding in subclasses. Disadvantages of Abstract Class  Limited Multiple Inheritance: Java does not support multiple inheritance with classes (only interfaces).  Performance Overhead: Requires subclassing, which may increase complexity. Advantages of Final Class o Security & Integrity: Prevents modification by subclassing. o Thread Safety: Ensures consistency in multi-threaded applications. o Optimization: The compiler can optimize final classes better. Disadvantages of Final Class o No Flexibility: Cannot be extended, limiting reuse. o Not Suitable for Large Systems: Makes code less adaptable for changes. Key Differences between Abstract and Final Class Feature Abstract Class Final Class Purpose Acts as a base for subclasses Prevents further extension Instantiation Cannot be instantiated Can be instantiated Methods Can have abstract & concrete methods Only concrete methods Inheritance Must be inherited Cannot be inherited Use Case Used for polymorphism & extensibility Used for security & stability  Use an abstract class when you want a base class with common functionality and enforce method implementation in child classes.  Use a final class when you want to prevent inheritance and ensure a class remains unchanged.