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

Understanding Abstraction in Java

Abstraction in Java is the process of hiding implementation details and exposing only essential features, achieved through abstract classes and interfaces. It reduces complexity, increases code reusability, and improves maintainability. Additionally, the document explains the differences between abstract classes and interfaces, as well as concepts like upcasting and dynamic method dispatch.

Uploaded by

Omkar Saste
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)
11 views5 pages

Understanding Abstraction in Java

Abstraction in Java is the process of hiding implementation details and exposing only essential features, achieved through abstract classes and interfaces. It reduces complexity, increases code reusability, and improves maintainability. Additionally, the document explains the differences between abstract classes and interfaces, as well as concepts like upcasting and dynamic method dispatch.

Uploaded by

Omkar Saste
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

Q1.

Explain Abstrac on in Java

Answer:
Abstrac on in Java is the process of hiding the internal implementa on details and showing
only the essen al features of an object.
It helps reduce complexity and increases code reusability and maintainability.

In Java, abstrac on can be achieved in two ways:

1. Using Abstract Classes

2. Using Interfaces

Example using Abstract Class:

abstract class Vehicle {

abstract void start(); // abstract method

void fuel() { // concrete method

[Link]("Vehicle needs fuel.");

class Car extends Vehicle {

void start() {

[Link]("Car starts with key igni on.");

public class Main {

public sta c void main(String[] args) {

Vehicle v = new Car();

[Link]();
[Link]();

Explana on:

 The Vehicle class hides how the car actually starts.

 The subclass Car provides the specific implementa on.

 This way, abstrac on allows focus on what the object does rather than how it does it.

Advantages of Abstrac on:

 Reduces code complexity.

 Increases code security.

 Improves maintainability.

 Promotes reusability and flexibility.

Q2. Differen ate between Abstract Class and Interface

Feature Abstract Class Interface

Keyword Declared using abstract keyword. Declared using interface keyword.

Can have both abstract and Can have abstract methods, default, sta c,
Methods
concrete (implemented) methods. and private methods (from Java 8+).

Only constants (public sta c final by


Variables Can have instance variables.
default).

A class can extend only one abstract A class can implement mul ple interfaces
Inheritance
class (single inheritance). (mul ple inheritance).

Constructors Can have constructors. Cannot have constructors.

Access Methods can have any access


All methods are implicitly public.
Modifiers modifier.
Feature Abstract Class Interface

Used when classes share common Used to define a contract or behavior for
Purpose
characteris cs. unrelated classes.

Example abstract class Animal {} interface Animal {}

Example:

// Abstract class example

abstract class Shape {

abstract void draw();

void display() { [Link]("Displaying shape"); }

// Interface example

interface Drawable {

void draw();

Q3. Explain the concept of Upcas ng and Dynamic Method Dispatch

Answer:

(a) Upcas ng

Upcas ng is the process of assigning a subclass reference to a superclass reference variable.


It is used to achieve run me polymorphism and allows generaliza on of object handling.

Syntax:

Parent obj = new Child(); // Upcas ng

Example:

class Animal {

void sound() { [Link]("Animal makes sound");


}

class Dog extends Animal {

void sound() {

[Link]("Dog barks");

public class Main {

public sta c void main(String[] args) {

Animal a = new Dog(); // Upcas ng

[Link](); // calls Dog’s version due to dynamic method dispatch

(b) Dynamic Method Dispatch

Dynamic Method Dispatch (also known as Run me Polymorphism) is the mechanism by which
a call to an overridden method is resolved at run me rather than compile- me.

 The type of object (not reference) determines which method is executed.

 It enables Java’s run me polymorphism.

Explana on of Example:

 Animal a = new Dog(); → a is of type Animal but refers to a Dog object.

 When [Link]() is called, JVM determines at run me that the actual object is of Dog
type, so it calls the Dog class version of sound().

Output:

Dog barks

You might also like