Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Java Notes - Unit 2

Uploaded by

kobawag832
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java Notes - Unit 2

Uploaded by

kobawag832
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________

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.

• Class: Can be declared • Interface: Can only be declared


as public, private, protected, or as public or abstract. Can contain static
default. Can contain all types of and final methods and constants, but not
methods and fields, including instance methods or fields
instance, static, final, and abstract

• Class: Depends on its design and • Interface: Highly reusable as a contract,


complexity, but generally less reusable independent of specific implementations.
than interfaces due to concrete
implementation details.

Example:
Java
// Interface de,ining shape drawing behavior
interface Shape {
void draw();
}
Prof. Bhak+ Chaudhari 1
SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________

// Class implementing the Shape interface


class Circle implements Shape {
private int radius;

public Circle(int radius) {


this.radius = radius;
}

@Override
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
}
}

// Another class implementing the Shape interface


class Square implements Shape {
private int sideLength;

public Square(int sideLength) {


this.sideLength = sideLength;
}

@Override
public void draw() {
System.out.println("Drawing a square with side length " + sideLength);
}
}

// Client code using the Shape interface


public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape square = new Square(4);

circle.draw(); // Output: Drawing a circle with radius 5


square.draw(); // Output: Drawing a square with side length 4
}
}

2 Prof. Bhak+ Chaudhari


SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________
2. What is inheritance? How different types of inheritance implemented in Java?
In Java, inheritance is a fundamental object-oriented programming (OOP) concept that
allows you to create new classes (subclasses) based on existing classes (superclasses). It
establishes a "parent-child" relationship between classes, enabling the subclass to inherit
the attributes (,ields) and methods (behaviors) of the superclass. This promotes code
reusability, maintainability, and modularity.

Here are the different types of inheritance implemented in Java:


1. Single Inheritance:
• A subclass inherits from a single parent class.
• This is the most basic and commonly used type of inheritance.
• It ensures clarity and avoids confusion in terms of where methods and 5ields are
inherited from.
2. Multilevel Inheritance:
• A subclass inherits from another subclass, which itself inherits from a parent class.
• This creates a chain of inheritance, where each subclass inherits from the previous
one in the chain.
• Use it cautiously to avoid complexity and potential ambiguity in method resolution.
3. Hierarchical Inheritance:
• Multiple subclasses inherit from the same parent class.
• This allows for specialization of the parent class into different child classes with their
own unique characteristics.
• It's useful for modeling real-world scenarios where entities share common ancestry
but have distinct features.
4. Hybrid Inheritance:
• Combines single, multilevel, and hierarchical inheritance in a single class hierarchy.
• It can create complex inheritance structures, and should be used judiciously due to
potential maintenance challenges.
Implementing Inheritance in Java:
You use the extends keyword to declare a subclass that inherits from a superclass. For
example:

class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}
Prof. Bhak+ Chaudhari 3
SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________

class Dog extends Animal {


@Override
public void makeSound() {
System.out.println("Woof!");
}
}

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

Course Name: Java Programming


____________________________________________________________________________________
• Implicitly public and abstract (no need for explicit modi0iers).
Example:
abstract class Vehicle
{
abstract void move(); // Abstract method
}

Signi%icance of Using abstract:

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.

4. Polymorphism: - Abstract classes and methods enable polymorphism, where objects of


different subclasses can be treated as their common abstract type, enabling 6lexible and
adaptable code.

5. Encapsulation: - Abstraction promotes encapsulation, hiding implementation details and


exposing only essential functionality, leading to more robust and maintainable code.

Prof. Bhak+ Chaudhari 5


SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________
4. What is the signi-icance of using 'super' when dealing with method overriding and
inheritance in java?
In Java, the super keyword is also used in the context of method overriding and inheritance to refer to the
superclass or parent class.
1. Calling Superclass Methods:
• In the case of method overriding, when a subclass provides a speci%ic
implementation for a method that is already de1ined in its superclass, you can
use super to call the overridden method from the parent class.
• This allows you to reuse the functionality of the superclass method while adding
or modifying behavior in the subclass.

class Book {
String title;

Book(String title) {
this.title = title;
}

void display() {
System.out.println("Title: " + title);
}
}

class FictionBook extends Book {


String genre;

FictionBook(String title, String genre) {


super(title); // Calling the constructor of the superclass (Book)
this.genre = genre;
}

@Override
void display() {
super.display(); // Calling the display method of the superclass (Book)
System.out.println("Genre: " + genre);
}

6 Prof. Bhak+ Chaudhari


SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________
}

public class Main {


public static void main(String[] args) {
FictionBook *ictionBook = new FictionBook("The Adventure", "Adventure");
!ictionBook.display();
}
}

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.

5 .Explain the concept of method overriding with an example.


Method overriding is a feature in object-oriented programming where a subclass provides
a speci(ic implementation for a method that is already de(ined in its superclass. The
overridden method in the subclass must have the same signature (method name, return
type, and parameters) as the method in the superclass.
an example using shapes, speci0ically a Shape class, and its subclasses Circle and Rectangle:
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}

class Circle extends Shape {


double radius;

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

Course Name: Java Programming


____________________________________________________________________________________
}

class Rectangle extends Shape {


double length;
double width;

Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

@Override
void draw() {
System.out.println("Drawing a rectangle with length: " + length + " and width: " +
width);
}
}

public class Main {


public static void main(String[] args) {
Circle circle = new Circle(5.0);
Rectangle rectangle = new Rectangle(4.0, 6.0);

// Method overriding: calling the overridden draw method in each shape


circle.draw(); // Calls the draw method in Circle
rectangle.draw(); // Calls the draw method in Rectangle
}
}
Here,
The Shape class has a method called draw.
The Circle class and Rectangle class are subclasses of Shape.
Both Circle and Rectangle override the draw method de4ined in the Shape class with their
speci&ic implementations.

-----------x--------------

8 Prof. Bhak+ Chaudhari

You might also like