0% found this document useful (0 votes)
20 views6 pages

Javacollection

Uploaded by

sudarsank815
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Javacollection

Uploaded by

sudarsank815
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

finel keyword-: Java final keyword is used to indicate that a variable holds a

constant value. It is used with a variable. It is used to restrict the user from
updating the value of the variable.

Inhertence :- Inheritance in Java is a mechanism in which one object acquires all


the properties and behaviors of a parent object. It is an important part of OOPs
(Object Oriented programming system).
Inheritance represents the IS-A relationship which is also known as a parent-child
relationship.
For Method Overriding (so runtime polymorphism can be achieved).
For Code Reusability.

Class: A class is a group of objects which have common properties. It is a template


or blueprint from which objects are created.

Sub Class/Child Class: Subclass is a class which inherits the other class. It is
also called a derived class, extended class, or child class.

Super Class/Parent Class: Superclass is the class from where a subclass inherits
the features. It is also called a base class or a parent class.

Aggregation - If a class have an entity reference, it is known as Aggregation.


Aggregation represents HAS-A relationship.
For Code Reusability.

Abstraction: - is a process of hiding the implementation details and showing only


functionality to the user

An abstract class must be declared with an abstract keyword.


It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not to change the body of
the method.

Interface in Java:
- The interface in Java is a mechanism to achieve abstraction. There can be only
abstract methods in the Java interface, not method body.
ava Interface also represents the IS-A relationship.
It is used to achieve abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling.

Polymorphism :
in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means
many and "morphs" means forms. So polymorphism means many forms.

here 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.

compile time polymorphism =


If you overload a static method in Java, it is the example of compile time
polymorphism

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.
Super Keyword in Java :
The super keyword in Java is a reference variable which is used to refer immediate
parent class object.
Usage of Java super Keyword
super can be used to refer immediate parent class instance variable.
super can be used to invoke immediate parent class method.
super() can be used to invoke immediate parent class constructor.

The final keyword :


in java is used to restrict the user. The java final keyword can be used in many
context. Final can be:

variable
method
class
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.
encapsulation in java
We can create a fully encapsulated class in Java by making all the data members of
the class private. Now we can use setter and getter methods to set and get the data
in it.
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.

package :
A java package is a group of similar types of classes, interfaces and sub-packages.

Method Overloading:
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.
Advantage of method overloading
Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

By changing number of arguments


By changing the data type
In Java, Method Overloading is not possible by changing the return type of the
method only.

method overriding:
If 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 to provide the specific implementation of a method which
is already provided by its superclass.
Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.
There must be an IS-A relationship (inheritance).

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.
Use of Wrapper classes in Java
Java is an object-oriented programming language, so we need to deal with objects
many times like in Collections, Serialization, Synchronization, etc. Let us see the
different scenarios, where we need to use the wrapper classes.

recursion:
in java is a process in which a method calls itself continuously. A method in java
that calls itself is called recursive method.
It makes the code compact but complex to understand.

Method
a method is a way to perform some task. Similarly, the method in Java is a
collection of instructions that performs a specific task. It provides the
reusability of code. We can also easily modify code using methods. In this section,
we will learn what is a method in Java, types of methods, method declaration, and
how to call a method in Java.

Every time an object is created using the new() keyword, at least one constructor
is called.
There are two types of constructors in Java: no-arg constructor, and parameterized
constructor
Default constructor (no-arg constructor)
Parameterized constructor

Default Constructor :
A constructor is called "Default Constructor" when it doesn't have any parameter.

Parameterized Constructor :
A constructor which has a specific number of parameters is called a parameterized
constructor

Call by Value and Call by Reference in Java :

There is only call by value in java, not call by reference. If we call a method
passing a value, it is known as call by value. The changes being done in the called
method, is not affected in the calling method.

String :
is a sequence of characters. But in Java, string is an object that represents a
sequence of characters. The java.lang.String class is used to create a string
object.

How to create a string object?


There are two ways to create String object:

