You are on page 1of 3

OOP concept

Object Oriented programming is a programming style which is associated with the concepts like class,
object, Inheritance, Encapsulation, Abstraction, Polymorphism.

Class vs Object
A class is a template for objects. A class defines object properties including a canid range of values, and a
default value. A class also describes object behavior. An object is a member or an “instance” of a class. An
object has a state in which all of its properties have values that you either explicitly define ir that are defined
by default settings.

Method vs constructor
Constructors is used to initialize an object whereas method is used to exhibits functionality of an object.

Constructor does not return any value whereas method may or may not return a value.

In case constructor is not present, a default constructor is provided by java compiler. In the case of a
method, no default method is provided.

Constructor should be the same name as that of class. Method name should not be the same name as that
of class.

Override vs Overload

Method Overloading Method Overriding

Provides functionality to reuse a method name with Used to override a behavior which the class has
different parameters. inherited from the parent class.
Usually in a single class but may also be used in a Always in two classes that have a child-parent or
child class. IS-A relationship.

Must have different parameters Must have the same parameters and same name

May have different return types Must have the same return type or covariant return
type (child class)

May have different access modifiers (private, Must NOT have a lower modifier but may have a
protected, public) higher modifier.

May throw different exceptions Must NOT throw a new or broader checker
exception.

ArrayList vs Vector
Synchronization: Vector is synchronized, which means only one thread at a time can access the code,
while arrayList is not synchronized, which means multiple threads can work on arrayList at the same time.

Performance: ArrayList is faster, since it is non-synchronized. If one thread works on a vector, it has
acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is
released.

Data Growth: ArrayList and Vector both grow and shrink dynamically to maintain optimal use of storage,
but the way they resize is different. ArrayList increments 50% of the current array size if the number of
elements exceeds its capacity, while vector increments 100%, essentially doubling the current array size.

Static vs Instance Variables


Static variables only have one copy that is shared by all the different objects of a class, whereas every
object has it’s own personal copy of an instance variable. So instance variables across different objects can
have different values whereas static variables across different objects can have only one value. If changes
are made to that variable, all other instances will see the effect of the change.

Interface VS Abstract Class


Type of methods: Interface can have only abstract methods. Abstract class can have abstract and non-
abstract methods.

Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has
only static and final variables.

Multiple Implementation: An interface can extend another Java Interface only, an abstract class can
extend another Java class and implement multiple Java interface.

Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class
can have class members like private, protected, etc.

Exception vs Error
An error indicates serious problems that a reasonable application should not try to catch. An exception
indicates conditions that a reasonable application might want to catch.

Recovering from Error is not possible. However, we can recover from exceptions by either using try-catch
block or throwing exceptions back to caller.

All errors in Java are unchecked type. Exceptions include both checked as well as unchecked type.

Errors are mostly caused by the environment in which program is running. However, program itself is
responsible for causing exceptions.

Errors occur at runtime and not known to the compiler. All exceptions occurs at runtime but checked
exceptions (such as IOException) are known to compiler while unchecked (such as
ArrayIndexOutOfBoundException, NullPointerException) are not.

How to handle exception


Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. Program
statements that you think can raise exceptions are contained within a try block. If an exception occurs
within the try block, it is thrown. Your code can catch this exception and handle it in some rational manner.
System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an
exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such
by a throws clause. Any code that absolutely must be executed after a try block completes is put in a finally
block.

Throw vs Throws
Throws is used to postpone the handling of a checked exception, the throws keyword appears at the end of
a method’s signature. Throw is used to invoke an exception explicitly.

Describe LinkedList in Java


LinkedList are linear data structures where the elements are not stored in contiguous locations and every
element is a separate object with a data part and address part. The elements are linked using pointers and
addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions,
they are preferred over the arrays. It also has few disadvantages like the nodes cannot be accessed directly
instead we need to start from the head and follow through the link to reach to a node we wish to access. To
store the elements in a linked list we use a doubly linked list which provides a linear data structure and also
used to inherit and abstract class and implement list and request interface.

collections in Java
A Collection is a group of individual objects represented as a single unit.Java provides Collection
Framework which defines several classes and interfaces to represent a group of objects as a single unit.
The Collection interface and Map interface are the two main “root” interface of Java collection classes.
Collection interfaces included Set, List, Queue. All classes, like ArrayList, LinkedList, Vector that implement
these interfaces have some common set of methods.

HashMap
HashMap is a part of Java’s collection, it provides the basic implementation of Map interface of Java. It
stores the data in (Key, Value) pairs. To access a value one must know its key. HashMap is known as
HashMap because it uses a technique called hashing. Hashing is a technique of converting a large String to
small String that represents same String. A shorter value helps in indexing and faster searches. Internally
HashMap contains an array of Node and a node is represented as a class which contains 4 fields: int hash,
K key, V value, Node next. HashMap doesn’t allow duplicate keys but allows duplicate values.

Methods in HashMap
void clear(): used to remove all mappings from a map.

boolean containsKey(Object key): Used to return True if for a specified key, mapping is present in the map.

boolean containsValue(Object value): Used to return True if one or mow key is mapped to a specified value.

Object clone(): Used to return a shallow copy of the mentioned hash map

boolean usEmpty(): Used to check whether the map is empty or not.

Object get(Object key): it is used to retrieve or fetch the value mapped by a particular key.

int size(): return the size of a map.

Object put(Object key, Object value): insert a particular mapping of a key-value pair into a map.

Object remove(Object key) remove the values for and particular key in the Map.

HashMap vs HashTable
HashMap allows one null key and multiple null values, but HashTable doesn’t allow any null key or value.

HashMap is non synchronized. It is not- thread safe and can’t be shared between many threads without
proper synchronization code, but HashTable is synchronized. It is thread-safe and can be shared with many
threads.

HashMap is generally preferred over HashTable if thread synchronization is not needed.

HashMap vs TreeMap
HashMap does not maintain any order neither based on key nor on basis of value. TreeMap maintained the
keys in a sorted order.

get/put/containsKey() operations in HashMap are O(1) in average case. However, in TreeMap, these
operations take O(logn) time.

HashMap and TreeMap are part of collection framework.

HashMap implements Map interface while TreeMap implements SortedMap interface. A Sorted Map
interface is a child of Map.

HashMap implements Hashing, while TreeMap implements Red-Black Tree.

How multithreading works in Java


Multithreading allows concurrent execution of two or more parts of a program for maximum utilization of
CPU. Each part of such program is called a thread.

Threads can be created by using two mechanisms:

Extending the Thread class. This class overrides the run() method available in the Thread class. A thread
begins its life inside run() method. We create an object of our new class and call start() method to start the
execution of a thread. Start() invokes the run() method on the Thread object.

Implementing the Runnable Interface. We create a new class which implements java.lang.Runnable
interface and override run() method. Then we instantiate a Thread object and call start() method on this
object.

You might also like