You are on page 1of 22

1.

b)

Access Modifiers in Java.

There are four types of Java access modifiers:

1. Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
2. Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
3. Protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed from within
the class, outside the class, within the package and outside the package.

1) Private

The private access modifier is accessible only within the class.

Simple example of private access modifier

1. class A{
2. private int data=40;
3. private void msg(){System.out.println("Hello java");}
4. }
5.
6. public class Simple{
7. public static void main(String args[]){
8. A obj=new A();
9. System.out.println(obj.data);//Compile Time Error
10. obj.msg();//Compile Time Error
11. }
12. }
Note: A class cannot be private or protected except nested class.
2) Default

If you don't use any modifier, it is treated as default by default. The default modifier is accessible
only within package. It cannot be accessed from outside the package. It provides more accessibility
than private. But, it is more restrictive than protected, and public.

Example of default access modifier

1. //save by A.java
2. package pack;
3. class A{
4. void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4. class B{
5. public static void main(String args[]){
6. A obj = new A();//Compile Time Error
7. obj.msg();//Compile Time Error
8. }
9. }

3) Protected

The protected access modifier is accessible within package and outside the package but through
inheritance only.

The protected access modifier can be applied on the data member, method and constructor. It can't
be applied on the class.

It provides more accessibility than the default modifer.

Example of protected access modifier

1. //save by A.java
2. package pack;
3. public class A{
4. protected void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4.
5. class B extends A{
6. public static void main(String args[]){
7. B obj = new B();
8. obj.msg();
9. }
10. }
Output:Hello

4) Public

The public access modifier is accessible everywhere. It has the widest scope among all other
modifiers.

Example of public access modifier

1. //save by A.java
2.
3. package pack;
4. public class A{
5. public void msg(){System.out.println("Hello");}
6. }
1. //save by B.java
2.
3. package mypack;
4. import pack.*;
5.
6. class B{
7. public static void main(String args[]){
8. A obj = new A();
9. obj.msg();
10. }
11. }
Output:Hello

1.C)

Interface in Java

An interface in Java is a blueprint of a class. It has static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods
in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance
in Java.

Why use Java interface?

There are mainly three reasons to use interface. They are given below.

o It is used to achieve abstraction.


o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.

Java Interface Example

In this example, the Printable interface has only one method, and its implementation is provided
in the A6 class.

1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6.
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11. }
2.a) Must note it by yourself(V.V.I)

Why is Java Platform Independent

Java is a class-based, object-oriented programming language that is designed to have as few


implementation dependencies as possible. It is intended to let application developers write once
and run anywhere (WORA), meaning that compiled Java code can run on all platforms that support
Java without recompilation.Java is platform-independent because it uses a "write once, run
anywhere" approach. Java code is compiled into an intermediate form called bytecode, which can
be executed on any platform that has a Java Virtual Machine (JVM) installed. This allows Java
applications to run on different operating systems without modification, making it platform-
independent.

Java is a popular programming language because it:


1. Is Object-Oriented: This means it focuses on organizing code into objects, like real-
world things. It's like building with LEGO, where you use different pieces (objects) to
create complex structures (programs). This makes code more organized and easier to
manage.
2. Is Platform Independent: Java code can work on different types of computers without
changes. It's like writing a book in a language that people from different countries can
read. You don't need to rewrite the whole book for each country.
3. Supports Multithreading: Java can do multiple tasks at once, like a juggler juggling
balls. Multithreading helps programs run faster by handling several things
simultaneously, like a chef cooking multiple dishes at once.
2.b)

Inheritance

When one object acquires all the properties and behaviors of a parent object, it is known as
inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
Polymorphism

If one task is performed in different ways, it is known as polymorphism. For example: to convince
the customer differently, to draw something, for example, shape, triangle, rectangle, etc.

In Java, we use method overloading and method overriding to achieve polymorphism.

Another example can be to speak something; for example, a cat speaks meow, dog barks woof,
etc.

Abstraction

Hiding internal details and showing functionality is known as abstraction. For example phone call,
we don't know the internal processing.

In Java, we use abstract class and interface to achieve abstraction.

Encapsulation

Binding (or wrapping) code and data together into a single unit are known as encapsulation. For
example, a capsule, it is wrapped with different medicines.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all
the data members are private here.
2.c)
Method
A method is a block of code or collection of statements or a set of code grouped together to
perform a certain task or operation. It is used to achieve the reusability of code. We write a
method once and use it many times. We do not require to write code again and again. It also
provides the easy modification and readability of code, just by adding or removing a chunk of
code. The method is executed only when we call or invoke it.
Ex:
public class MyClass {
// Method
public int add(int a, int b) {
return a + b;
}
}