By string literal
By new keyword
1) String Literal
Java String literal is created by using double quotes. For Example:

String s="welcome";

By new keyword
String s=new String("Welcome");//creates two objects and one reference variable

immutable string :
A String is an unavoidable type of variable while writing any application program.
String references are used to store various attributes like username, password,
etc. In Java, String objects are immutable. Immutable simply means unmodifiable or
unchangeable.
Once String object is created its data or state can't be changed but a new String
object is created.

substring :
A part of String is called substring. In other words, substring is a subset of
another String. Java String class provides the built-in substring() method that
extract a substring from the given string by using the index values passed as an
argument. In case of substring() method startIndex is inclusive and endIndex is
exclusive.

StringBuffer:
StringBuffer class is used to create mutable (modifiable) String objects. The
StringBuffer class in Java is the same as String class except it is mutable i.e. it
can be changed.

StringBuilder:
class is used to create mutable (modifiable) String. The Java StringBuilder class
is same as StringBuffer class except that it is non-synchronized. It is available
since JDK 1.5.

StringBuffer
StringBuilder
1) StringBuffer is synchronized i.e. thread safe. It means two threads can't
call the methods of StringBuffer simultaneously. StringBuilder is non-
synchronized i.e. not thread
safe. It means two threads can call the methods of StringBuilder simultaneously.
2) StringBuffer is less efficient than StringBuilder.
StringBuilder is more efficient than StringBuffer.
3) StringBuffer was introduced in Java 1.0
StringBuilder was introduced in Java 1.5

Multithreading:
in Java is a process of executing multiple threads simultaneously.

A thread is a lightweight sub-process, the smallest unit of processing.


Multiprocessing and multithreading, both are used to achieve multitasking.

However, we use multithreading than multiprocessing because threads use a shared


memory area. They don't allocate separate memory area so saves memory, and context-
switching between the threads takes less time than process.

Java Multithreading is mostly used in games, animation, etc.


Advantages of Java Multithreading
1) It doesn't block the user because threads are independent and you can perform
multiple operations at the same time.

2) You can perform many operations together, so it saves time.

3) Threads are independent, so it doesn't affect other threads if an exception


occurs in a single thread.

Multitasking:

Multitasking is a process of executing multiple tasks simultaneously. We use


multitasking to utilize the CPU. Multitasking can be achieved in two ways

Process-based Multitasking (Multiprocessing)


Thread-based Multitasking (Multithreading)

1) Process-based Multitasking (Multiprocessing)


Each process has an address in memory. In other words, each process allocates a
separate memory area.
A process is heavyweight.
Cost of communication between the process is high.
Switching from one process to another requires some time for saving and loading
registers, memory maps, updating lists, etc.

2) Thread-based Multitasking (Multithreading)


Threads share the same address space.
A thread is lightweight.
Cost of communication between the thread is low.

Exception Handling:
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.

Java try block


Java try block is used to enclose the code that might throw an exception. It must
be used within the method.
If an exception occurs at the particular statement in the try block, the rest of
the block code will not execute. So, it is recommended not to keep the code in try
block that will not throw an exception.
Java try block must be followed by either catch or finally block

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. However, the good approach is to
declare the generated type of exception.
The catch block must be used after the try block only. You can use multiple catch
block with a single try block.

Java Garbage Collection:


In java, garbage means unreferenced objects.
Garbage Collection is process of reclaiming the runtime unused memory
automatically. In other words, it is a way to destroy the unused objects.
To do so, we were using free() function in C language and delete() in C++. But, in
java it is performed automatically. So, java provides better memory management.

Advantage of Garbage Collection


It makes java memory efficient because garbage collector removes the unreferenced
objects from heap memory.
It is automatically done by the garbage collector(a part of JVM) so we don't need
to make extra efforts.

Java Arrays
Normally, an array is a collection of similar type of elements which has contiguous
memory location.
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.

There are two types of array.

Single Dimensional Array


Multidimensional Array

You might also like