classes.
The static keyword belongs to the class than an instance of the
class.
1) Java static variable
If you declare any variable as static, it is known as a static variable.
o The static variable can be used to refer to the common property of all
objects (which is not unique for each object), for example, the company
name of employees, college name of students, etc.
o The static variable gets memory only once in the class area at the time
of class loading.
2) Java static method
If you apply static keyword with any method, it is known as static method.
o A static method belongs to the class rather than the object of a class.
o A static method can be invoked without the need for creating an
instance of a class.
o A static method can access static data member and can change the value
of it.
Q) Why is the Java main method static?
Ans) It is because the object is not required to call a static method. If it were
a non-static method, JVM creates an object first then call main() method that
will lead the problem of extra memory allocation.
3) Java static block
o Is used to initialize the static data member.
o It is executed before the main method at the time of classloading.
16 | P a g e
Q) Can we execute a program without main() method?
Ans) No, one of the ways was the static block, but it was possible till JDK
1.6. Since JDK 1.7, it is not possible to execute a Java class without the main
method.
this keyword in Java
There can be a lot of usage of Java this keyword. In Java, this is
a reference variable that refers to the current object.
Java Inheritance
Inheritance in Java
Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviours of a parent object.
Inheritance represents the IS-A relationship which is also known as
a parent-child relationship.
Why use inheritance in java
o For Method Overriding (so runtime polymorphism can be achieved).
17 | P a g e
o For Code Reusability.
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java:
single, multilevel and hierarchical.
1) Single Inheritance
When a class inherits another class, it is known as a single inheritance.
2)Multilevel Inheritance Example
When there is a chain of inheritance, it is known as multilevel inheritance.
3)Hierarchical Inheritance Example
When two or more classes inherits a single class, it is known as hierarchical
inheritance.
Q) Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is
not supported in java.
Aggregation in Java
If a class have an entity reference, it is known as Aggregation. Aggregation
represents HAS-A relationship.
Method Overloading in Java
If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.
If we have to perform only one operation, having same name of the methods
increases the readability of the program.
18 | P a g e
Q) Why Method Overloading is not possible by changing the
return type of method only?
In java, method overloading is not possible by changing the return type of
the method only because of ambiguity.
Q) Can we overload java main() method?
Yes, by method overloading. You can have any number of main methods in
a class by method overloading. But JVM calls main() method which receives
string array as arguments only.
Java Polymorphism
Method Overriding in Java
If subclass (child class) has the same method as declared in the parent class,
it is known as method overriding in Java.
In other words, If a subclass provides the specific implementation of the
method that has been declared by one of its parent class, it is known as
method overriding.
19 | P a g e
Covariant Return Type
The covariant return type specifies that the return type may vary in the same
direction as the subclass.
Super Keyword in Java
The super keyword in Java is a reference variable which is used to refer
immediate parent class object.
Instance initializer block
Instance Initializer block is used to initialize the instance data member. It
run each time when object of the class is created.
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
20 | P a g e
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).
2) Java final method
If you make any method as final, you cannot override it.
3) Java final class
If you make any class as final, you cannot extend it.
Q) Is final method inherited?
Ans) Yes, final method is inherited but you cannot override it.
Q) What is blank or uninitialized final variable?
A final variable that is not initialized at the time of declaration is known as
blank final variable.
Q) Can we initialize blank final variable?
Yes, but only in constructor.
Q) What is final parameter?
If you declare any parameter as final, you cannot change the value of it.
21 | P a g e
Q) Can we declare a constructor final?
No, because constructor is never inherited.
Polymorphism in Java
Polymorphism in Java is a concept by which we can perform a single action
in different ways.
There are two types of polymorphism in Java: compile-time polymorphism
and runtime polymorphism. We can perform polymorphism in java by
method overloading and method overriding.
Runtime Polymorphism in Java
Runtime polymorphism or Dynamic Method Dispatch is a process in
which a call to an overridden method is resolved at runtime rather than
compile-time.
Upcasting
If the reference variable of Parent class refers to the object of Child class, it
is known as upcasting.
Static Binding and Dynamic Binding
Connecting a method call to the method body is known as binding.
There are two types of binding
1. Static Binding (also known as Early Binding).
2. Dynamic Binding (also known as Late Binding).
22 | P a g e
static binding
When type of the object is determined at compiled time (by the compiler), it
is known as static binding.
Dynamic binding
When type of the object is determined at run-time, it is known as dynamic
binding.
Java instanceof
The java instanceof operator is used to test whether the object is an instance
of the specified type (class or subclass or interface).
The instanceof in java is also known as type comparison operator because it
compares the instance with type. It returns either true or false.
Java Abstraction
Abstract class in Java
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).
23 | P a g e
Abstraction in Java
Abstraction is a process of hiding the implementation details and showing
only functionality to the user.
Ways to achieve Abstraction
There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
Abstract Method in Java
A method which is declared as abstract and does not have implementation is
known as an abstract method.
Interface in Java
An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
24 | P a g e
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.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
Q) What is marker or tagged interface?
An interface which has no member is known as a marker or tagged interface,
for example, Serializable, Cloneable, Remote, etc. They are used to provide
some essential information to the JVM so that JVM may perform some useful
operation.
Difference between abstract class and interface
Abstract class Interface
1) Abstract class can have abstract and Interface can have only abstract methods.
non-abstract methods. Since Java 8, it can have default and static
methods also.
2) Abstract class doesn't support Interface supports multiple inheritance.
multiple inheritance.
25 | P a g e
3) Abstract class can have final, non- Interface has only static and final variables.
final, static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation
implementation of interface. of abstract class.
5) The abstract keyword is used to The interface keyword is used to declare
declare abstract class. interface.
6) An abstract class can extend another An interface can extend another Java interface
Java class and implement multiple Java only.
interfaces.
7) An abstract class can be extended An interface can be implemented using
using keyword "extends". keyword "implements".
8) A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface
achieves fully abstraction (100%).
Java Encapsulation
Java Package
A java package is a group of similar types of classes, interfaces and sub-
packages.
Package in java can be categorized in two form, built-in package and user-
defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net,
io, util, SQL etc.
26 | P a g e
Sub package in java
Package inside the package is called the sub package. It should be created to
categorize the package further.
Access Modifiers in Java
There are two types of modifiers in Java: access modifiers and non-access
modifiers.
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.
Access within within outside package outside
Modifier class by subclass only package
package
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Encapsulation in Java
27 | P a g e
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.
Advantage of Encapsulation in Java
By providing only a setter or getter method, you can make the class read-
only or write-only. In other words, you can skip the getter or setter methods.
It provides you the control over the data. Suppose you want to set the value
of id which should be greater than 100 only, you can write the logic inside
the setter method.
It is a way to achieve data hiding in Java because other class will not be
able to access the data through the private data members.
Java Array
Java Arrays
Java array is an object which contains elements of a similar data type.
Additionally, The elements of an array are stored in a contiguous memory
location. It is a data structure where we store similar elements. We can store
only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th
index, 2nd element is stored on 1st index and so on.
Advantages
o Code Optimization: It makes the code optimized, we can retrieve or
sort the data efficiently.
o Random access: We can get any data located at an index position.
Disadvantages
o Size Limit: We can store only the fixed size of elements in the array.
It doesn't grow its size at runtime. To solve this problem, collection
framework is used in Java which grows automatically.
28 | P a g e
Types of Array in java
There are two types of array.
o Single Dimensional Array
o Multidimensional Array
Java OOPs Misc.
Object class in Java
The Object class is the parent class of all the classes in java by default. In
other words, it is the topmost class of java.
The Object class is beneficial if you want to refer any object whose type you
don't know. Notice that parent class reference variable can refer the child
class object, know as upcasting.
1. Object obj=getObject();//we don't know what object will be returned from t
his method
Object Cloning in Java
The object cloning is a way to create exact copy of an object. The clone()
method of Object class is used to clone an object.
The [Link] interface must be implemented by the class whose
object clone we want to create. If we don't implement Cloneable interface,
clone() method generates CloneNotSupportedException.
Why use clone() method ?
The clone() method saves the extra processing task for creating the exact
copy of an object. If we perform it by using the new keyword, it will take a
lot of processing time to be performed that is why we use object cloning.
29 | P a g e
Advantage of Object cloning
o You don't need to write lengthy and repetitive codes. Just use an
abstract class with a 4- or 5-line long clone() method.
o It is the easiest and most efficient way for copying objects, especially
if we are applying it to an already developed or an old project.
o Clone() is the fastest way to copy array.
Disadvantage of Object cloning
o To use the [Link]() method, we have to change a lot of syntaxes
to our code, like implementing a Cloneable interface, defining the
clone() method and handling CloneNotSupportedException, and
finally, calling [Link]() etc.
o We have to implement cloneable interface while it doesn't have any
methods in it.
Java Math class
Java Math class provides several methods to work on math calculations like
min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc.
Unlike some of the Strict Math class numeric methods, all implementations
of the equivalent function of Math class can't define to return the bit-for-bit
same results.
Wrapper classes in Java
The wrapper class in Java provides the mechanism to convert primitive
into object and object into primitive.
Since J2SE 5.0, autoboxing and unboxing feature convert primitives into
objects and objects into primitives automatically. The automatic conversion
of primitive into an object is known as autoboxing and vice-versa unboxing.
Autoboxing
The automatic conversion of primitive data type into its corresponding
wrapper class is known as autoboxing, for example, byte to Byte, char to
30 | P a g e