Wednesday, October 26, 2016

Java Abstract with Example

We already know that a class is a model for creating the objects. A class contains variables and methods of its objects. There is a rule that any thing written in the class,then that object applicable to all of its objects.For Example,write a method calculate() in the Demo class. If we create two objects to this class,then two objects get the copy of this method,hence from any object we can call and use this method.


Example:

class Demo

{
//method to calculate square value

void calculate(double x)
{
System.out.println("square="+(x*x));
}
}

class Test
{
public static void main(String args[])

{
//create two objects
Demo d1=new Demo();
Demo d2=new Demo();
//call calculate method from the objects
d1.calculate(4);
d2.calculate(5);
}
}
Out Put:

Square=16
square=25

In the above program the requirements of all the objects is same that means calculate square value. But sometimes the requirement of the object will be different.For example the first object want to calculate square value,second object wants the cube value. Then how to write the calculate() method in Demo class.

For this we have to fallow the steps:
  • First write a calculate() method in Demo class, so every object wants to calculate something.
  • If you implement body for calculate() method then it is commonly available to all the objects,so do not write  body for calculate() method. These type of methods are called abstract method.
  • Now derive a sub class square from Demo class,so that the calculate() method available to the sub class.similarly  we create another subclass with name cube where we write the calculate() method.
  • It is possible to create objects for the sub class.Using these objects the corresponding methods will be called.
Abstract method:

An abstract method does not contain any body. Abstract method should have only declaration but not implementation. Hence,Abstract method declaration should end with semicolon(;). The main purpose of abstract method is when the same method has to perform different tasks depending on the object calling it.

Example:

public abstract void m1();//correct declaration
public abstract void m1(){}//wrong one

If you declare abstract method in the parent class,then child is responsible to provide implementation for that abstract methods.

Abstract class:

An abstract class is a class that contains abstract methods. In abstract class contains both abstract methods(without body) and concrete(with body) methods but interface contains 100% abstract methods. Both abstract class and abstract method should be declared by using keyword 'abstract'.



Example:
Now will take The above example where the abstract class Demo has one abstract method which has got various implementations in sub classes.
abstract class Demo
{
//this is abstract method
abstract void calculate(double x);
}
class One extends Demo
{
//calculate square value
void calculate(double x)
{
System.out.println("square="+(x*x));
}
}
class Two extends Demo
{
//calculate cube value
void calculate(double x)
{
System.out.println("cube="+(x*x*x));
}
}
class Test 
{
public static void main(String args[])
{
//create sub class objects
One o=new One();
Two t=new Two();
//call and use calculate()method
o.calculate(4);
t.calculate(5);
}
}
Output:
square value=16
cube=125

Example 2:

 
abstract class Vehicle
{
public abstract int getNoOfWheels();
}
class Bus extends Vehicle
{
public int getNoOfWheels()
{
System.out.println("no of Bus wheels:"+7);
return 7;
}
}
class Car extends Vehicle
{
public int getNoOfWheels()
{
System.out.println("no of car wheels:"+4);
return 4;
}
}
class Test
{
public static void main(String args[])
{
Bus b=new Bus();
b.getNoOfWheels();
Car c=new Car();
c.getNoOfWheels();
}
}
Out put:
no of Bus wheels: 7
no of car wheels : 4

I hope you people understand this concept clearly and share this in social websites and give comments below about on this topic.

 




No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...