Anukul Java 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

NAME- Anukul Kumar

1) What do you mean by final variable in java? explain it's types with
examples?
=> In Java, a final variable is one whose value cannot be changed once it has been
assigned. There are three types of final variables:

a) Final Variables: These are variables declared with the final keyword and are
initialized only once. Once initialized, their value cannot be modified

e.g final int MAX_VALUE = 100;

b) Final Parameters: These are parameters in a method or constructor declaration


that are marked as final. Their value cannot be changed within the method or
constructor.

e.g. void printMessage(final String message) {

System.out.println(message);

c) Final Fields: These are fields in a class marked as final. They must be
initialized either at the time of declaration or within the constructor and cannot be
modified thereafter.

E.g class Circle {

final double PI = 3.14;

final int radius;

Circle(int radius) {

this.radius = radius;

}
NAME- Anukul Kumar

2) Explain super and extends in java?

=> In Java, super is a keyword used to refer to the immediate parent class object,
accessing its methods and variables, and invoking its constructor. On the other
hand, extends is a keyword used to create a subclass that inherits properties and
behaviors from a superclass, establishing an "is-a" relationship between classes.

3) What is the difference between method overloading and method overriding?

Method Overloading Method overriding


a) Involves defining multiple methods in a) Involves redefining a method in a
the same class with the same name but subclass with the same name and
different parameters. parameters as in the superclass.
b) Method overloading is a compile- b) Method overriding is a run-time
time polymorphism. polymorphism.
c) It occurs within the class. c) It is performed in two classes with
inheritance relationships.
d) In method overloading, the return d) In method overriding, methods must
type can or can not be the same, but we have the same name and same signature.
just have to change the parameter.
e) Method overloading helps to increase e) Method overriding is used to grant
the readability of the program. the specific implementation of the
method which is already provided by its
parent class or superclass.

4) What is the difference between compile time polymorphism and run time
polymorphism?
NAME- Anukul Kumar

=>

Compile Time Polymorphism Run time Polymorphism

In Compile time Polymorphism, the call is In Run time Polymorphism, the call is not
resolved by the compiler. resolved by the compiler.

It is also known as Static binding, Early It is also known as Dynamic binding, Late
binding and overloading as well. binding and overriding as well.

Method overloading is the compile-time


Method overriding is the runtime
polymorphism where more than one methods
polymorphism having the same method
share the same name with different
with same parameters or signature but
parameters or signature and different return
associated withcompared, different classes.
type.

It is achieved by function overloading and It is achieved by virtual functions and


operator overloading. pointers.

It provides slow execution as compare to


It provides fast execution because the
early binding because the method that
method that needs to be executed is known
needs to be executed is known at the
early at the compile time.
runtime.

5) What do you understand by heap memory in java ?

=> In Java, the heap memory is a region of the computer's memory that is allocated
for dynamic memory allocation. It's where objects and their associated data are
stored during the execution of a Java program. Unlike the stack memory, which is
used for static memory allocation and method execution, the heap memory is used
for storing objects created at runtime.

6) What is the difference between serial garbage collector and parallel garbage
collector?

=>

Serial Garbage Collector Parallel Garbage Collector


a) Uses a single thread for garbage a) Utilizes multiple threads for garbage
NAME- Anukul Kumar

collection. collection, improving efficiency on


multi-core systems.
b) Generally lower throughput due to b) Typically higher throughput due to
single-threaded execution. parallel execution on multiple threads.
c) Best suited for applications with c) Better suited for applications with
small heaps. large heaps
d) Often used as the default garbage d) Typically not the default choice in
collector in older JDK versions or on modern JDK versions due to its limited
small devices. scalability.

Inheritance program:
class Father{
Father(){
System.out.println("Fathers's place");
}

Father(int a){
System.out.println("Param father "+a);
}
int Father(int a, int b){
return a+b;
}
}
class Son1 extends Father{
Son1(){
super(5);
System.out.println("Son's place");
}
}

public class Inheritance {


public static void main(String[] args) {
Son1 f = new Son1();
System.out.println(f.Father(33,2));
}
}

o/p -> Param father 5

Son's place

35

You might also like