Java Viva
Java Viva
Java Viva
Q) History of Java ?
Q) What is Platform ?
Q) Applications of Java ?
Desktop Applications.
Web Applications.
Banking applications.
Mobile Applications.
Q) Features of Java ?
1. Simple - Java is very easy to learn, and its syntax is simple, clean and easy to
understand.
2. Object-Oriented - Java is an object-oriented programming language. Everything
in Java is an object.
3. Portable - Java is portable because it facilitates you to carry the Java bytecode to
any platform. It doesn't require any implementation.
4. Platform independent - Java is platform independent because it is different from
other languages like C, C++, etc. which are compiled into platform specific
machines while Java is a write once, run anywhere language.
5. Secured - Java is best known for its security. With Java, we can develop virus-free
systems.
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
JRE : Java Runtime Environment is a set of software tools which are used for
developing Java applications. It is used to provide the runtime environment. It is the
implementation of JVM. It physically exists. It contains a set of libraries + other files
that JVM uses at runtime.
Compilation Process :
Data Types :
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
Operators in Java
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Types of Variables
local variable - A variable declared inside the body of the method is called local
variable.
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.
static variable - A variable that is declared as static is called a static variable. It cannot
be local.
Q) What is an Array?
If 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.
A. The method must have the same name as in the parent class.
B. The method must have the same parameter as in the parent class.
class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}
//Creating a child class
class Bike extends Vehicle
{
public static void main(String args[])
{
//creating an instance of child class
Bike obj = new Bike();
//calling the method with child class instance
obj.run();
}
}
Output:
Vehicle is running
Method Overloading
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Java Package
Inheritance
Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object.
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
In Java, an exception is an event that disrupts the normal flow of the program. It is an
object which is thrown at runtime.
Java try block is used to enclose the code that might throw an exception. It must be
used within the method.
Java catch block is used to handle the Exception by declaring the type of exception
within the parameter. The declared exception must be the parent class exception ( i.e.,
Exception) or the generated exception type.
public class TryCatchExample2 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
//handling the exception
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}
Java finally block is a block used to execute important code such as closing the
connection, etc.
The wrapper class in Java provides the mechanism to convert primitive data types into
object and object into primitive data types. Java Wrapper classes wrap the primitive
data types, that is why it is known as wrapper classes.
Input in Java :
Java Scanner class allows the user to take input from the console. It belongs
to java.util package.
Scanner sc=new Scanner(System.in);
Interface in Java
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
How to achieve complete abstraction in java ?
In java, abstraction is achieved by interfaces and abstract classes. We can achieve
100% abstraction using interfaces. Abstract classes and Abstract methods : An
abstract class is a class that is declared with an abstract keyword.
A class which is declared with the abstract keyword is known as an abstract class
in Java. It can have abstract and non-abstract methods (method with the body).
A method which is declared as abstract and does not have implementation is known as
an abstract method.
import: Java import keyword makes classes and interfaces available and accessible to
the current source code.
This keyword : This can be used to refer current class instance variable. This can be
used to invoke current class method (implicitly). This() can be used to invoke current
class constructor.
OOPS IN JAVA :
What is class and object ?
Class is a collection of objects of similar type.Once a class has been defined, we can
create any number of objects belonging to that class.
What is inheritance?
Inheritance is the process of acquiring the properties of the exiting class into the new
class. The existing class is called as base/parent class and the inherited class is called
as derived/child class
What is polymorphism ?
Polymorphism is one of the OOPs feature that allows us to perform a single action in
different ways.
What do you mean by abstraction ?
Abstraction means displaying only essential information and hiding the details. (Or)
Abstraction refers to hiding the internal implementation and exhibiting only the
necessary details.
Define Encapsulation ?
Data abstraction :- displaying the only essential information and hiding the details .
Inheritance :- process by which objects of one class acquire the properties of object of
another class.