Java Notes - Unit 2
Java Notes - Unit 2
UNIT 2
1. What is the difference between an interface and class. With the help of suitable
java program explain the purpose of interfaces in java.
Class Interface
• A class provides a blueprint for • An interface defines a set of methods and
creating objects with specific attributes constants that any implementing class
(fields) and methods. must adhere to.
• It can implement interfaces, inheriting• It does not provide any implementation for
their methods and providing concrete these methods, acting as a blueprint or
implementations. contract.
• A class can also extend another class, • Classes can implement multiple interfaces,
inheriting its attributes and methods inheriting their methods and adhering to
and potentially overriding them. their rules.
Example:
Java
// Interface de,ining shape drawing behavior
interface Shape {
void draw();
}
Prof. Bhak+ Chaudhari 1
SYIT SEM IV
@Override
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
}
}
@Override
public void draw() {
System.out.println("Drawing a square with side length " + sideLength);
}
}
class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}
Prof. Bhak+ Chaudhari 3
SYIT SEM IV
3. Explain the signi0icance of the "abstract" keyword in Java when used with classes and
methods.
1. Abstract Classes:
Purpose:
• Serve as blueprints for subclasses, de#ining a general structure and shared behavior.
• Cannot be instantiated directly—they exist to be extended by concrete subclasses.
Features:
• Can contain both abstract and concrete methods.
• May have instance -ields.
• Cannot be marked as !inal (preventing inheritance).
Example:
abstract class Animal {
abstract void makeSound(); // Abstract method
public void eat() { // Concrete method
System.out.println("Animal is eating");
}
}
2. Abstract Methods:
Purpose:
• Declare a method signature without providing an implementation.
• Subclasses must provide a concrete implementation to be instantiated.
Features:
• Only exist within abstract classes or interfaces.
4 Prof. Bhak+ Chaudhari
SYIT SEM IV
1. Enforce Inheritance: - Abstract classes and methods ensure that subclasses provide
necessary implementations, promoting code reuse and consistency.
2. Design Flexibility: - Abstract classes allow you to de0ine a common structure and
behavior while deferring implementation details to subclasses, enabling adaptable designs.
3. Code Modularity: - Abstract classes break down complex problems into smaller, more
manageable units, enhancing code organization and maintainability.
class Book {
String title;
Book(String title) {
this.title = title;
}
void display() {
System.out.println("Title: " + title);
}
}
@Override
void display() {
super.display(); // Calling the display method of the superclass (Book)
System.out.println("Genre: " + genre);
}
In this example, we have a Book class with a !tle a"ribute and a display method. The Fic$onBook class extends
Book and adds a genre a"ribute. In the constructor of Fic$onBook, super('tle) is used to call the constructor of
the superclass (Book). In the display method of Fic$onBook, super.display() is used to call the display method of
the superclass before adding extra func4onality.
Circle(double radius) {
this.radius = radius;
}
@Override
void draw() {
System.out.println("Drawing a circle with radius: " + radius);
}
Prof. Bhak+ Chaudhari 7
SYIT SEM IV
@Override
void draw() {
System.out.println("Drawing a rectangle with length: " + length + " and width: " +
width);
}
}
-----------x--------------