Karatas, FERHAT
I would love to change the world, but they won't give me the source code!
Abstract Methods and Classes
28.02.2007 11:09:49 - Filed under : Java
An abstract class is a class that is declared abstract-it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, the class itself must be declared abstract, as in:
public abstract class GraphicObject {
       // declare fields
       // declare non-abstract methods
       abstract void draw();
    }
When an abstract class is subclassed, the subclass provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

Note: All of the methods in an interface are implicitly abstract, so the abstract modifier is not used with interface methods (it could be-it's just not necessary).

Example :

abstract class myBase{
    //A Non abstract method
    int AddTwoNumbers(int Num1, int Num2)
    {
        return Num1 + Num2;
    }

    //An abstract method, to be overridden in derived class
    abstract int MultiplyTwoNumbers(int Num1, int Num2);
 }

class myBusiness extends myBase {
   
    /** Creates a new instance of myBusiness */
    myBusiness() {
    }   
   
    //implementing the abstract method MultiplyTwoNumbers
    int MultiplyTwoNumbers(int Num1, int Num2)
    {
        return Num1 * Num2;
    }
}

public class myStart {
   
    /** Creates a new instance of myStart */
    public myStart() {
    }
   
    public static void main (String args[]){
       //You can create an instance of the derived class

       myBusiness calculate = new myBusiness();
       int added = calculate.AddTwoNumbers(10,20);
       int multiplied = calculate.MultiplyTwoNumbers(10,20);
       System.out.println( "Added :" + added + " Multiplied :" + multiplied);
    }
}

Keywords : Abstract class cannot be instantiated. And subclass provides implementations for all of the abstract methods in its parent class.
with 0 comments

What do you think ?
Security Code 4808
Check S. Code
Name  
Email  
Home Page  
Comments