Types of Method

There are two types of methods in Java:

o Predefined Method
o User-defined Method

Let's see an example of the predefined method.

1. public class Demo


2. {
3. public static void main(String[] args)
4. {
5. // using the max() method of Math class
6. System.out.print("The maximum number is: " + Math.max(9,7));
7. }
8. }

Output:

The maximum number is: 9


***Let's create a user defined method that checks the number is even or odd. First, we will define
the method.

1. //user defined method


2. public static void findEvenOdd(int num)
3. {
4. //method body
5. if(num%2==0)
6. System.out.println(num+" is even");
7. else
8. System.out.println(num+" is odd");
9. }

Constructor

In Java, a constructor is a block of codes similar to the method. It is called when an instance of
the class is created. At the time of calling constructor, memory for the object is allocated in the
memory.

It is a special type of method which is used to initialize the object.

Every time an object is created using the new() keyword, at least one constructor is called.

It calls a default constructor if there is no constructor available in the class. In such case, Java
compiler provides a default constructor by default.

Types of Java constructors

There are two types of constructors in Java:

1. Default constructor (no-arg constructor)


2. Parameterized constructor

****Example of default constructor


1. class Bike1{
2. Bike1(){System.out.println("Bike is created");}
3. public static void main(String args[]){

4. Bike1 b=new Bike1();


5. }
}
***Example of parameterized constructor
1. //Java Program to demonstrate the use of the parameterized constructor.
2. class Student4{
3. int id;
4. String name;
5. //creating a parameterized constructor
6. Student4(int i,String n){
7. id = i;
8. name = n;
9. }
10. //method to display the values
11. void display(){System.out.println(id+" "+name);}
12.
13. public static void main(String args[]){
14. //creating objects and passing values
15. Student4 s1 = new Student4(111,"Karan");
16. Student4 s2 = new Student4(222,"Aryan");
17. //calling method to display the values of object
18. s1.display();
19. s2.display();
20. }
21. }

3.a) function of Constructor : Try it yourself


3.b) (V.V.I)
Function Overloading:

Function overloading refers to the ability to define multiple methods in the same class with the
same name but different parameters. The compiler determines which method to call based on the
number or type of arguments provided during the function call. Function overloading is a
compile-time (static) polymorphism.
Here's an example of function overloading in Java:

class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}

// Method to add three integers


public int add(int a, int b, int c) {
return a + b + c;
}
}

public class Main {


public static void main(String[] args) {
Calculator calculator = new Calculator();

int result1 = calculator.add(5, 7); // Calls the first add method


int result2 = calculator.add(3, 8, 2); // Calls the second add method

System.out.println("Result 1: " + result1); // Output: Result 1: 12


System.out.println("Result 2: " + result2); // Output: Result 2: 13
}
}

Function Overriding:
Function overriding occurs when a subclass provides a specific implementation of a method that
is already defined in its superclass. The overridden method in the subclass has the same name,
return type, and parameters as the method in the superclass. Function overriding is a runtime
(dynamic) polymorphism.

Here's an example of function overriding in Java:

java
Copy code
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}

class Circle extends Shape {


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

public class Main {


public static void main(String[] args) {
Shape shape1 = new Shape();
Shape shape2 = new Circle();

shape1.draw(); // Output: Drawing a shape


shape2.draw(); // Output: Drawing a circle
}
}
3.c)
Bytecode is important in Java because it1:
• Is a highly optimized set of instructions that is executed by the Java Virtual Machine.
• Helps Java achieve both portability and security.
• Is a low-level representation of Java code, executed by the JVM, and stored in .class files.
• Can run on any platform/machine regardless of the system’s architecture.
• Allows loading classes dynamically.
• Is device independent and helps make Java platform-independent.
4.a)

Final Keyword In Java

The final keyword in java is used to restrict the user. The java final keyword can be used in many
context. Final can be:

1. variable
2. method
3. class

1) Java final variable

If you make any variable as final, you cannot change the value of final variable(It will be constant).

Example of final variable

There is a final variable speedlimit, we are going to change the value of this variable, but It can't
be changed because final variable once assigned a value can never be changed.

1. class Bike9{
2. final int speedlimit=90;//final variable
3. void run(){
4. speedlimit=400;
5. }
6. public static void main(String args[]){
7. Bike9 obj=new Bike9();
8. obj.run();
9. }
10. }//end of class
Test it Now
Output:Compile Time Error

2) Java final method

If you make any method as final, you cannot override it.

Example of final method


1. class Bike{
2. final void run(){System.out.println("running");}
3. }
4.
5. class Honda extends Bike{
6. void run(){System.out.println("running safely with 100kmph");}
7.
8. public static void main(String args[]){
9. Honda honda= new Honda();
10. honda.run();
11. }
12. }
Test it Now
Output:Compile Time Error

3) Java final class

If you make any class as final, you cannot extend it.

Example of final class


1. final class Bike{}
2.
3. class Honda1 extends Bike{
4. void run(){System.out.println("running safely with 100kmph");}
5.
6. public static void main(String args[]){
7. Honda1 honda= new Honda1();
8. honda.run();
9. }
10. }
4.b)
What is class and object in java

In Java, a class is a blueprint or template for creating objects. It defines the structure and
behavior of objects of that type. A class acts as a blueprint because it specifies the attributes (also
known as fields or properties) and methods (functions) that an object of that class will have.
Classes are the fundamental building blocks of object-oriented programming in Java.
An object, on the other hand, is an instance of a class. It is created based on the blueprint
provided by the class. You can think of objects as concrete, tangible instances of a class that
have their own set of attributes and can perform actions defined by the class's methods. Here's
how you can create and use objects of the Car class.

5.a)
What is thread , how many ways thread can be created

In Java, a thread is a lightweight sub-process that runs concurrently with other threads within a
program. Threads allow a program to perform multiple tasks or operations concurrently, making
better use of multi-core processors and improving application responsiveness.

**There are two main ways to create threads in Java:

1. Extending the Thread Class::

class MyThread extends Thread {


public void run() {
// Code to be executed by the thread
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // Start the thread
}
}
2. Implementing the Runnable Interface:
class MyRunnable implements Runnable {
public void run() {
// Code to be executed by the thread
}
}

public class Main {


public static void main(String[] args) {
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start(); // Start the thread
}
5.b)
thread life cycle

1) New (Ready to run)

A thread is in New when it gets CPU time.

2) Running

A thread is in a Running state when it is under execution.

3) Suspended

A thread is in the Suspended state when it is temporarily inactive or under execution.

4) Blocked

A thread is in the Blocked state when it is waiting for resources.

5) Terminated

A thread comes in this state when at any given time, it halts its execution immediately.
5.c)
Here's a step-by-step explanation of how exceptions propagate in code:

• An exception is thrown
• The JVM searches for a matching catch block
• If no matching catch block is found in the current method, the method's context is popped
off the call stack:

**Here's an example to illustrate how exceptions propagate through the call stack:

public class ExceptionPropagationExample {


public static void main(String[] args) {
try {
method1();
} catch (Exception e) {
System.out.println("Exception caught in main(): " + e);
}
}

static void method1() {


method2();
}

static void method2() {


int[] arr = new int[3];
arr[4] = 5; // This line will throw an ArrayIndexOutOfBoundsException
}
}
6.a) Class variable, local variable,instance variable

• Class variables − Class variables also known as static variables are declared with the
static keyword in a class, but outside a method, constructor or a block. There would only
be one copy of each class variable per class, regardless of how many objects are created
from it.
• Instance variables − Instance variables are declared in a class, but outside a method.
When space is allocated for an object in the heap, a slot for each instance variable value is
created. Instance variables hold values that must be referenced by more than one method,
constructor or block, or essential parts of an object's state that must be present throughout
the class.
• Local variables − Local variables are declared in methods, constructors, or blocks. Local
variables are created when the method, constructor or block is entered and the variable will
be destroyed once it exits the method, constructor, or block.
6.b)
Advantages of Packages in Java:

Organization: Packages allow you to organize your Java classes into a hierarchical structure,
making it easier to manage and locate your code.

Encapsulation: Packages provide a level of access control, allowing you to hide classes and
methods that are not meant to be used outside of the package. This supports the principle of
encapsulation in object-oriented programming.

Name Clashes Prevention: Packages help prevent naming conflicts by allowing classes with the
same name to exist in different packages without ambiguity.

Reusability: Packages enable code reuse because classes within the same package can easily
access each other's methods and attributes.

