Posts

How Method Overloading Helping in Java - Part 9

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. Where have u observed method overloading helped us in java? Where have you seen in java default classes using method overloading? We need method overloading because if we have multiple arguments for add method and we have to create multiple methods in that case we will be confused which method to call to perform specific addition. Example: class   PrintOverloading1 {      public   void  show( char  c)     {           System . out .println(c);     }      public   void  show( char  c,  int  number)      {           System . out .println(c +  " " + number);     }    public ...

Access Modifiers in java

Access Modifiers in Java Access to classes is controlled using Access Modifiers aka ( Access Specifiers, Visibility Specifiers ) in java. These are used to regulate access to classes, fields and methods in java. Using access modifiers you can control the access of variables, methods with in a class. Java provides number of access modifiers to control the access for class as well as the fields, methods and constructors in your classes. We have 4 types of access modifiers in java which are explained below:- Default: Visible to the package, the default. No modifiers are needed, no keyword is required. Private: Visible to the class only. Public: Visible to the world. Protected: Visible to the package and all subclasses. Default: We do not have any default specifier in Java. It provides Default specifier which is used where no access modifier is present. Using this you can access class, methods or fields which belongs to same package, but not from outside ...

Important Java Interview Questions - Part 7

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. Where have u used abstraction and encapsulation in your project? We use encapsulation everywhere in java like when we create a class in java we are using encapsulation. Abstraction we use when we create abstract class or interface using abstraction in java. " Encapsulation  is to hide the variables or something inside a class, preventing unauthorized parties to use. So the public methods like getter and setter access it and the other classes call these methods for accessing" Using Encapsulation we achieve  “Data Hiding, Information Hiding”  in java. Encapsulation binds data (attributes and methods) and the code in the form of a class. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. Encapsulation provides “ access control ” through which we can con...

Important Java Interview Questions - Part 5

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. What is difference between Linkedlist & Arraylist? LinkedList is an implementation of List interface in java. The LinkedList Java class is a concrete implementation of this abstract concept. LinkedList is based on double linked List data structure.  LinkedList<E>  allows for constant-time insertions or removals  using iterators , but only sequential access of elements and maintains insertion order. LinkedList is non synchronized. ArrayList is an implementation of List interface in java. ArrayList is based on an Array data structure. ArrayList implements it with a dynamically re-sizing array and maintains insertion order. It is non synchronized. ArrayList LinkedList 1) ArrayList internally uses dynamic array to store the elements. LinkedList internally uses doubly linked list to st...

Important Java Interview Questions - Part 6

I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. Why linkedlist is good for insertion in java? A LinkedList looks like a chain, consisting of people who hold each other's hand. You can insert people or node into that chain or remove.  LinkedList is actually very flexible extension of an ArrayList, For example if you need to add new element to the end of an ArrayList you have to look at the size of the collection and then add that new element at  n+1  place. In LinkedList you do it directly by  addLast(E e)  method. In a LinkedList there are three parts. First part stores the pointer to previous element, second stores the data & third stores pointer to next element. So when insertion & deletion happens in linkedlist these pointers get updated. Thats why linkedlist is fast.  Whereas, ArrayList internally uses array & when you ...

Aggregation and Composition in Java

Image
The distinction between aggregation and composition depends on context. The composition is stronger than Aggregation. In Short, a relationship between two objects is referred as an association, and an association is known as composition when one object  owns other while an association is known as aggregation when one object uses another object. In the case of Composition, One object is  OWNER  of another object, while in the case of aggregation one object is just a  USER  or another object. 1. Composition is a  strong  Association whereas Aggregation is a  weak  Association. 2. Aggregation relation is  “has-a”  and composition is  “part-of”  relation. Take the example of Team in which Player can stand “on its own” so may not be in composition with a Team. Player which is a part of Team can exist without a team and can become part of other team as well. Just like Car example, it is tru...

Important Java Interview Questions - Part 4

Image
I'm sharing my experience with you as these are the questions which have been asked to me while interviewing on java profile. What is the impelementation of linkedlist in java? Singly Linked Lists are a type of data structure. It is a type of List. In a singly linked list each node in the list stores the contents of the node and a pointer or reference to the next node in the list. Here is the pictorial view of singly linked list: A doubly-linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. This structure allows for efficient insertion or removal of elements from any position in the sequence. In a doubly linked list each node has two links one pointing to the next node in the list and one pointing to the previous node in the list. Here is the pictorial view of doubly l...