Java All 36
Java All 36
Explain
concepts of OOP.
Object:-
► Any entity that has state and behavior is known
as an object.
► Object take up space in memory and have an
associated address like a record.
► Objects are instance of a class.
Class:-
► Collection of objects is called class. It is a logical
entity.
► A class can also be defined as a blueprint from
which you can create an individual object. Class
doesn't consume any space.
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.
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.
Inheritance:-
► When one object acquires all the properties
and behaviors of a parent object, it is known as
inheritance.
► It provides code reusability.
Polymorphism:-
► If one task is performed in different ways, it is
known as polymorphism.
► In Java, we use method overloading and
method overriding to achieve polymorphism.
2)What is an Object?
Ans : An object is an instance of a class,
representing a real-world entity or concept. It has
its own set of attributes (data) and methods
(functions) that describe and define its behavior.
Example:
➢Attributes:
- Color
- Model
- Year
- Speed
➢Methods:
- Start engine
- Accelerate
- Brake
- Turn
3)What is a Class?
Syntax:
Example:
void accelerate() { //
Implementation
}
4)What is encapsulation ?
Ans : Encapsulation:-
►Encapsulation is a fundamental concept in
object-oriented programming (OOP) that refers to
the binding of data (variables) and methods
(functions) that operate on the data into a single
unit, or class. It restricts direct access to some of
the object's components, which is a means of
preventing accidental or unauthorized
modification of data.
► A java class is the example of encapsulation.
5) what is abstraction ?
Ans:
Abstraction:-
► Abstraction is the concept of hiding the
internal details and complexities of a system while
exposing only the essential features, allowing you
to focus on what an object does.
► In Java, we use abstract class and interface to
achieve abstraction.
2. Secure
3. Portable
4. Object-oriented
5. Robust (strong memory management.
automatic garbage collection) 6.
Multithreaded(many tasks at once )
7. Architecture-neutral(no implementation
dependent features)
8. Interpreted
Componets :
1)Java Source Code (.java file):
• Written by developers in Java, these files
contain the source code.
2)Java Compiler (javac):
• Compiles .java files into platform-independent
bytecode stored in .class files.
3)Java Bytecode (.class file):
• Intermediate code that can run on any
platform with a JVM.
4)Java Virtual Machine (JVM):
• Executes Java bytecode. It loads classes,
verifies bytecode, interprets it, or compiles it
to native code using the JIT compiler.
5)Java Runtime Environment (JRE):
• Includes the JVM and core libraries required
to run Java applications.
6)Java Development Kit (JDK):
• A superset of the JRE that includes
development tools like the compiler (javac)
and debugger, used for writing and compiling
Java programs.
2. Class Declaration:
o Every Java program must have at least
one
class declaration.
args) { ... }
• Loads code
• Verifies code
• Executes code
• Provides runtime environment
Example:
// Default Constructor
Person() {
this.name = "Unknown";
this.age = 0;
}
}
(ii)Parameterized Constructor: A constructor with
parameters.
Example:
class Person {
private String name;
private int age;
// Parameterized Constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
(iii) Copy Constructor: A constructor that creates
a copy of an object.
Example:
class Person {
private String name;
private int age;
// Copy Constructor
Person(Person other) {
this.name = other.name;
this.age = other.age;
}
}
// Nullify references
obj1 = null; // obj1 is no longer reachable
obj2 = null; // obj2 is no longerreachable
{
int c;
c = a1 + b1;
System.out.println(“Sum of a and b is : ”+c);
}
void add(int a1,int b1,int c1)
{
int d;
d = a1 + b1 + c1;
System.out.println(“Sum of a, b and c is : ”+d);
}
public static void main(String
args[]) {
Method m = new Method();
m.add(10,20);
m.add(10,20,30);
}
}
Ex:
void print(int a) {}
void print(double a) {}
➢Order of Parameters: The order of parameters
can be different.
Ex:
void print(int a, double b) {}
void print(double a, int b) {}
return a + b;
Ex :
class MainMethodOverload1
{
// Overloaded main() method 1
//invoked when an int value is passed
public static void main(Integer args)
{
System.out.println("Overloaded main() method invoked that
passes an integer argument");
}
// Overloaded main() method 2
//invoked when a char is passed public
static void main(char args)
{
System.out.println("Overloaded main() method invoked that
passes a char argument");
}
//Original main() method public
static void main(String args[]) {
Ans:
Static variable: If you declare any variable as
static, it is known as a static variable.
3. Syntax:
static String college='LJ';
class Employee
{
static int numberOfEmployee = 0;
}
Employee()
numberOfEmployee++;
}
int getNumberOfEmployee()
return numberOfEmployee;
}
System.out.println(emp1.getNumberOfEmployee
());
System.out.println(emp1.getNumberOfEmployee
());
Output :
1
1
➢Static Method:
System.out.println("Instance method");
}
public static void main(String
args[])
{
// Calling static method without
object
New1.staticMethod();
Ans:
Example:
class Outer
{
private int data =
100;
void display()
class Inner
void display1()
{
System.out.println(data);
}
}
inn.display1();
args[])
out.display();
}
24)what is Unicode?
Ans: A unicode is a computing industry
standard designed to consistently and
uniquely encode character used in written
languages throughout the world, the unicode
standard uses hexa-decimal to express a
character
(Note:-unicode has a 16bit)
25) What is Inheritance? Explain its types.
Ans: Inheritance is one of the key features of
Object Oriented Programming.
• Inheritance provided mechanism
that allowed a class to inherit
property of another class.
• Inheritance represents the
relationship, that also known as
parent-child relationship.
• When a Class extends another
class it inherits all non-private
members including fields and
methods.
• It's having one class as a parent
class (called a super class) and
another class as a child of the
parent (the sub class). The child
class is said to be derived from the
parent class.
Types of Inheritance :
1)Single Inheritance : There is only
one Super class and only one Sub class ,
means they have one to one communication.
Super Class
Sub Class
Sub Class
Sub Class
Super Class
Super Class
class Outer
{
private int data = 100;
void display()
{
class Inner
{
void print()
{
System.out.println(data);
}
}
Ans:
ln inheritance, constructors work in a specific
order, known as constructor chaining. Here's how
it works:
example:
class Animal {
Animal(String name) {
System.out.println("Animal
constructor called for: " + name);
super(name);
class Main1 {
}
Output:
Animal constructor called for: Buddy
In this example:
- The 'Dog' class constructor calls the
‘Animal' class constructor using super(name). -
The 'Animal' class constructor initializes the
'name' field and prints a message. - The 'Dog'
class constructor initializes the ‘name’ field
and prints a message.
Here's an example:
class Animal {
String name;
String color = "White";
Animal(String name) {
this.name = name;
System.out.println("Animal constructor called
for: " + name);
}
void sound()
{
System.out.println("The animal makes a
sound");
}
void display()
{
System.out.println("Color in animal is :
"+color);
}
}
class Dog extends Animal {
Dog(String name) {
super(name); // Calls the constructor of the
superclass (Animal)
}
void display()
{
System.out.println("Color in dog is : "+color);
System.out.println("Color in animal is :
"+super.color); //Accessing superclass field
}
}
Example: class A
{
void disp()
{
System.out.println("Hello");
}
}
class B extends A
{
void disp()
{
System.out.println("Hi");
}
Ans:
Method must be same name as in the
parent class.
Method must have the same
parameter as in the parent class.
There must be an IS-A relationship
(inheritance).
Static or final methods can not be
override.
Ans :
class Calculator
{
int add(int a, int b)
{
return a + b;
}
double add(double a, double b)
{
return a + b;
}
class Dog extends Animal { void
sound() {
System.out.println("Dog barks");
}
}
(ii)Interface (100%)
Syntax :
abstract class A{}
Example :
abstract class A
{
abstract void display();
void print();
class B extends A
void display()
{
System.out.println("I will print your
details");
}
}
class Main1{
public static void main(String args[])
B bh = new B();
bh.display();
bh.print();
Example:
final int MAX SIZE = 10;
MAX_SIZE = 20; // Compile-time error
Example:
class Animal {
final void sound() {
System.out.println("Animal makes a sound”);
}
}
}
3. Final Classes : A class declared as
'final' cannot be subclassed.
Example:
final class Animal {}
Class A Class B
Class C
interface Animal {
void eat();
}
interface Pet {
void play();
interface Animal {
interface Pet {
default void
sound() {
System.out.pr
intln("Pet
makes a
sound");
}
}
myDog.sound();
1. Byte
2. Short
3. Integer
4. Long
5. Float
6. Double
7. Character
8. Boolean
int primitiveInt = 5;
+ wrappedInt);
}
34) What is Interface? Explain working of
interface in class with proper example.
Ans : 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.
Example :
interface First
{
void display(); //public abstract by default
}
interface First
{
void display();
}
interface Second
{
void display1();
}