Hashmap And Hashtable

Hashmap And Hashtable

Guys, One of the important concepts that you should know in java is the Hashmap and the Hashtable. This is one of the important questions raised in majority of the interviews. You should know to explain them and you should know to spot the difference between Hashmap and Hashtable. You can get an idea on this concept below.

Hashmap and Hashtable both comes under java collections. Both provide key values in order to access the data. Hashtable comes under original collection class in java whereas hashmap comes under collection framework part. We can access the data in the hashtable in synchronized manner whereas it is not possible to access data in synchronized manner in hashmap. Iterator in hashmap is fail safe. Hashmap allows NULL values and one NULL key whereas hashtable does not allow NULL values. HashMap is good for non threaded applications.

Hashmap

import java.util.HashMap;
public class hashmap
{
public static void main (String [] args)
{
HashMap<Integer, String> om = new HashMap<Integer, String> ();
om.put (new Integer (1), "insurance");
om.put (new Integer (2), "bank");
System.out.println ("Displaying Elements:" + om);
}
}
Hashtable

import java.util.Hashtable;
public class Hashtable
{
public static void main (String [] args)
{
Hashtable hasht = new Hashtable ();
hasht.put ("insurance", new Integer (1));
hasht.put ("bank", new Integer (2));
Object ob = hasht.get ("insurance");
System.out.println (ob);
}

Inheritance in Java

Inheritance in Java

Guys,

Welcome you all to this page. Inheritance is one of the important concepts in java what all software professionals and all computer science students should be strong enough to code in. Inheritance in Java is also an important question asked in majority of the interviews and in exams as well. Since guys find hard and quiet confusing in this concept of object oriented programming studies, I described the easiest way on the exact concept with examples in this page. Let me share my knowledge on this with you.

Inheritance means one class acquiring the properties from another class. A class which is derived from other class is a subclass or derived class or child class and the class from which the child class is derived is called as a super class or the base class or the parent class. Inheritance is an important concept in object oriented programming language. There are many types of inheritances in object oriented design. They are single inheritance, multiple inheritance, multilevel inheritance and hierarchical inheritance. But here we are going to discuss about two types namely the single inheritance and the multilevel inheritance. Also an important concept in java what you should know is java does not support multiple inheritance. Because, when a class inherits from more than one class, it will lead to an inheritance path ambiguity problem which is commonly called as a diamond problem. But the concept of multiple inheritances can be achieved in java by using the concept of interfaces. Interfaces are friendly and are advanced which replaces the concept of multiple inheritances in java. Two important concepts of inheritances namely single inheritance and multilevel inheritance are explained in detail below. Please have a look. 


Single Inheritance 

The concept of single inheritance is illustrated below. I.e. one class is derived from another class. I.e. one class acquiring the properties from another class. Here insurance is the parent class and trading is the child class. Child class extends the parent class I.e. inherits with the parent class using the keyword extends as shown in the below example.

Class Class in Java Examples or Inheritance Examples

class insurance

{
int cash;
int deposit_id;
int substitute (int a, int b)
{
cash=a;
deposit_id=b;
return (0);
}
void display ()
{
System.out.println ("Please claim the insurance");
}
}
class trading extends insurance
{
public static void main (String args [])
{
insurance ins = new insurance ();
ins.substitute (5000,123);
ins.display ();
}
void screen ()
{
System.out.println ("The insurance amount deposited is valid");
}
}

Multilevel Inheritance

When a child class is derived from a derived class, then it is known as multilevel inheritance.

class insurance
{
//implementations
}
class trading extends insurance
{
//implementations
}
class bank extends trading
{
//implementations
}

Difference Abstract Class and Interface

Difference between Abstract Class and Interface 

Guys, Difference between Abstract Class and Interface is one of the confusing topic for beginners in object oriented programming language. The difference between the two concepts are spotted simple in this page. You can have a look and make it clear.

The main difference between Abstract Class and Interface is, Interface do not have any implementations.
They contain only the method name. I.e. the signature part. The body implementation part is done in the class in which it is implemented. This is the reasons why methods of an interface are called as implicitly abstract.  A class which is implementing an interface has to implement all the methods of the interface.
Interface is not a class. Interface is an entity.
Interface forms the contract between the class and the outside world.
Interface should be implemented in another class using the keyword implements.
Interface cannot be instantiated, but we can instantiate a class that implements the interface.
Java does not support multiple inheritances.So the concept of multiple inheritances can be achieved by using interface in java.
Also many interface can be implemented in a class using the keyword implements with the interface names separated with commas.

An abstract class may contain complete methods or incomplete methods.
An abstract method declared in abstract class should be sure used in the class in which it is extended. A method with abstract keyword should be implemented in the class in which it is extended. All the abstract methods should be implemented in the class in which it is extended. A method without abstract keyword in abstract class should be implemented right there itself.
A class may inherit only one abstract class.
An abstract class can contain fields, constructors, etc…
Abstract classes should be extended with another class or abstract class using the keyword extends.
Abstract classes are faster than interface.

What is an Abstract Class

What is an Abstract Class ?

Abstract Class is one of the important concepts in java what all software professionals and all computer science students should be strong enough to code in. Abstract Class in Java is also an important question asked in majority of the interviews and in exams as well. Since guys find hard and quiet confusing in this concept of object oriented programming studies, I described the easiest way on the exact concept with examples in this page. Let me share my knowledge on this with you.

An abstract class may contain complete methods or incomplete methods. A method with abstract keyword should be implemented in the class in which it is extended. All the abstract methods should be implemented in the class in which it is extended. A method without abstract keyword in abstract class should be implemented right there itself as shown in the example.  A class may inherit only one abstract class. An abstract class can contain fields, constructors, etc… Abstract classes should be extended with another class or abstract class using the keyword extends. Abstract classes are faster than interface. You can have a look at the example described below.

Abstract class in Java with Example.

abstract class insurance
{
int a, b;
void plan (int aa, int bb)
{
//body implementations
}
abstract void trading ();
abstract void Citibank ();
}

Note: Each subclass of insurance which is not abstract such as bank and SBI must sure provide implementations for the trading and Citibank methods.

class bank extends insurance

{
void trading ()
{
//body implementations
}
void Citibank()
{
//body implementations
}
//Its your choice to make use of "void plan (int aa, int bb)" method here. Since it is not abstract it may or may not be used.
}
class SBI extends insurance
{
void trading ()
{
//body implementations
}
void Citibank ()
{
//body implementations
}
}

You should create an instance for class bank and for class SBI.

Abstract Class

abstract class insurance
{
abstract void Citibank ();
void Lifeinsurance ()
{
System.out.println ("Life Insurance plans are good always");
}
}
class trading extends insurance
{
void Citibank ()
{
System.out.println ("It’s better to create a Citibank account");
}
}
class bank
{
public static void main (String args[])
{
trading trade = new trading();
trade.Citibank ();
trade.Lifeinsurance ();
}
}
Abstract Class Extends Abstract Class
 abstract class a{
public abstract void insurance ();
public abstract void trading ();
}
abstract class b extends a
{
public void insurance ()
{
System.out.println ("Insurance is claimed");
}
}

Interface in Java

Interface in Java

Interface in Java is one of the important concepts what all software professionals and all computer science students should be strong enough to code in. Interface in Java is also an important question asked in majority of the interviews and in exams as well. Since guys find hard and quiet confusing in this concept of object oriented programming studies, I described the easiest way on the exact concept with examples in this page. Let me share my knowledge on this with you.

The main difference between Interface and Abstract Class in Java is methods of a Java interface are implicitly abstract. Interface don’t have any implementations. Interface contain only the signature alone without body implementation. The body implementation part is done in the class in which it is implemented. A class which is implementing an interface has to implement all the methods of the interface. Interface is not a class. Interface is an entity. Interface forms the contract between the class and the outside world. Interface should be implemented in another class using the keyword implements. Interface cannot be instantiated, but we can instantiate a class that implements the interface. Java does not support multiple inheritances. So the concept of multiple inheritances can be achieved by using interface in java. Also many interface can be implemented in a class using the keyword implements with the interface names separated with commas. It is also illustrated with an example in this page.


Interface in Java

interface insurance
{
public void account ();
}

class bank implements insurance
{
public void account ()
{
System.out.println ("It’s easy to create a bank account");
}
}

public class banking
{
public static void main (String [] args)
{
insurance trading=new bank ();
trading.account ();
}
}

For multiple interface in java please visit the below link.


Thanks...
My Profile