Professional Documents
Culture Documents
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
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.
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.
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.
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
Object get(Object key): it is used to retrieve or fetch the value mapped by a particular key.
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 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 implements Map interface while TreeMap implements SortedMap interface. A Sorted Map
interface is a child of Map.
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.