JAVA
JAVA
1.What is Java?
Java is a high-level, general-purpose, object-oriented, and secure programming language developed
by James Gosling at Sun Microsystems, Inc. in 1991. It is formally known as OAK. In 1995, Sun
Microsystem changed the name to Java. In 2009, Sun Microsystem takeover by Oracle Corporation.
2.Feature of Java:
o Simple: Java is a simple language because its syntax is simple, clean, and easy to understand.
Complex and ambiguous concepts of C++ are either eliminated or re-implemented in Java. For
example, pointer and operator overloading are not used in Java.
o Object-Oriented: In Java, everything is in the form of the object. It means it has some data
and behavior. A program must have at least one class and object.
o Robust: Java makes an effort to check error at run time and compile time. It uses a strong
memory management system called garbage collector. Exception handling and garbage
collection features make it strong.
o Secure: Java is a secure programming language because it has no explicit pointer and
programs runs in the virtual machine. Java contains a security manager that defines the access
of Java classes.
o Platform-Independent: Java provides a guarantee that code writes once and run anywhere.
This byte code is platform-independent and can be run on any machine.
C++ Java
C++ is platform-dependent. Java is platform-independent.
C++ is mainly used for system programming Java is mainly used for application programming. It is
widely used in Windows-based, web-based, enterprise,
and mobile applications.
C++ was designed for systems and applications Java was designed and created as an interpreter for
programming. It was an extension of the C printing systems but later extended as a support
programming languages. network computing. It was designed to be easy to use
and accessible to a broader audience.
C++ uses compiler only. C++ is compiled and run Java uses both compiler and interpreter. Java source
using the compiler which converts source code code is converted into bytecode at compilation time.
into machine code so, C++ is platform dependent. The interpreter executes this bytecode at runtime and
produces output. Java is interpreted that is why it is
platform-independent.
C++ doesn't support documentation comments. Java supports documentation comment (/** ... */) to
create documentation for java source code.
5.What is Variable ?
A variable is a container which holds the value while the java program is executed. A variable is
assigned with a data type.
1) Local Variable: A variable declared inside the body of the method is called local variable. You can
use this variable only within that method and the other methods in the class aren't even aware that the
variable exists.A local variable cannot be defined with "static" keyword.
2) Instance Variable: A variable declared inside the class but outside the body of the method, is
called an instance variable. It is not declared as static.It is called an instance variable because its value
is instance-specific and is not shared among instances.
3) Static variable: A variable that is declared as static is called a static variable. It cannot be local.
You can create a single copy of the static variable and share it among all the instances of the class.
Memory allocation for static variables happens only once when the class is loaded in the memory.
6.What is Method?
A method is a block of code which only runs when it is called. Methods are used to perform certain
actions, and they are also known as functions.
Why use methods?
To reuse code: define the code once, and use it many times.
A method must be declared within a class. It is defined with the name of the method, followed by
parentheses (). Java provides some pre-defined methods, such as System.out.println().
Advantage of Method
o Code Reusability
o Code Optimization
Data types specify the different sizes and values that can be stored in the variable. There are two types
of data types in Java:
Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and
double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
8.What is OOP?
Alan Kay coined the term “object oriented programming” in 1966 .
Procedural programming is about writing procedures or methods that perform operations on the data,
while object-oriented programming is about creating objects that contain both data and methods.
Class: Laptop
In your example, Laptop is a class. It serves as a blueprint for creating laptop objects. It defines the
structure and behavior that all laptops should have.
Attributes: Attributes, also known as fields or properties, are characteristics or data associated with a
class. In the context of a Laptop class, attributes could include:
Brand: This attribute represents the brand of the laptop, such as HP, Lenovo, or Dell.
Model: It specifies the model or series of the laptop, like Pavilion, ThinkPad, or XPS.
In your example, HP, Lenovo, and Dell are objects created from the Laptop class. These objects are
instances of the Laptop class and have their own unique values for the attributes.
10.What is Polymorphism?
Polymorphism allows us to perform a single action in different ways. In other words, polymorphism
allows you to define one interface and have multiple implementations. The word “poly” means
many and “morphs” means forms, So it means many forms.Polymorphism promotes code reusability
and flexibility because you can write code that can work with a wide variety of objects that share a
common superclass.
Example: Animals
Consider an animal hierarchy with a common superclass Animal and subclasses like Dog, Cat, and
Bird. The Animal class may have a method called makeSound(). Polymorphism enables you to
interact with animals in a uniform way by calling makeSound() on any animal object. The specific
sound produced (barking, meowing, or chirping) depends on the actual type of animal.
Method Overloading:
When there are multiple functions with the same name but different parameters then these functions
are said to be overloaded. Functions can be overloaded by changes in the number of arguments
or/and a change in the type of arguments. Method overriding is used for Compile-time polymorphism
Example: Calculator
The Calculator class has two add methods with different parameter lists—one for integers and one for
double numbers. When you call the add method, the compiler determines which version of the method
to invoke based on the argument types you provide.
class Calculator {
// Method to add two integers
public int add(int num1, int num2) {
return num1 + num2;
}
// Method to add two double numbers
public double add(double num1, double num2) {
return num1 + num2;
}
}
Method OverRiding:
A subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java. Method overriding is used for runtime polymorphism
For a BankAccount hierarchy, a superclass may define an interestRate method. Subclasses like
SavingsAccount and FixedDeposit can override it with their own interest rate calculations.
class BankAccount {
protected double balance;
BankAccount(double balance) {
this.balance = balance;
}
double calculateInterest() {
return 0.0; // Default interest for a generic bank account
}
}
class SavingsAccount extends BankAccount {
SavingsAccount(double balance) {
super(balance);
}
@Override
double calculateInterest() {
// Specific interest calculation formula for a savings account
double interestRate = 0.03; // 3% annual interest rate for savings
return balance * interestRate;
}
}
class FixedDeposit extends BankAccount {
FixedDeposit(double balance) {
super(balance);
}
@Override
double calculateInterest() {
// Specific interest calculation formula for a fixed deposit
double interestRate = 0.05; // 5% annual interest rate for fixed deposit
return balance * interestRate;
}
}
public class Main {
public static void main(String[] args) {
SavingsAccount savingsAccount = new SavingsAccount(1000.0);
FixedDeposit fixedDeposit = new FixedDeposit(2000.0);
Double savingsInterest=savingsAccount.calculateInterest();
Double fixedDepositInterest = fixedDeposit.calculateInterest();
System.out.println("Savings Account Interest: " + savingsInterest);
System.out.println("Fixed Deposit Interest: " + fixedDepositInterest);
}
}
Disadvantages of Polymorphism
Can make it more difficult to understand the behavior of an object, especially if the code is
complex.
This may lead to performance issues, as polymorphic behavior may require additional
computations at runtime.
Advantages of Polymorphism in Java
Increases code reusability by allowing objects of different classes to be treated as objects of a
common class.
Improves readability and maintainability of code by reducing the amount of code that needs
to be written and maintaince.
11.What is Inheritance?
Getting the properties from base class to derived class. The new class that is created is known
as subclass (child or derived class) and the existing class from where the child class is derived is
known as superclass (parent or base class).
The extends keyword is used to perform inheritance
The super keyword is used to call the method of the parent class from the method of the child
class.
The most important use of inheritance in Java is code reusability. The code that is present in the parent
class can be directly used by the child class.
1. Single Inheritance
In single inheritance, a single subclass extends from a single superclass. For example,
2. Multilevel Inheritance
example,
3.Hierarchical Inheritance
4.Multiple Inheritance
5.Hybrid Inheritance
12.What is Encapsulation?
Encapsulation in Java is a process of wrapping code and data together into a single unit, for
example, a capsule which is mixed of several medicines.
Example: SmartPhone
Imagine you have a smartphone, such as an iPhone or an Android device. A smartphone is a complex
piece of technology with various hardware components and software functionalities. It's a great
example of encapsulation because it encapsulates its internal workings, data, and functionalities from
the user.
13.What is Abstraction?
Abstraction in Java is a process of hiding the implementation details from the user and showing only
the functionality to the user. It can be achieved by using abstract classes, methods, and interfaces.
14.What is JRE?
The Java Runtime Environment (JRE) is software that Java programs require to run correctly.
JRE is the part of the Java Development Kit (JDK). It is a freely available software distribution which
has Java Class Library, specific tools, and a stand-alone JVM. It is the most common environment
available on devices to run java programs. The source Java code gets compiled and converted to Java
bytecode.
15.What is JVM?
The JVM is software that runs the Java program line by line. A Java Virtual Machine (JVM) is a program
that interprets Java bytecode to run as a program by providing a runtime environment that executes
this process.
16.What is JDK?
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software
development environment which is used to develop java applications and applets. It physically exists.
It contains JRE + development tools.