0% found this document useful (0 votes)
17 views3 pages

Java Abstract Class Example

The document contains Java code defining an abstract class 'Department' and a subclass 'Faculty' that extends it. The 'Faculty' class includes methods to display faculty details and department name. A main class creates an instance of 'Faculty' and displays its details, demonstrating the use of inheritance and abstraction in Java programming.

Uploaded by

Subham Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Java Abstract Class Example

The document contains Java code defining an abstract class 'Department' and a subclass 'Faculty' that extends it. The 'Faculty' class includes methods to display faculty details and department name. A main class creates an instance of 'Faculty' and displays its details, demonstrating the use of inheritance and abstraction in Java programming.

Uploaded by

Subham Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Registration Number: 12206671 Course Code:CAP680

Roll No: B65 Section: D2214 Group- 2

Sandbox Link:
[Link]

Code:
// Abstract Class Department

abstract class Department {

String dept_name;

public Department(String dept_name) {

this.dept_name = dept_name;

public abstract void display_department_name();

// Faculty Class extending from Department

class Faculty extends Department {

String faculty_name;

int faculty_id;

String[] courses;

public Faculty(String faculty_name, int faculty_id, String dept_name, String[] courses) {

super(dept_name);

this.faculty_name = faculty_name;

this.faculty_id = faculty_id;

[Link] = courses;

Page 1 of 5
Registration Number: 12206671 Course Code:CAP680

Roll No: B65 Section: D2214 Group- 2

public void display_department_name() {

[Link]("Department: " + dept_name);

public void display_faculty_details() {

[Link]("Faculty Name: " + faculty_name);

[Link]("Faculty Id: " + faculty_id);

display_department_name();

[Link]("Courses: " + [Link](", ", courses));

// Main class

public class Main {

public static void main(String[] args) {

// Creating an instance of Faculty class

Faculty f = new Faculty("Kumar", 12345, "SCA", new String[]{"CAP615", "CAP680", "JAVA solution"});

// Displaying Faculty details

f.display_faculty_details();

Screen Shot of Sandbox with code:

Page 2 of 5
Registration Number: 12206671 Course Code:CAP680

Roll No: B65 Section: D2214 Group- 2

Page 3 of 5

Common questions

Powered by AI

If the method 'display_department_name' was not implemented in the 'Faculty' class, the program would not compile. As 'Department' is an abstract class requiring 'display_department_name' to be defined, all subclasses, including 'Faculty', must provide a concrete implementation of this method. Failing to do so would result in a compilation error, as 'Faculty' would not meet the contractual obligations prescribed by its superclass .

The abstract class 'Department' enhances code reusability and maintainability by providing a shared contract for any subclasses like 'Faculty'. By defining the 'display_department_name' method as abstract, it ensures that any subclass must implement it, promoting uniformity across the codebase. This design pattern allows developers to add new types of department-related classes that share a common interface, facilitating easier updates and expansions without altering existing code substantially .

Inheritance is used in this code through the relationship between the 'Department' and 'Faculty' classes. The 'Faculty' class inherits from the 'Department' class, gaining access to its properties and methods. By doing this, 'Faculty' can extend the functionality of 'Department', including the ability to manipulate and display department names, while also introducing new attributes and behaviors specific to faculty members, such as faculty details and courses taught .

The 'main' class in this Java program serves as the entry point for execution. Its primary role is to create an instance of the 'Faculty' class and invoke the method 'display_faculty_details' to demonstrate the functionality of the class hierarchy. This setup allows the program to execute and display the faculty and department details using the console output .

The 'Faculty' class uses an array to store and handle multiple course assignments, utilizing a 'String[]' for the 'courses' field. This data structure is appropriate because it allows the storage of a fixed number of course names, enabling the organization and retrieval of course information in a simple, indexed manner. Arrays are suitable for this use case due to their efficiency in accessing elements and their compatibility with routine operations such as iteration and display .

Encapsulation in the 'Faculty' class is achieved by declaring class properties such as 'faculty_name', 'faculty_id', and 'courses' as private. The class provides public methods like 'display_faculty_details' to interact with these properties. This ensures that the internal representation of the object's state is hidden from the outside, allowing controlled access and modification through well-defined interfaces .

The abstract class 'Department' in object-oriented programming is used to define a common interface for all department-related subclasses without providing implementation details. In the context of this code example, 'Department' acts as a superclass for the 'Faculty' class, forcing any subclass to implement the 'display_department_name' method. This approach facilitates code reusability and consistency across various department-related classes .

The code enables faculty details to be printed to the console using the method 'display_faculty_details' within the 'Faculty' class. This method outputs the faculty name, ID, department name, and courses by calling 'System.out.println' for each attribute. Additionally, it calls the 'display_department_name' method to show the department name on the console .

The 'Faculty' class implements polymorphism by extending the abstract class 'Department' and providing a concrete implementation of the abstract method 'display_department_name'. This allows the 'Faculty' class to offer a specific behavior for displaying department names, demonstrating polymorphism through method overriding. This ensures that even though 'Faculty' instances are accessed through a 'Department' reference, they execute the overridden method defined in the 'Faculty' class .

The 'super' keyword in the constructor of the 'Faculty' class is used to call the constructor of its superclass 'Department'. This initializes the 'dept_name' field of the 'Faculty' class's superclass. Using 'super' ensures that the superclass's state is properly initialized before additional functionalities are added in the subclass constructor .

You might also like