0% found this document useful (0 votes)
1 views2 pages

Java Concepts

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)
1 views2 pages

Java Concepts

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

What is the difference between Abstract class and Interface.

The main difference between an Abstract Class and an Interface is their purpose.

 An Abstract Class is used when multiple related classes share common behavior and state.

 An Interface is used to define a contract that unrelated classes can implement.

Abstract Class Interface

Can have both abstract and concrete


Can have abstract methods and default/static methods
methods

Can have instance variables (state) Can only have constants (public static final)

Supports constructors Does not support constructors

A class can extend only one abstract class A class can implement multiple interfaces

Uses extends keyword Uses implements keyword

Suitable for achieving multiple inheritance and loose


Suitable for code reuse
coupling

What is the difference between ArrayList and LinkedList

ArrayList and LinkedList are both implementations of the List interface, but they store data
differently.

 ArrayList uses a dynamic array internally.

 LinkedList uses a doubly linked list internally.

ArrayList LinkedList

Backed by a dynamic array Backed by a doubly linked list

Faster for data retrieval (get()) Slower for data retrieval

Slower for insertion/deletion in the middle because Faster for insertion/deletion because only
elements need to be shifted links are updated

Consumes more memory due to storing


Consumes less memory
node references

Preferred when insert/delete operations


Preferred when read operations are more frequent
are more frequent
What is the difference between hashmap and hashset

HashMap and HashSet are both part of the Java Collections Framework and use hashing internally,
but they serve different purposes.

 HashMap stores data as key-value pairs.

 HashSet stores only unique values.

HashMap HashSet

Stores key-value pairs Stores only values

Keys must be unique Elements must be unique

Allows one null key and multiple null values Allows one null element

Implements Map interface Implements Set interface

Used when data needs to be retrieved using a key Used when only unique elements are required

String vs StringBuilder vs StringBuffer

String, StringBuilder, and StringBuffer are used to handle text in Java, but they differ in mutability
and thread safety.

Feature String StringBuilder StringBuffer

Mutable ❌ No (Immutable) ✅ Yes ✅ Yes

Thread Safe ❌ No ❌ No ✅ Yes

Performance Slow for frequent modifications Fastest Slower than StringBuilder

Creates new object on


Memory Usage Modifies existing object Modifies existing object
modification

Single-threaded Multi-threaded
Use Case Fixed text
applications applications

You might also like