Java Qutns: 1.what Are The Principle Concepts of OOPS?
Java Qutns: 1.what Are The Principle Concepts of OOPS?
There are four principle concepts upon which object oriented design and
programming rest. They are:
Abstraction
Polymorphism
Inheritance
Encapsulation
2.What is Abstraction?
Abstraction refers to the act of representing essential features without
including the background details or explanations.
3.What is Encapsulation?
5.What is Inheritance?
Inheritance is the process by which objects of one class acquire the
properties of objects of another class.
o
o
In some cases, multiple methods have the same name, but different formal
argument lists (overloaded methods).
In other cases, multiple methods have the same name, same return type,
and same formal argument list (overridden methods).
8.Explain the different forms of Polymorphism.
There are two types of polymorphism one is Compile time polymorphism and the
other is run time polymorphism. Compile time polymorphism is method
overloading. Runtime time polymorphism is done using inheritance and
interface.
Note: From a practical programming viewpoint, polymorphism manifests itself
in three distinct forms in Java:
Method overloading
13.What are the differences between method overloading and method overriding?
Overloaded Method
Overridden Method
Arguments
Must change
Return type
Can change
Exceptions
Can change
Access
Can change
Invocation
super.overriddenMethod();
17.What is super?
super is a keyword which is used to access the method or member variables from
the superclass. If a method hides one of the member variables in its
superclass, the method can refer to the hidden variable through the use of the
super keyword. In the same way, if a method overrides one of the methods in
its superclass, the method can invoke the overridden method through the use of
the super keyword.
Note:
You can only go back one level.
In the constructor, if you use super(), it must be the very first code,
and you cannot access any this.xxxvariables or methods to compute its
parameters.
18.How do you prevent a method from being overridden?
To prevent a specific method from being overridden in a subclass, use the
final modifier on the method declaration, which means "this is the final
implementation of this method", the end of its inheritance hierarchy.
Method statements
}
19.What is an Interface?
An interface is a description of a set of methods that conforming implementing
classes must have.
Note:
Abstract Class
Interfaces
28.When should I use abstract classes and when should I use interfaces?
Use Interfaces when
you need some classes to use some methods which you don't want to be
included in the class, then you go for the interface, which makes it easy to
just implement and make use of the methods defined in the interface.
Use Abstract Class when
If various implementations are of the same kind and use common behavior
or status then abstract class is better to use.
When you want to provide a generalized form of abstraction and leave the
implementation task with the inheriting subclass.
They do not have return types, not even void and therefore they cannot
return values.
They cannot be inherited, though a derived class can call the base class
constructor.
Methods
Purpose
Modifiers
Return Type
Name
this
super
Inheritance
Class Methods
Instance Methods
Protected- protected methods and fields can only be accessed within the
same class to which the methods and fields belong, within its subclasses, and
within classes of the same package.
Private- private methods and fields can only be accessed within the same
class to which the methods and fields belong. private methods and fields are
not visible within subclasses and are not inherited by subclasses.
Situation
public
protected
default
private
Accessible to class
from same package?
yes
yes
yes
no
Accessible to class
from different package?
yes
no
no
The final modifier keyword makes that the programmer cannot change the value
anymore. The actual meaning depends on whether it is applied to a class, a
variable, or a method.
Set up a loop that makes a call to hasNext(). Have the loop iterate as
long as hasNext() returns true.
Iterator
50.How is ListIterator?
ListIterator is just like Iterator, except it allows us to access the
collection in either the forward or backward direction and lets us modify an
element
51.What is the List interface?
The List interface provides support for ordered collections of objects.
Lists may contain duplicate elements.
52.What are the main implementations of the List interface ?
The main implementations of the List interface are as follows :
Vector
ArrayList internally uses and array to store the elements, when that
array gets filled by inserting elements a new array of roughly 1.5 times the
size of the original array is created and all the data of old array is copied
to new array.
During deletion, all elements present in the array after the deleted
elements have to be moved one step back to fill the space created by deletion.
In linked list data is stored in nodes that have reference to the previous
node and the next node so adding element is simple as creating the node an
updating the next pointer on the last node and the previous pointer on the new
node. Deletion in linked list is fast because it involves only updating the
next pointer in the node before the deleted node and updating the previous
pointer in the node after the deleted node.
57.Why are Iterators returned by ArrayList called Fail Fast ?
Because, if list is structurally modified at any time after the iterator is
created, in any way except through the iterator's own remove or add methods,
the iterator will throw a ConcurrentModificationException. Thus, in the face
of concurrent modification, the iterator fails quickly and cleanly, rather
than risking arbitrary, non-deterministic behavior at an undetermined time in
the future.
58.How do you decide when to use ArrayList and When to use LinkedList?
If you need to support random access, without inserting or removing elements
from any place other than the end, then ArrayList offers the optimal
collection. If, however, you need to frequently add and remove elements from
the middle of the list and only access the list elements sequentially, then
LinkedList offers the better implementation.
59.What is the Set interface ?
The Set interface provides methods for accessing the elements of a
finite mathematical set
Sets do not allow duplicate elements
Two Set objects are equal if they contain the same elements
EnumSet
61.What is a HashSet ?
A HashSet is an unsorted, unordered Set.
It uses the hashcode of the object being inserted (so the more efficient
your hashcode() implementation the better access performance youll get).
Use this class when you want a collection with no duplicates and you
dont care about order when you iterate through it.
62.What is a TreeSet ?
TreeSet is a Set implementation that keeps the elements in sorted order. The
elements are sorted according to the natural order of elements or by the
comparator provided at creation time.
63.What is an EnumSet ?
An EnumSet is a specialized set for use with enum types, all of the elements
in the EnumSet type that is specified, explicitly or implicitly, when the set
is created.
64.Difference between HashSet and TreeSet ?
HashSet
TreeSet
65.What is a Map ?
A map is an object that stores associations between keys and values
(key/value pairs).
Given a key, you can find its value. Both keys and values are objects.
Some maps can accept a null key and null values, others cannot.
66.What are the main Implementations of the Map interface ?
The main implementations of the List interface are as follows:
HashMap
HashTable
TreeMap
EnumMap
67.What is a TreeMap ?
TreeMap actually implements the SortedMap interface which extends the Map
interface. In a TreeMap the data will be sorted in ascending order of keys
according to the natural order for the key's class, or by the comparator
provided at creation time. TreeMap is based on the Red-Black tree data
structure.
68.How do you decide when to use HashMap and when to use TreeMap ?
For inserting, deleting, and locating elements in a Map, the HashMap offers
the best alternative. If, however, you need to traverse the keys in a sorted
order, then TreeMap is your better alternative. Depending upon the size of
your collection, it may be faster to add elements to a HashMap, then convert
the map to a TreeMap for sorted key traversal.
Hashtable
HashMap is unsynchronized.
Hashtable is synchronized.
Note: Only one NULL is allowed as a key in HashMap. HashMap does not allow
multiple keys to be NULL. Nevertheless, it can have multiple NULL values.
70.How does a Hashtable internally maintain the key-value pairs?
TreeMap actually implements the SortedMap interface which extends the Map
interface. In a TreeMap the data will be sorted in ascending order of keys
according to the natural order for the key's class, or by the comparator
provided at creation time. TreeMap is based on the Red-Black tree data
structure.
71.What Are the different
Collection Views That Maps
Provide?
Maps Provide Three Collection Views.
interface Comparable<T>
where T is the name of the type parameter.
All classes implementing the Comparable interface must implement
the compareTo() method that has the return type as an integer. The signature
of the compareTo() method is as follows:
int i = object1.compareTo(object2)
If object1 < object2: The value of i returned will be negative.
Comparato
t uses the compare() method.
int compare(ObjOne, ObjTwo)