Friday, 11 November 2016

Association ,Composition and Aggregation

Association :

Association is a relationship between two objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.

Example: A Student and a Faculty are having an association.



Composition:

Without existing container object if there is no chance of existing contained objects then the relationship between container object and contained object is called composition which is a strong association.
Example: In examination a single question can have multiple answer and answer cannot belong to multiple questions whenever questions object destroys automatically all the answer objects will be destroyed that is without existing question object there is no chance of existing dependent object hence these are strongly associated and this relationship is called composition.

Aggregation :

Without existing container object if there is a chance of existing contained objects such type of relationship is called aggregation. In aggregation objects have weak association.It is also called a HAS-A relationship.
Example:Within a department there may be a chance of several professors will work whenever we are closing department still there may be a chance of existing professor object without existing department object the relationship between department and professor is called aggregation where the objects having weak association.


Monday, 31 October 2016

Polymorphism

Polymorphism means taking many forms, where ‘poly’ means many and ‘morph’ means forms. It is the ability to allows us to perform a single action in different ways. Polymorphism in Java is of two types: 

  • Compile time polymorphism( or Static Polymorphism)
  • Run time polymorphism (or Dynamic Polymorphism)
We can perform polymorphism in java by :
  1. Method overloading (Compile time polymorphism)
  2. Method overriding (Runtime polymorphism)


Tuesday, 6 September 2016

Environment Setup

Set the Path :

1.Go on my computer icon and right click, after that click on properties option.

2.Now click on Advance Settings



3.Click on advance option



4.Click on Environment Variable.



5.Click on new button



6.Now one dialog box is appear, now ignore this but don't close




7.Now open my computer open c:/ > Programs Files > Java > jdk1.8.0_144 > bin copy this path



C:\Program Files\Java\jdk1.8.0_144\bin

Now come back on previous open dilogbox and write variable name 'path' and for variable value paste all copied path upto the bin folder. Put .; at the end. It (.) selects all the tools from the bin folder.




Thursday, 11 August 2016

What are different ways of iterating over keys, values and entry in Map?

What are different ways of iterating over keys, values and entry in Map?

Answer: 
Create and put key-value pairs in HashMap

package com.tutorial4u;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapDemo {
     public static void main(String[] args) {
           Map hashMap = new HashMap();
           hashMap.put(11, "CSS");
           hashMap.put(21, "IT");
           hashMap.put(31, "ECE");
           hashMap.put(41, "EEE");
     }
    
}

Iterate over keys :-

hashMap.keySet().iterator() method returns iterator to iterate over keys in HashMap.



Iterator keyIterator =hashMap.keySet().iterator();
           while(keyIterator.hasNext()){
           System.out.println(keyIterator.next());
       }

/*OUTPUT

21
41
11

31
*/
Iterate over values :-


hashMap.values().iterator() method returns iterator to iterate over keys in HashMap.


Iterator valueIterator=hashMap.values().iterator();
           while(valueIterator.hasNext()){
           System.out.println(valueIterator.next());
       }

/*OUTPUT

IT
EEE
CSS

ECE
*/

Iterate over Entry-


hashMap.entrySet().iterator() method returns iterator to iterate over keys in HashMap.

Iterator entryIterator=hashMap.entrySet().iterator();
           while(entryIterator.hasNext()){
           System.out.println(entryIterator.next());
       }

/*OUTPUT
21=IT
41=EEE
11=CSS
31=ECE

*/

Friday, 8 July 2016

Encapsulation

Encapsulation is a mechanism where you bind your data and code together as a single unit. It also means to hide your data in order to make it safe from any modification. 
Real Time Example :
take a example of news channel. Here the journalist finds the information and he only knows from where he had sourced this information.So it's source is protected inside their workplace.And the public only knows about the news. And the source is hidden.

We can achieve encapsulation in Java by: 

Declaring the variables of a class as private. Providing public setter and getter methods to modify and view the variables values.
Example :
               
package com.tutorial4u;

