Thursday, June 23, 2016

Abstract Method and Abstract Class Example Java

Abstract Method and Abstract Class 

An abstract method does not contain any body.It contains only the method header.So we can say it is an incomplete method.An abstract class is a class that generally contains ,some abstract methods.Both the abstract  class and the abstract methods should be declared by using the key word abstract.
Since, abstract class contains incomplete methods.it is not possible to estimate the total memory required to create the object.So JVM can not create objects to an abstract class.

Abstract Method: An abstract method is a method without method body .An abstract method is written when the same method has to perform different tasks depending on the object calling it.

Abstract Class: An abstract class is a class that contains 0 or more abstract methods.

Sample Program:

/**
 * 
 */
package com.interfaceabhi;

/**
 * @author Abhinaw.Tripathi
 *
 */
abstract class MyClass
{
  abstract void calculate(double x);
}

class C extends MyClass
{

@Override
void calculate(double x)
{
System.out.println("Suare root: "+ (x*x));
}
 
}

class Cube extends MyClass
{

@Override
void calculate(double x)
{
System.out.println("Cube root: "+ (x*x*x));
}
 
}

public class AbstractClassDemo
{
/**
* @param args
*/
public static void main(String[] args)
{
C c=new  C();
c.calculate(10);

Cube cube=new Cube();
cube.calculate(20);

}


}

Result:

Square value: 100.0
Cube value: 8000.0

Can you declare a class as abstract and final also?
Ans: No,abstract class needs sub classes.final key word represents sub classes which can not be created.So, both are quite contradictory and cannot be used for the same class.


Another Sample Program:

/**
 * 
 */
package com.interfaceabhi;

/**
 * @author Abhinaw.Tripathi
 *
 */

abstract class Car
{
 int regno;
 public Car(int r)
 {
regno=r;
 }
 void opentank()
 {
System.out.println("Fill the tank");
 }
 abstract void steering(int direction,int angle);
 abstract void braking (int force);
}

class Maruti extends Car
{

public Maruti(int r) 
{ super(r);
}

@Override
void steering(int direction, int angle) 
{
System.out.println("take a turn");
System.out.println("this is ordinary steering");
}

@Override
void braking(int force)
{
System.out.println("Brakes applied");
System.out.println("These are hydraulic brakes");
}
}

class Santro extends Car
{

public Santro(int r)
{
super(r);
}

@Override
void steering(int direction, int angle)
{
System.out.println("take a turn");
System.out.println("this is power steering");
}

@Override
void braking(int force)
{
System.out.println("Brakes applied");
System.out.println("These are power brakes");
}
 
}

public class AnotherDemo {


public static void main(String[] args) {
// TODO Auto-generated method stub
  
Maruti mrt=new Maruti(1001);
Santro sntr=new Santro(5005);
Car ref;
ref=mrt;
mrt.opentank();
mrt.braking(500);
mrt.steering(1, 90);
}

}

Result:

Fill the tank
Brakes applied
These are hydraulic brakes
take a turn
this is ordinary steering

Take one more Sample Program:

/**
 * 
 */
package com.interfaceabhi;

/**
 * @author Abhinaw.Tripathi
 *
 */
abstract  class Plan
{
  protected double rate;
  
  public abstract void getRate(); 
 public void calculateBill(int units)
 {
System.out.println("Bill amount for " +units + "Units:");
System.out.println(rate*units);
 }
}

class CommercialPlan extends Plan
{

@Override
public void getRate()
{
rate=7.00;
}
}

class DomesticPlan extends Plan
{

@Override
public void getRate()
{
rate=2.0;
}
}


public class AbstractCalculateDemo
{

/**
* @param args
*/
public static void main(String[] args)
{
Plan p;
System.out.println("Commercial connection: ");
p=new CommercialPlan();
p.getRate();
p.calculateBill(250);
System.out.println("Domestic Connection : ");
p=new DomesticPlan();
p.getRate();
p.calculateBill(250);

}

}

Result: 

Commercial connection: 
Bill amount for 250Units:
1750.0
Domestic Connection : 
Bill amount for 250Units:
500.0


No comments: