Professional Documents
Culture Documents
b)
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
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.
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.
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.
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.
There are mainly three reasons to use interface. They are given below.
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)
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.
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.
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
o Predefined Method
o User-defined Method
Output:
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.
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.
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;
}
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.
java
Copy code
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
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
If you make any variable as final, you cannot change the value of final variable(It will be constant).
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
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.
2) Running
3) Suspended
4) Blocked
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:
• 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.