Abstract class,Interfaces and its implementations

Abstract class
We need abstract class for the sense of simplicity.Learn it this way,suppose there is a college administration with over 100 of persons working in different sections namely,Head of department,teachers of diff. subjects,accounts department staff,mess staff,cleaners etc.
Each have their own role in the college.But,here's the situation,what if the different department(classes) want the similar thing,for example,retrieving student info or getting the salary.We have to make method with similar motive,for each and every class we make,WHAT A WASTE!!!
Abstract class comes to the rescue. 
Abstract class is nothing but a normal class with some restrictions.Well,these restrictions are:


  1. At least one of the method/s must be abstract.
  2. You cannot make an object of an abstract class,i.e.,no new keyword before the class name.But the sub-classes of an abstract class is allowed.
  3. An abstract method MUST NOT HAVE A BODY.For example:void method(arguments);// there is no braces here{}.
Even with these limitations why so we need an abstract class??
You have heard "abstract" word,before....right.One of the first few pages of good book have an abstract covering the overall idea and a very brief description of the book.Same for the abstract class.We need not to define every method(i.e. abstract method).If the method is abstract it must not have a body(as described in above example).


Abstract class program:
abstract class Vehicle
{
abstract void accelerate();
abstract void brake();//both are abstract methods,remember no body
public void gears()
{
//concrete method statement to gears
}
}
class Harley extends Vehicle{
void accelerate()//defining the methods
{
//statement to accelerate harley 
}
void brake()
{
//statement to brake harley 
}
public static void main(String args[])
{
Harley h=new Harley();
h.accelerate();
h.gears();
h.brake();
}
}
Interfaces
The abstract class does not provide 100% abstraction,i.e.we can use concrete methods along with the abstract methods.Interfaces are similar to the abstract class,but there are differences:

  1. Interfaces are implemented not extended.Ex:class A implements C.
  2. Interfaces contains only static and final variables.
  3. Interface methods are abstract only(without body) unlike abstract class.
  4. A class implementing an interface must implement,i.e. define every method of the interface. 
  5. No constructor of interfaces is allowed.
Yess..its Harley davidson!!


Things are pretty much the same except the gears functionality..so why write from the scratch



You can thought of interface as a base of other class but itself it does not exist.
Interfaces is the only way to have multiple inheritance in java.
Interface program:
Interface Vehicle
{
int SPEED=70;//By default static and final
void accelerate();
void brake();//public by default
public void gears();
}
class Harley implements Vehicle{
void accelerate()//defining the methods
{
//statement to accelerate harley 
}
void brake()
{
//statement to brake harley 
}
public static void main(String args[])
{
Harley h=new Harley();
h.accelerate();
h.gears();
h.brake();
System.out.println(SPEED);//variable inside interface called
}
}

Comments

Popular posts from this blog

Four building blocks of Object oriented programming language

Exceptions and exceptional handling

Lists in java