class Employee{
private int empNo;
private String empName;
private int salary;
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}

}
public class EncapsulationDemo{
public static void main(String[] args) {
      Employee emp = new Employee();
      emp.setEmpNo(101);
      emp.setEmpName("Ashish Kumar");
      emp.setSalary(1000);
      System.out.println("Employee No : "+emp.getEmpNo());
      System.out.println("Employee Name is : "+emp.getEmpName());
      System.out.println("Employee salary is : "+emp.getSalary());
}
}

Output :

Employee No : 101
Employee Name is : Ashish Kumar
Employee salary is : 1000

   
        
The main advantages of encapsulation are:
  1. We can achieve security.
  2. Enhancement will become very easy.
  3. It improves maintainability of the application. 
  4. It provides flexibility to the user to use system very easily.

Disadvantage :

The main disadvantage of encapsulation is it increases length of the code and slows down execution.

Wednesday, 6 April 2016

How many ways create an Object In Java?

Ques : How many ways create an Object In Java?

In interview asked how to create an object in java generally and everyone says
using the “new” operator to create an Object of a Class. But is it the only way to create an Object?


Simple answers is NO, then in how many ways we can create Object of a Class. 

There are four different ways to create objects in java:
  • Using New keyword
  • Class.forName()
  • Using Clone()
  • Using Deserilization

1. Using new keyword:

This is the most common way to create an object in java. Almost 99% of objects are created in this way.



MyObject object = new Object();

2. Using Class.forName():

If we know the name of the class & if it has a public default constructor we can create an object in this way.

 


MyObject obj = (MyObject) class.forName("object").newInstance();



3. Using clone():

The clone() can be used to create a copy of an existing object.

             
  MyObject obj new MyObject();

  MyObject object = (MyObject )obj.clone();

4. Using Object Deserialization :

Object deserialization is nothing but creating an object from its serialized form.




ObjectInputStream istream = new objectInputStream(some data);
MyObject object=(MyObject) instream.readObject();

Tuesday, 5 April 2016

Inheritance :


Inheritance is one such concept where the properties of one class can be inherited by the other. It helps to reuse the code and establish a relationship between different classes.

In Java, there are two classes:
  • Parent class ( Super or Base class)
  • Child class (Subclass or Derived class )
A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is known as Parent class.

Inheritance defines IS-A relationship between a Super class and its Sub class. extends and implements keywords are used to describe inheritance in Java.


Inheritance is further classified into 4 types:
  • Single Inheritance 
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
Note : Multiple inheritance is not supported in java

1.Single Inheritance :




2.Multilevel Inheritance:



3.Multiple Inheritance : 

Having more than one Parent class at the same level is called multiple inheritance.

  • Any class can extends only one class at a time and can’t extends more than one class simultaneously hence java won’t provide support for multiple inheritance.
  • But an interface can extends any number of interfaces at a time hence java provides multiple inheritance support through interfaces.

4.Hierarchical Inheritance :



5.Hybrid Inheritance : 



Tuesday, 22 March 2016

Difference between Hash Set and Hash Map

Ques : What is Difference between Hash Set and Hash Map?

                        Hash Map                       
 Hash Set
HashMap  is a implementation of Map interface
HashSet is an implementation of Set Interface
HashMap Stores data in form of key value pair
HashSet Store only objects
Put method is used to add element in map
Add method is used to add element is Set
In hash map hashcode value is calculated using key object
Here member object is used for calculating hashcode value which can be same for two objects so equal () method is used to check for equality if it returns false that means two objects are different.
HashMap is faster than hashset because unique key is used to access object
HashSet is slower than Hashmap

Wednesday, 16 March 2016

Remove white space from String

package com.tutorial4u;

import java.util.Scanner;

public class RemoveWwhiteSpace {
       public static void main(String[] args) {
             System.out.println("Enter any string : ");
             Scanner sc = new Scanner(System.in);
             String str = sc.nextLine();
             char[] c = str.toCharArray();
             StringBuffer sb = new StringBuffer();
             for(int i=0; i<c.length; i++){
                    if((c[i] !=' ')&& (c[i] !='\t')){
                           sb.append(c[i]);
                    }
             }
             System.out.println(sb);
       }


}

Output:

Enter any string :
Welcome To Tutorial4u
WelcomeToTutorial4u

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...