Navigation Links

Responsive Topnav Example

Resize the browser window to see how it works.

Monday, March 25, 2013

Example of abstract class in java



Below Program explain example of abstract class in java


package com.thanu.welcome;

/*
 * Abstract Class Example.
 * Abstract class have Abstract method or method implementation.
 * It can't be instantated, But we can be subclassed. Also, we can access static method from abstract class
 * But Subclass must implemented abstract method of abstract class.Otherwise, it will be abstract
 * Different b/w Abstract class or interface.
 * In Abstract class can have method implemented, and abstract methods. But interface have only method definintion.
 * Interface have only static and Final Variables. But Absract class any one variable.
 * Situation for using abstract class:
 * In Object oriented Drawing Application:
 * Some Object have common behavior and some object have different behaviour.
 * For ex: Fillcolor,MoveTo,position is same for circle,rectanlge,triangle
 * draw,resize will be different behavior for circle, rechtangle, trigangle
 * Same behaviour can be implemented , different behaviour will be abstract method.
 */
abstract class MyAbstract {
int a,b;

//Method Implementation in Abstract Class
void fillColor (int x, int y) {

System.out.println("This is fillColor method " + x +" "+  y);
}
//Static method in MyAbstract Class
static void printValue() {
System.out.println("Just simply printing value static method");
}

//Method Definition in Abstract Class
abstract void draw(int x, int y);
abstract void resize(int x,int y);
}


public class ExampleAbstractClass extends MyAbstract {

public static void main (String arg[]) {

System.out.println("Many more welcome to our website");

//Calling static method from Abstract Method
MyAbstract.printValue();

//Create object for base class
ExampleAbstractClass instance = new ExampleAbstractClass();

//calling Abstract Class Implemented Method
instance.fillColor(10,20);

instance.draw(20,30);

instance.resize(40,50);



}

void draw (int x, int y) {
System.out.println("Implementation of draw method");
}
void resize(int x,int y) {
System.out.println("Implementation of resize method");

}


}

No comments:

Post a Comment