0% found this document useful (0 votes)
11 views7 pages

Inheritance and Abstract Classes in Java

The document outlines a programming assignment focused on inheritance, interfaces, and abstract classes in Java. It includes multiple tasks such as creating classes for multilevel inheritance, defining an abstract class with subclasses, implementing an interface for geometric calculations, and creating a menu-driven program for managing student data in a hostel context. Each task is accompanied by code snippets demonstrating the required functionality.

Uploaded by

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

Inheritance and Abstract Classes in Java

The document outlines a programming assignment focused on inheritance, interfaces, and abstract classes in Java. It includes multiple tasks such as creating classes for multilevel inheritance, defining an abstract class with subclasses, implementing an interface for geometric calculations, and creating a menu-driven program for managing student data in a hostel context. Each task is accompanied by code snippets demonstrating the required functionality.

Uploaded by

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

Assignment 3: Inheritance and Interfaces

Set A
a) Write a program for multilevel inheritance such that country is inherited from
continent. State is inherited from country. Display the place, state, country and
continent.

import [Link].*;

class Continent

String continent= "Asia";

class Country extends Continent

String country= "India";

class State extends Country

String state= "Maharshatra";

class Place extends State

String place= "Satara";

void Display()

[Link]("place name:" +place);

[Link]("state name:" +state);

[Link]("country name:" +country);

[Link]("continent name:" +continent);

class Inheritance

{
public static void main(String args[])

Place p = new Place()

[Link]();

b) Define an abstract class Staff with protected members id and name. Define a
parameterized constructor. Define one subclass OfficeStaff with member
department. Create n objects of OfficeStaff and display all details.

import [Link];

abstract class Staff

protected int id;

protected String name;

Staff(int id, String name)

[Link]= id;

[Link]= name;

class Officestaff extends Staff

String department;

Officestaff(int id, String name, String department)

{
super(id, name);

[Link] = department;

void Display()

[Link]("id: " + id + ",name: " + name + ",department: " + department);

class Ass32

public static void main(String args[])

Scanner sc = new Scanner([Link]);

[Link]("Enter number of staff members:");

int n = [Link]();

Officestaff[] obj = new Officestaff[n];

for(int i=0; i<n; i++)

[Link]("Enter details of the staff:");

[Link]("ID:");

int id = [Link]();

[Link]();

[Link]("name: ");

String name= [Link]();

[Link]("department: ");

String dept = [Link]();

obj[i] = new Officestaff(id, name, dept);

[Link]("All Staff Details.");

for(Officestaff s : obj)

[Link]();

}
}

c) Define an interface “Operation” which has methods area(),volume().Define a


constant PI having a value [Link] a class cylinder which implements this
interface (members – radius, height) Create one object and calculate the area
and volume. d) Write a program to find the cube of given number using function
interface.

interface Operation

double PI = 3.142;

public void area();

public void volume();

public class Cylinder implements Operation

int radius = 4;

int height = 9;

public void area()


{

double carea = 2 * PI * radius * radius * height;

[Link]("Area of cylinder is:" +carea);

public void volume()

double vol = PI * radius * radius * height;

[Link]("volume of cylinder:" +vol);

public static void main (String[] args)

Cylinder c = new Cylinder();

[Link]();

[Link]();

d) Write a program to find the cube of given number using function interface.

import [Link].*;

interface Cube

public void User();

public class Ass33 implements Cube

Scanner sc = new Scanner([Link]);

public void User()

{
[Link]("Enter the number:");

int num = [Link]();

int cube = num * num * num;

[Link]("cube of given number is:" +cube);

public static void main(String args[])

Ass33 a = new Ass33();

[Link]();

Set B
a) Create an abstract class “order” having members id,[Link] two
subclasses “Purchase Order” and “Sales Order” having members customer name
and Vendor name [Link] methods accept and display in all cases.
Create 3 objects each of Purchase Order and Sales Order and accept and display
details.

b) Write a program to using marker interface create a class product(product_id,


product_name, product_cost, product_quantity) define a default and
parameterized constructor. Create objects of class product and display the
contents of each object and Also display the object count.

Set C
a) Create an interface Department containing attributes deptName and
deptHead. It also has abstract methods for printing the attributes. Create a class
hostel containing hostelName, hostelLocation and numberOfRooms. The class
contains method printing the attributes. Then write Student class extending the
Hostel class and implementing the Department interface. This class contains
attributes studentName, regNo, electiveSubject and avgMarks. Write suitable
printData method for this class. Also, implement the abstract methods of the
Department interface. Write a driver class to test the Student class. The program
should be menu driven containing the options:
i. Admit new student.
ii. Migrate a student.
iii. Display details of a student
For the third option, a search is to be made on the basis of the entered
registration Number.

