Lab 4
Lab 4
Java Inheritance
Types of Inheritance:
1. Single Inheritance :
import java.io.*;
// Base or Super Class
class Employee {
int salary = 60000;
}
class Main {
public static void main(String args[])
{
Engineer E1 = new Engineer();
System.out.println("Salary : " + E1.salary
+ "\nBenefits : " + E1.benefits);
}
//salary is accessible through Engineer class
object as it is the subclass of Employee
}
Example2:
class employee{
int salary = 6000;
void display(){
System.out.println("display");
}
}
void show(){
System.out.println("show");
}
}
class main7{
public static void main(String args[]){
//engineer e1 = new engineer();//subclass to
subclass//
//System.out.println("salary:" + e1.salary +
"\nbenefits:" + e1.benefits);
2. Multilevel Inheritance
import java.io.*;
class Animal{
void eat(){
System.out.println("eating...");}
}
3. Hierarchical Inheritance
import java.io.*;
class Animal{
void eat(){
System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){
System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){
System.out.println("meowing...");}
}
class Main{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
}}
but here if we call c.bark(); in Main class then it
would produce the following error:
This implies Dog and Cat class can inherit the properties of Animal class as they
extend it but they can't access variables and methods of each other as they are not
related to each other.
4. Multiple Inheritance
To reduce the complexity and simplify the language, multiple inheritance is not
supported in java. Consider a scenario where A, B, and C are three classes. The C
class inherits A and B classes. If A and B classes have the same method and you
call it from child class object, there will be ambiguity to call the method of A or B
class.
class A{
void msg(){
System.out.println("Hello");}
}
class B{
void msg()
{System.out.println("Welcome");}
}
class C extends A,B{
public static void main(String args[]){
C obj=new C();
obj.msg();
}
}
Output
The most common use of the super keyword is to eliminate the confusion between
superclasses and subclasses that have methods with the same name.
super can be used with (In below examples it is assumed Main class is created and
an object of subclass is created)
class Vehicle {
void display() {
+ super.maxSpeed);
If subclass (child class) has the same method as declared in the parent class, it is known as
method overriding in Java. In other words, If a subclass provides the specific implementation of
the method that has been declared by one of its parent class, it is known as method overriding.
Method overriding occurs only when the names and the type signatures of the
two methods are identical. If they are not, then the two methods are simply
overloaded.
Example
class Vehicle{
//defining a method
void run(){
System.out.println("Vehicle is running");}
void run(){
System.out.println("Bike is running");}
class Main{
obj.run();
Output
Bike is running
Method overriding forms the basis for one of Java’s most powerful concepts: dynamic
method dispatch. Dynamic method dispatch is the mechanism by which a call to an
overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is
important because this is how Java implements run-time polymorphism.
Interfaces
// interface
interface Animal {
public void animalSound(); // interface method (does
not have a body)
public void run(); // interface method (does not have
a body)
}
To access the interface methods, the interface must be "implemented" (kind of like
inherited) by another class with the implements keyword (instead of extends). The
body of the interface method is provided by the "implement" class
// Interface
interface Animal {
public void animalSound(); // interface method (does
not have a body)
public void sleep(); // interface method (does not
have a body)
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
Abstract class: A class in Java can contain both methods and variables. In
some situations, where it's not possible to provide a method's
implementation but only its declaration, the abstract keyword is used.
//Concrete method
public int getSpeed { return speed; }
public String getManufacturer() {
return manufacturer;
}
//abstract method (only declaration)
public abstract double computeTax();
// Final class
public final class FinalClass {
Example:
class SuperClass {
// Final method
public final void finalMethod() {
System.out.println("This is a final method.");
}
// Non-final method
public void nonFinalMethod() {
System.out.println("This is a non-final method.");
}
}
@Override
public void nonFinalMethod() {
System.out.println("Overridden non-final method.");
}
}
Static Class: In Java, a static class is a class that is nested within another
class and is declared with the static keyword. This concept is specific to
nested classes.
Example:
// Outer class
public class OuterClass {
// Static member of the outer class
private static String staticMember = "Outer static
member";
Example:
// Outer class
public class OuterClass {
private String instanceMember = "Outer instance
member";
// Inner class
public class InnerClass {
public void display() {
// Access instance member of the outer class
System.out.println("Accessing from InnerClass:
" + instanceMember);
}
}
class outerClass {
Example:
class Polygon {
public void display() {
System.out.println("Inside the Polygon class");
}
}
class AnonymousDemo {
public void createClass() {
class Main {
public static void main(String[] args) {
AnonymousDemo an = new AnonymousDemo();
an.createClass();
}
}
Circle Class:
Rectangle Class:
Fish Class:
Bird Class:
Implement the Animal and Fly interfaces. Include an
additional method layEggs() to represent the
characteristic of laying eggs.
Requirements:
1.LibraryItem Hierarchy:
○ Create an abstract class LibraryItem that
represents a general library item. It should
contain common attributes like title and year,
and an abstract method displayInfo() for
displaying item-specific details.
2.Book Class:
○ Extend LibraryItem to create a Book class that
includes additional attributes like author,
borrowed (boolean), and borrower (String).
○ Implement the Borrowable interface with methods
borrowItem(), returnItem(), and isBorrowed() to
handle the borrowing functionality.
○ Add logic to apply penalties for late returns.
3.Magazine Class:
○ Extend LibraryItem to create a Magazine class
with an additional attribute issueNumber.
○ Implement the displayInfo() method to showcase
magazine-specific details.
4.Multimedia Items (DVD Class):
○ Extend LibraryItem to create a DVD class,
adding attributes like duration (in minutes).
○ Implement the Borrowable interface to manage
borrowing functionality with specific rules for
multimedia items.
5.Borrowing History Tracking:
○ Inside LibraryItem, create a nested class
BorrowingHistory to track borrowing and return
events. Include details such as borrower name,
borrowing date, and return date.
○ Ensure the borrowing history can be displayed
alongside other item information.
6.Library Management:
○ Create a Library class that maintains a list of
all LibraryItem objects.
○ Implement methods to add, remove, and display
all library items.
○ Include basic search functionality to find
items by title.
7.Librarian Class:
○ Implement a nested Librarian class within
Library to manage library operations, including
adding/removing items and handling
borrowing/returning processes.
8.Main Class:
○ Instantiate LibraryItem objects (Books,
Magazines, DVDs) in the Main class and add them
to the library.
○ Demonstrate borrowing and returning items,
including the application of late return
penalties.
○ Utilize the search functionality to find and
display items based on specific criteria.
○ Ensure the complete history and current status
of all items are printed.
By the end of this question you will have implemented a
functional Library Management System that encapsulates
core OOP concepts. And have gained practical experience
in using interfaces, abstract classes, nested classes,
and handling complex data operations in Java