Access Control: You can control the visibility of classes, methods, and fields by using access
modifiers like public, private, protected, and package-private (default) within packages.
6.c)
Multiple Inheritance (Not Allowed):
Multiple inheritance means a class trying to inherit attributes and methods from more than one
parent class.
Java doesn't support multiple inheritance for classes to avoid conflicts and ambiguities.
For example, if a class wants to inherit from both Class A and Class B, it's not allowed in Java.
Multilevel Inheritance (Allowed):
Multilevel inheritance involves a chain of classes where one class inherits from another, and
another class inherits from that, forming a hierarchy.
It's like a relay race, where each runner passes the baton to the next runner.
For example, Class A can inherit from Object, Class B can inherit from Class A, and so on.
7.a)
Data Encapsulation: Data encapsulation is a concept in object-oriented programming that refers
to the bundling of data (attributes or properties) and methods (functions) that operate on that data
into a single unit known as a class. It restricts direct access to some of an object's components,
providing control over the data's state and preventing unauthorized modification.

JVM (Java Virtual Machine): JVM is a software component that executes Java bytecode,
which is the compiled form of Java source code. It allows Java programs to run on different
platforms without modification. JVM provides memory management, garbage collection, and
execution of Java applications.

JDK (Java Development Kit): JDK is a software development kit provided by Oracle (and
other vendors) for building Java applications. It includes the Java compiler (javac), libraries,
development tools, and documentation needed to develop, compile, and run Java programs.

JRE (Java Runtime Environment): JRE is a subset of the JDK and is required to run Java
applications. It includes the JVM and essential libraries, but it lacks the development tools (like
the Java compiler) present in the JDK. JRE is used for executing Java applications but not for
development.
7.c)
discuss about different between interface and abstract class
Interface:
• An interface in Java is like a contract that defines a set of method signatures but doesn't
provide any implementation.
• All methods in an interface are implicitly public and abstract (no method body).
• A class can implement multiple interfaces.
• Interfaces are used for achieving multiple inheritance-like behavior in Java.
• Interfaces are ideal for defining a common set of methods that multiple classes must
implement.
Abstract Class:
• An abstract class is a class that cannot be instantiated on its own and is meant to be
subclassed by other classes.
• It can have both abstract (methods without implementation) and concrete (methods with
implementation) methods.
• A class can extend only one abstract class.
• Abstract classes are used when you want to provide a common base for a group of related
classes.
• Abstract classes can have fields, constructors, and method implementations.
8.a)
Do final, finally, finalize keyword is same function in java

No, the final, finally, and finalize keywords in Java serve different purposes and are not related
in terms of functionality.
1. final Keyword:
• The final keyword is used to declare a constant value, make a class not
inheritable, or prevent method overriding.
• When applied to a variable, it makes the variable's value unchangeable
(immutable).
2. finally Block:
• The finally block is used in a try-catch-finally construct to ensure that a block of
code is always executed, regardless of whether an exception is thrown or caught.
• It is typically used for cleanup operations, such as closing resources (e.g., files or
database connections) that must be performed regardless of whether an exception
occurs.
3. finalize Method:
• The finalize method is a method provided by the java.lang.Object class. It is
used for performing cleanup operations on an object just before it is garbage
collected.
• It's rarely used in modern Java programming because explicit resource
management and cleanup are usually preferred over relying on the finalize
method.
Ex-Final
final int x = 10; // Declares a constant variable
final class FinalClass { /* ... */ } // Declares a final class
void finalMethod() final { /* ... */ } // Declares a final method

Ex-Finally
try {
} catch (Exception e) {
}
Finally
}
Ex-Finalize
class MyClass {
protected void finalize() {
}
}
8.b)
When you use super class

In Java, the super keyword is used to access and invoke members (fields or methods) of a
superclass (parent class) from a subclass (child class). It is primarily used in the context of
inheritance to differentiate between members with the same name in the superclass and subclass.
There are several scenarios where you might use the super keyword:
1. Accessing Superclass Members:.
2. Accessing Superclass Methods
3. Invoking Superclass Constructors:

8.c)
can be static method is ovverridding , Can static method is overload .?
1. Static Method Overriding: No, static methods cannot be overridden in the same way
that instance methods can be overridden. Inheritance and method overriding apply to
instance methods, not static methods. When a subclass declares a static method with the
same name as a static method in the superclass, it simply hides the superclass's static
method; it does not override it.
2. Static Method Overloading: Yes, static methods can be overloaded. Method
overloading means defining multiple methods in the same class with the same name but
different parameter lists (different types or number of parameters). Overloaded methods,
including static ones, are differentiated by their parameter lists.

You might also like