Common questions

Powered by AI

Encapsulation ensures that data attributes are not directly accessible from outside the class, promoting security and integrity. In the 'OfficeStaff' class, encapsulation is demonstrated via the protected access modifier for inherited id and name attributes, allowing secure initialization through constructors while providing controlled expose via display methods. For 'Student', data concerning registration, names, and other personal details are likely encapsulated, accessed, and modified only through interface-implemented methods. This practice promotes loose coupling and maintains internal states, reinforcing safe interactions .

The logical structure in combining inheritance and interface implementation involves organizing a system where derived classes and the classes implementing interfaces efficiently utilize inherited properties and specialized behaviors. This dual approach leverages Java's design patterns: multilevel inheritance for hierarchical structuring of related entities like 'Place', and interfaces like 'Operation' for defining capabilities shared across unrelated classes such as 'Cylinder'. These considerations require forethought to balance reusability, flexibility, and maintainability while keeping the codebase comprehensible and robust against future scale or feature additions .

Multilevel inheritance in Java allows a class to inherit from a superclass, which itself is a subclass of another superclass, creating a hierarchy. This organization promotes the reuse of code and logical groupings, making the system easier to understand and extend. In the example given, the 'Continent', 'Country', 'State', and 'Place' classes demonstrate multilevel inheritance where properties are inherited down the chain, allowing the display of hierarchical information about a place .

Utilizing interfaces like 'Operation' to calculate geometric properties fosters a systematic approach to defining what behavior a class must provide without enforcing how it should be done. With 'Cylinder', such an interface allows for precise, uniform handling of operations like area and volume calculations, enforcing a clear contract that ensures all implementing classes provide necessary computational methods. This is especially effective when aiming to extend capabilities to other geometric forms, as it allows implementations to vary behavior while maintaining expected operations. However, rigorous interface adherence can increase initial complexity and integration effort .

Abstract methods in interfaces mandate implementing classes to provide concrete implementations of these methods, ensuring object behaviors adhere to a set protocol. This enhances polymorphism and decouples object behavior from implementation specifics, maintaining flexibility and scalability. For the 'Department' interface with attributes 'deptName' and 'deptHead', abstract methods dictate how these should be printed. The 'Student' class, which extends from 'Hostel' and implements 'Department', must provide a concrete 'printData' method, thus following a standardized approach while remaining versatile for extended functionalities .

A menu-driven student management system in Java integrates features for admitting, migrating, and displaying student details by implementing classes and methods that manage student records effectively. Using a 'Student' class that extends 'Hostel' and implements 'Department', and adopting a menu-driven input handling mechanism, different operations can be interactive and user-friendly. This involves designing abstract methods for setting and printing attributes, and a robust searching mechanism based on registration numbers for displaying specific student details. Functionality is further enhanced through encapsulating operations within methods and classes, facilitating a clean, extensible architecture .

Marker interfaces in Java are used to signal to the JVM or tools that a class has special behavior, without dictating any methods that need to be implemented. They can simplify checking for particular characteristics of a class instance at run-time, as seen where a 'Product' class might implement a marker interface to indicate certain properties. However, since marker interfaces do not carry any operations or properties, they may limit flexibility and can be considered less robust compared to annotation based mechanisms introduced in later versions of Java .

Interfaces in Java allow defining constant values and method signatures that must be implemented by any class that implements the interface. This provides a standard protocol. The 'Operation' interface includes the constant 'PI' and method signatures 'area()' and 'volume()'. The 'Cylinder' class implements these methods to calculate and print the specified area and volume, ensuring any class that implements 'Operation' will provide this functionality .

The nested hierarchy practiced in the 'Continent > Country > State > Place' structure leverages inheritance to build class relationships that mirror real-world hierarchies, emphasizing reusability and extension. This approach simplifies code management by congregating generalized behavior and attributes in top-level classes like 'Continent', while specialized features reside in subclasses like 'Place'. It enhances readability through logical flow, making code intuitive to follow, and easier to maintain or extend with additional related concepts without altering existing structures .

In Java, an abstract class can have a constructor, but it cannot be instantiated directly; instead, subclasses should handle instantiation. Protected access for members is advisable when descendant classes should access member data without exposing it to other packages. In the provided example, the abstract class 'Staff' has a constructor that initializes 'id' and 'name', which are protected. The 'OfficeStaff' subclass extends 'Staff', calling the superclass constructor using 'super(id, name)' to initialize the inherited attributes, and adds its own member 'department' .

You might also like