Java Notes 2 Final
Java Notes 2 Final
[wbsu 2023]
Ans:- Object-Oriented Programming is a way of programming where we use classes and
objects to create programs based on the real world. These objects have data (called
attributes) and functions (called methods) inside them. In OOP, computer programs are
made using objects that act like real-life things and can interact with each other.
Example:-
Car myCar = new Car();
Inheritance
3.a) What are the different forms of Inheritance? 5 [DSE B2]
Ans:- There are five common types of inheritance in object-oriented
programming: single, multiple, multilevel, hierarchical, and hybrid. Each type defines a
different way classes can inherit properties and behaviors from other classes.
Here's a breakdown of each type:
Single Inheritance:A class inherits from only one parent class. This is the simplest
form and provides a direct parent-child relationship.
Multiple Inheritance:
A class inherits from multiple parent classes. This allows a class to combine features from
different sources, but can lead to complexities.
Multilevel Inheritance:
A class inherits from a class that itself inherits from another class, creating a chain of
inheritance. This forms a hierarchy of parent and child classes.
Hierarchical Inheritance:
Multiple classes inherit from a single parent class. This creates a tree-like structure
where several classes share common properties from the parent.
Hybrid Inheritance:
This type of inheritance combines two or more of the other inheritance types. For
example, it could involve multilevel and multiple inheritance.
6.a) Describe the uses of super keyword with respect to inheritance. Give an example.
[2021-22] dse 1 (5)
Ans:- bhebe dekhbo
5. (a) How is inheritance incorporated(babohar) in Java?
(b) Is it possible in Java to have multiple inheritance? If not, then how can it be
implemented in Java? [wb 2023] 2+(2+4)
Ans:- In Java, the extends keyword is used to do inheritance. It allows a subclass to get
the fields and methods of the superclass. When one class extends another class, it
means it gets all the non-primitive members (like fields and methods) from the parent
class. The subclass can also change (override) or add new features to them.
Example: In the following example, Animal is the base class and Dog, Cat and Cow are
derived classes that extend the Animal class.
3.b) The main reason why Java does not support multiple inheritance is because Java is
designed to be simple and clear, not complex. If multiple inheritance is allowed, it can
create confusion and problems when a class has more than one parent. So, Java avoids
that.
In Java, a class can inherit from only one superclass using the extends keyword. This is
called single inheritance. It makes a clear class structure, where every class has only one
parent. This helps in writing clean code, makes it easier to maintain, and avoids any
conflicts.
Next part in khata
3. (a) What do you mean by inheritance? What purpose does it serve? Explain. 4
(b) How single inheritance is implemented in Java? Explain with an example. 4 [2021-
2022] DSE-1
3.a) Ans:- Java Inheritance is a fundamental concept in OOP(Object-Oriented
Programming). It is the mechanism in Java by which one class is allowed to inherit the
features(fields and methods) of another class. In Java, Inheritance means creating new
classes based on existing ones. A class that inherits from another class can reuse the
methods and fields of that class.
Syntax
class ChildClass extends ParentClass {
Code Reusability: The code written in the Superclass is common to all subclasses.
Child classes can directly use the parent class code.
Method Overriding: Method Overriding is achievable only through Inheritance. It is
one of the ways by which Java achieves Run Time Polymorphism.
Abstraction: The concept of abstraction where we do not have to provide all details,
is achieved through inheritance. Abstraction only shows the functionality to the user.
3.b) Single Inheritance in Java means that a class (called a subclass or child class) inherits
properties and methods from only one superclass (parent class).
This allows code reuse and method overriding.
Next part in khata
Polymorphism
1. Describe method overloading and method overriding. 5
Ans:- When a class has two or more methods with the same name but different
parameters, the respective method is called based on the arguments passed. The
respective method body is dynamically bound to the calling line. This mechanism is
known as method overloading.
Example:-
In method overriding, the super class and subclass have methods with the same name,
including parameters. JVM calls the respective method based on the object used to call
the method. In overriding, the return types should also be the same.
EXAMPLE
2. What restrictions are placed in method overloading and method overriding?
[wbsu 2023 hons] (3+3)
Ans:- ✅ Restrictions in Method Overloading:
1. Method name must be the same.
2. Parameter list must be different (type, number, or order).
3. Return type alone is not enough to distinguish overloaded methods.
4. Can’t overload by just changing access modifier.
✅ Restrictions in Method Overriding:
1. Method name and parameter list must be exactly same as superclass method.
2. The overriding method cannot reduce visibility (e.g., from public to private).
3. The overriding method cannot throw broader exceptions than the overridden
method.
4. Final, static, or private methods cannot be overridden.
5. The subclass method should use the @Override annotation for clarity
5.b) Explain the differences between method overriding and method overloading. Give
example. [2020, held 21] 5
1.f ) Differentiate between abstract class and interface. (2023) 2
Ans:-
It helps to hide the internal details of an object from the outside world. Only selected
parts are made accessible using access modifiers like private, public, or protected. This
ensures that the data is protected from direct changes and can only be modified through
methods.
c) The final keyword in Java is used to restrict modification of variables, methods,
and classes. It helps to ensure security, consistency, and immutability in the program.
1. Final Variable
When a variable is declared as final, its value cannot be changed once it is assigned.
It acts as a constant.
Example:
2. Final Method
class A {
System.out.println("Hello");
3. Final Class
A class declared as final cannot be extended (inherited). It is used when the class
functionality should not be modified by inheritance.
Example:
// class content
The final keyword is used to make variables constant, prevent method overriding,
and stop class inheritance. It is mainly used for safety and to avoid unintended
changes in the code.
DSE-b2 [2021]
In simple words, abstraction allows us to focus on what an object does instead of how
it does it.
EXAMPLE:-
void makeSound() {
System.out.println("Bark");
}
How does a class accomplish data hiding? 2[bu 2022]
Ans:- In Java, data hiding is achieved by declaring class variables as private and providing
public getter and setter methods to access and update the values.
This means the internal state of an object is hidden from outside classes and can only
be accessed through controlled methods. It helps in protecting data, preventing
unauthorized access, and ensuring encapsulation.
1. Declare variables as private so they are not directly accessible from outside the class.
2. Provide public methods (getters and setters) to access and modify those variables.
Example
Ans:- Java is called system-independent because it uses the Java Virtual Machine
(JVM) to run programs.
Java code is compiled into platform-neutral bytecode, which can be executed on any system
with a JVM, regardless of the operating system or hardware.
Thus, the same Java program runs on any system, making it system-
independent.
Ans:- Java is called a compiler-interpreter language because it uses both a compiler and an
interpreter to run programs.
Java code is first compiled by the Java compiler (javac) into bytecode, and then the Java
Virtual Machine (JVM) interprets this bytecode and executes it line by line on any system.
Ans:- JVM is a virtual machine that enables the execution of Java bytecode. The JVM acts as
an interpreter between the Java programming language and the underlying hardware. It
provides a runtime environment for Java applications to run on different platforms and
operating systems.
Ans:- The Java Virtual Machine (JVM) plays a key role in executing Java programs.
1) It reads the compiled bytecode and converts it into machine code specific to the
operating system.
2) This allows Java programs to run on any platform, making Java platform-independent.
3) It also handles memory management, security, and exception handling during program
execution.
Ans:-
1) Java garbage collection is an automatic process that manages memory in the heap.
2) It identifies which objects are still in use (referenced) and which are not in use
(unreferenced).
3) It removes the objects that are unreachable (no longer referenced).
4) The programmer does not need to mark objects to be deleted explicitly. Garbage
collection is implemented within the JVM.
1.a) What do you mean by portability of a program? Is Java language portable? [DSE
1] [2021-2022][WBSU]
Portability means that a program written on one system (platform) can run on another system
without needing any changes in the code.
In simple words, a portable program can work on different operating systems (like Windows,
Linux, or Mac) without rewriting it.
Is Java Portable?
Java is portable because Java programs are compiled into bytecode, which can run on any
system that has the Java Virtual Machine (JVM).
Since JVM is available for all major operating systems, the same Java program can run
anywhere — this makes Java write once, run anywhere.
Garbage collection
3.b.i) BU 2022
Ans:- In Java, Garbage Collection (GC) is a process by which the Java Virtual Machine
(JVM) automatically removes unused or unreferenced objects from the memory. This helps
to free up memory space and improve the performance and efficiency of the Java program.
Ans:- In Java, garbage collection is the process by which the Java Virtual Machine (JVM)
automatically removes objects from memory that are no longer needed or referenced. This helps
free up memory and keeps the program efficient.
Ans:- Garbage Collection in Java is used to automatically manage memory by deleting objects that
are no longer in use. It helps in:
2.a) What is a constructor? Discuss about different types of constructors. [DSE B2]
[2021] 5
Ans:- A constructor in Java is a special method that is automatically called when an object of
a class is created.
It is used to initialize the object.
It has the same name as the class and does not have any return type, not even void.
1. Default Constructor
If you don’t write any constructor in your class, Java automatically provides a default
constructor.
It doesn’t take any arguments and doesn’t do any special task—just helps in creating an
object.
Example
2. No-Argument Constructor (User-Defined)
This is a constructor you write yourself, and it doesn’t take any parameters.
It can be used to print a message or set default values when the object is created.
Example:
3. Parameterized Constructor
This constructor takes one or more values (parameters) when the object is created.
It is used to give initial values to variables inside the object.
Example:
4. Copy Constructor (Conceptual in Java)
Java doesn’t have a built-in copy constructor like C++, but we can make one.
It takes an object of the same class and copies its data to the new object.
Example:
5. Private Constructor
Example:
Conclusion:
Constructors are used to create and initialize objects. Java supports different types of
constructors for different use cases, like giving default values, setting custom data,
copying existing data, or restricting object creation.
2.c) Differentiate between default constructor and parameterized constructor. [WBSU]
[2021-2011][DSE 1]
3.b) What is constructor? How do you define parameterized constructor? Explain with
a program. 2+4[BU CC3 2019]
Ans:- A constructor in Java is a special method that is automatically called when an object of
a class is created.
It is used to initialize the object.
It has the same name as the class and does not have any return type, not even void.
SYNTAX:
Next part
(MY SUGGESTION)
Ans:- Constructor overloading means creating many constructors in one class with different
parameters.
It helps to create objects in different ways and give different values when the object is made.
Java decides which constructor to run based on the number or type of values you pass.
All constructors have the same name as the class, but their parameter lists are different.
It is a part of compile-time polymorphism.
SYNTAX:-
4.b) Explain multilevel inheritance with example. [2020 held 21] 5
Ans:- Multilevel Inheritance in java involves a class extending to another class that is already
extended from another class. In simple words it includes inheriting a class, which already
inherited some other class. We can create hierarchies in Java with as many layers of
Inheritance as we want. This means we can utilize a subclass as a superclass. In this case,
each subclass inherits all of the traits shared by all of its superclasses.
Example
Keywords
2.b) Explain the use of 'this' keyword with an example. [cu 2021] 2
Ans:- The this keyword in Java is a reference variable that refers to the current object of the
class. It is mainly used to distinguish between instance variables and parameters when they
have the same name. (DEFINITION)
Example
Using "this" reference can improve code readability and reduce naming conflicts.
1.D) What are the two uses of keyword super in Java? [DSE 1][WBSU 2021-2022] 2
Ans:- It is used to call superclass methods, and to access the superclass constructor.
The most common use of the super keyword is to eliminate the confusion between
superclasses and subclasses that have methods with the same name.
DEFINITION:-
The super keyword refers to superclass (parent) objects. It is used to call superclass methods,
and to access the superclass constructor. The most common use of the super keyword is to
eliminate the confusion between superclasses and subclasses that have methods with the same
name.
EXAMPLE:-
2.a) Explain the use of final keyword in variable, method and class. 3 (wbsu 2023)
Example:
Example:
System.out.println("Cannot be overridden");
Access Specifiers/Modifiers
Syntax
// Class body
1.d) What are the different access specifier in Java? [2021][DSE b2] 2
Or, 3.b) Discuss the different access modifiers in java. [BU 2019] 4
Ans:- Java has four access modifiers: public, private, protected, and default. These modifiers
control the visibility and accessibility of classes, methods, and variables.
Here's a breakdown:
public:
The most accessible, allowing access from anywhere (within the class, outside the class, and
across different packages).
private:
The most restrictive, limiting access to only within the declaring class.
protected:
Allows access within the same package and also by subclasses in different packages.
default (package-private):
When no access modifier is specified, it defaults to this level, granting access within the same
package only.
These access modifiers play a crucial role in encapsulation, a fundamental principle of object-
oriented programming, by controlling which parts of the code can interact with each other.
3.c) Define package. Write steps for creating a user defined package. How we can add a
new class into a user defined package? [BU 2019] 2+4+4
Ans:- A package in Java is used to group related classes. Think of it as a folder in a file directory. We
use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided
into two categories:
Next answer
package packagename;
// class body
javac -d . ClassName.java
import packagename.ClassName;
3.b) How does package differ from Interface? Explain it with suitable example. 5+5 [cu
2021 gen]
1.h) What are the similarities between interface and class? [2020-2021] wbsu (2)
Interfaces and classes can both be used to define a data type that can be used to
declare objects or references.
Classes can inherit from other classes, and interfaces can also be extended or
implemented, supporting inheritance.
After compilation, both are converted into bytecode (.class) files used by the JVM.
1.h) Differentiate between Class and Abstract Class in Java. 2 marks (WBSU 2021-
2022)
Exception Handling
Ans:- An exception (or exceptional event) is a problem that arises during the execution of a
program. When an Exception occurs the normal flow of the program is disrupted and the
program/Application terminates abnormally, which is not recommended, therefore, these
exceptions are to be handled.
1.b) What is the difference between error and exception? 2 (WBSU 2020, held 21)
Ans:-
Errors Exceptions
1.a) Is it possible to have a 'try' block without a 'catch' block? [2020 held 2021] wbsu
Ans:- yes, It is possible to have a try block without a catch block by using a final block.
As we know, a final block will always execute even there is an exception occurred in a try
block, except System.exit() it will execute always.
Q) What happens if there is no 'catch' block with a 'try' block in exception handling?
Ans:- If there is no catch block with a try block in exception handling, then a finally block
must be used. The finally block always executes whether an exception occurs or not. If
neither catch nor finally is present, it results in a compile-time error.
Ans:- In Java, throw and throws are used for exception handling.
Syntax:
3.a) How do you create your own exceptions in Java? 4 [BU 2019]
Definition:A user-defined exception is a class that inherits from the Exception class (for
checked exceptions) or the RuntimeException class (for unchecked exceptions). It behaves
like any other exception and can be thrown and caught using the throw and try-catch
mechanism.
Steps to Create a User-Defined Exception:
Syntax:
super(message);
Threads
Q) What is a thread?
Ans:- A thread in Java is a lightweight sub-process or a small unit of a process. It is the smallest part
of a program that can run independently and concurrently with other threads.
Example:
When you open a webpage, downloading images, loading text, and playing audio can all
happen at the same time using different threads.
Ans:- In Java, daemon threads are low-priority threads that run in the background to
perform tasks such as garbage collection or provide services to user threads. The life of a
daemon thread depends on the mercy of user threads, meaning that when all user threads
finish their execution, the Java Virtual Machine (JVM) automatically terminates the daemon
thread.
4.b) Explain thread prioritization in Java. (Cu 2021) 5
1.d) What is the method used for creating thread in Java? [ bu 2022] 2
The Thread class provides constructors and methods for creating and operating on threads.
The thread extends the Object and implements the Runnable interface.
2. Runnable Interface
Any class with instances that are intended to be executed by a thread should implement the
Runnable interface. The Runnable interface has only one method, which is called run().
Ans:- Multithreading is a Java feature that enables the concurrent execution of two or more parts
of a program, maximizing CPU utilization. Each part of such a program is called a thread. So, threads
are lightweight processes within a process.
Example: Imagine a restaurant kitchen where multiple chefs are working simultaneously on
different dishes. This setup ensures faster service and better CPU (chef) utilization, just like
threads in Java.
Ans:- The life cycle of a thread in Java refers to the various states of a thread goes through. For
example, a thread is born, started, runs, and then dies. Thread class defines the life cycle and
various states of a thread.
Flow Chart of Java Thread Life Cycle
New − A new thread begins its life cycle in the new state. It remains in this state until
the program starts the thread. It is also referred to as a born thread.
Runnable − After a newly born thread is started, the thread becomes runnable. A
thread in this state is considered to be executing its task.
Waiting − Sometimes, a thread transitions to the waiting state while the thread waits
for another thread to perform a task. A thread transitions back to the runnable state
only when another thread signals the waiting thread to continue executing.
Timed Waiting − A runnable thread can enter the timed waiting state for a specified
interval of time. A thread in this state transitions back to the runnable state when that
time interval expires or when the event it is waiting for occurs.
Terminated (Dead) − A runnable thread enters the terminated state when it
completes its task or otherwise terminates.
7) Explain wait() method related to thread (5 marks) [WBSU DSE 1][2020 held 21]
Ans:- The wait() method in Java is used to pause the execution of a thread until another
thread notifies it to continue. It is a part of Java’s synchronization system, which helps
threads to communicate and coordinate with each other.
When a thread calls the wait() method on an object, it must first hold the lock (monitor) of
that object. This means the wait() method must be called inside a synchronized block or
method.
Once the thread calls wait(), it releases the lock and goes into the waiting state.
The thread stays in the waiting state until one of the following happens:
1. Another thread calls the notify() or notifyAll() method on the same object.
2. A specified time runs out (if a timeout is used).
3. The thread is interrupted by another thread.
After being notified, the thread tries to get the lock again, and when it gets it, it resumes
execution from where it had paused.
Use of wait():
The wait() method is useful when a thread needs to wait for a certain condition before it can
move forward.
For example, a thread may wait for some data to be ready, for a resource to be free, or for
another thread to complete a task.
1.b) What is the purpose of wrapper class in Java? [2021 DSE B2] WBSU 2
Utility Methods:
Wrapper classes provide useful methods to convert values, parse strings, etc.
Example: Integer.parseInt("123");
Strings
Ans:- The charAt() method is a function of the java.lang.String class. Its purpose is to return
the character at a specified index within a string.
Example
2.b) Describe the substring() and indexOf() methods of String class with suitable
examples. [wbsu 2023]
1. substring() Method:
The substring() method is used to take out a part of a string. It returns a new string starting
from the given position and continues till the end, or up to the specified ending position
(excluding the last position).
SYNTAX
EXAMPLE
2.indexOf() Method:
The indexOf() method is used to find the position of the first time a letter or word appears in
a string. If the letter or word is not found, it gives -1.
Syntax:
Example:
System.out.println(str.indexOf('o')); // Output: 4
System.out.println(str.indexOf("World")); // Output: 5
System.out.println(str.indexOf('z')); // Output: -1
4.b) What is the difference between String equals() and == operator? [WBSU 2023] 2
Ans:-
S.No. == Operator Equals() Method
2. It is majorly used to compare the reference It is used to compare the actual content
values and objects. of the object.
3. We can use the == operator with objects We cannot use the equals method with
and primitives. primitives.
4. The == operator can’t compare conflicting The equals() method can compare
objects, so at that time the compiler conflicting objects utilizing the equals()
surrenders the compile-time error. method and returns “false”.
8.a) Write down the difference between String class and String Buffer class. 3 [WBSU
2020, held 2021]
Ans:-
S. String StringBuffer
No.
3 Here the length of the string class is Here the length can be modified whenever
static. required, as it is dynamic in behaviour.
5 It utilises a string constant pool to It prefers heap memory to store the objects.
store the values.
9.b) Write short notes on Strings in Java. [2020 held 2021 wbsu] 4
In Java, a String is used to store and work with words and sentences. It is an object that holds
a sequence of characters, such as "Hello" or "Java is fun".
The most important feature of a Java String is that it is immutable, meaning once a string is
created, it cannot be changed. If any change is made, Java actually creates a new string with
the updated value instead of modifying the old one.
For example, if we try to change the text of a string, a new object is created in memory with
the new value, and the original string remains unchanged.
The String class has many built-in methods to work with text. Some of the most commonly
used methods are:
Since Strings are immutable, changing them frequently can be inefficient. For such cases,
Java provides two classes:
StringBuilder – Used when the string is being changed often. It is fast and suitable
for single-threaded environments.
StringBuffer – Similar to StringBuilder but safer for multi-threaded programs. It is
slightly slower but thread-safe.
Ans: In Java, the method public static void main(String args[]) is the entry point of any
standalone Java program. Each keyword in this statement has a specific meaning:
1. public
It is an access modifier. This means the method can be accessed from anywhere. Since
the Java Virtual Machine (JVM) needs to call this method from outside the class, it must
be public.
2. static
This means the method belongs to the class, not to an object of the class. The JVM does
not create an object to call main(), so it must be static.
3. void
This means the method does not return any value.
4. main
This is the name of the method. It is predefined in Java and the JVM always looks for
this method to start program execution.
5. String args[]
This is a way to pass command-line arguments to the program. It is an array of String
objects.
2.c) Why Java is preferred for software development? [DSE B2] cu 3 marks
1. Platform Independent – Java programs can run on any operating system using JVM
(Java Virtual Machine), so the same code works everywhere.
2. Object-Oriented – Java uses object-oriented programming, which makes the code
easier to manage, reuse, and maintain.
3. Built-in Libraries – Java provides a rich set of standard libraries that help in faster
and easier development.
7.a) Differentiate between instance variables and class variables. [WBSU 2021-22]
Ans:-
A static variable is a variable that belongs to the class, not to any single object. This means all
objects of the class share the same copy of the variable. It is declared using the static
keyword. Memory for a static variable is given only once when the class is loaded.
Example:
Static Method:
A static method is a method that also belongs to the class, not to an object. It can be used
without creating any object of the class. It is declared using the static keyword. A static
method can directly use only static variables. It cannot directly use instance (non-static)
variables or methods.
Example:
The new operator in Java is used to create objects of a class. It allocates memory in the heap
and calls the constructor to initialize the object.
Example:
Ans:- No, private methods cannot be overridden in Java because they are not accessible
outside the class they are declared in. Since method overriding requires access in a subclass,
and private methods are hidden from subclasses, overriding is not possible.
However, a subclass can define a method with the same name, but it will be treated as a
new method, not an overridden one.
Typecasting in Java means converting one data type into another. It is of two types:
Example:
int a = 10;
double x = 9.78;
}
Output:
Widening: 10.0
Narrowing: 9
Explanation:
Ans:- Java does not support destructors like C++ because it uses automatic garbage
collection.
In Java, when an object is no longer used, the Garbage Collector automatically deletes it
to free memory. So, there is no need to write destructors manually.
Instead of destructors, Java provides a method called finalize(), but it is rarely used
and not reliable for cleanup tasks.
2.a) What happens if String args[] is not written in main() method? [2021-2022] WBSU
Ans:- If String args[] is not included as a parameter in the main() method signature in Java,
the code will compile successfully. However, when you attempt to run the program using
the Java Virtual Machine (JVM), a runtime error will occur, typically stating "Main method
not found" or "Error: Main method not found in class <YourClass>, please define the main
method as: public static void main(String[] args)".
Ans:- Yes, Java allows the use of the modulus operator (%) on floating-point numbers like
float and double.
Example:
double a = 10.5;
double b = 4.2;
double result = a % b;
System.out.println("Result: " + result); // Output: 2.1
So, the modulus operator can be used with both integers and floating-point numbers in Java.
4.b) What is the difference between an executable file and a .class file? [2021-2022]
[DSE 1][WBSU]
Ans:- Java has two categories in which data types are segregated
1. Primitive Data Type: These are the basic building blocks that store simple values directly
in memory. Examples of primitive data types are
boolean
char
byte
short
int
long
float
double
2. Non-Primitive Data Types (Object Types): These are reference types that store memory
addresses of objects. Examples of Non-primitive data types are
String
Array
Class
Interface
Object
9.a) Differentiate between object and object reference. 2 [wbsu 2021-2022] [DSE 1]
4.a) Java works as "pass by value" or "pass by reference" phenomenon. [WBSU 2023]
For primitive types (like int, float), the actual value is passed.
For objects, the value of the object reference (i.e., memory address) is passed. So,
changes to object data are reflected, but the reference itself cannot be changed to point
to a new object.
👉 Therefore, Java is strictly pass by value, but in the case of objects, it may look like pass
by reference because the reference value is copied.
Ans:- In Java, a constant is a variable whose value cannot be changed after it is initialized.
Constants are used when a fixed value is needed throughout the program.
Syntax:
Example:
1.b) Write the uses of net and util packages of Java.[BU 2019]
Ans:- In Java, metadata refers to "data about data." It provides additional information and
context about program elements such as classes, interfaces, methods, and fields. This
information can be used by various tools, frameworks, and runtime components to perform
specific actions or provide enhanced functionality.
2.d) How do you define an interface? How do you define multiple inheritance in Java?
[BU 2019] 2+3
Ans:- In Java, an interface is a collection of abstract methods (methods without a body). It is used to
define a contract that other classes can implement.
✔️Syntax:
interface Animal {
void sound(); // abstract method
✔️Implementation by a class:
System.out.println("Barks");