Friday , December 13 2019

JAVA #25 – Abstract Methods in Java

Abstract Methods

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon).

abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
    abstract void resize();
}

Each nonabstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods:

class Circle extends GraphicObject {
    void draw() {
        ...
    }
    void resize() {
        ...
    }
}
class Rectangle extends GraphicObject {
    void draw() {
        ...
    }
    void resize() {
        ...
    }
}

 

//File name AbstractMethods.java
package abstractmethods;
public class AbstractMethods extends Shape{
    @Override
    public void Rectangle()
    {
        System.out.println("");
    }
    @Override
    public void Line(int x, int y)
    {
        System.out.println("The Sum of line  is: From AbstractMethod Class "+(x+y));
    }
    
    @Override //Annotatoin in Java
    public void Triangle(int x, int y, int z)
    {
        System.out.println("The Area of Triangle is: "+(x+y+z));
    }
    public static void main(String[] args) {
        AbstractMethods objAbs = new AbstractMethods();
        objAbs.Line(45, 77);
        objAbs.Triangle(12, 45, 34);
        Shape2 objS2 = new Shape2();
        objS2.Line(13, 34);
                                
    }
    
}

 

//File name Shape.java and must be store in above package.
package abstractmethods;
public abstract class Shape {    
        
       public abstract void Triangle(int x , int y, int z); // Abstact Method Decleration
       public abstract void Rectangle(); // Abstact Method Declerations
       public abstract void Line(int x, int y); // Abstact Method Declerations
}
class Shape2 extends Shape{
    @Override
    public void Rectangle()
    {
        System.out.println("");
    }
    @Override
    public void Line(int x, int y)
    {
        System.out.println("The Sum of Line is: "+(x+y));
    }
    
    @Override //Annotatoin in Java
    public void Triangle(int x, int y, int z)
    {
        System.out.println("The Area of Triangle is: "+(x+y+z));
    }

}

 

 

About Faheem

Faheem
An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

JAVA #32 – Java application using SQL server (Part: 01)

  In this part we made the GUI front end of a Application.

Leave a Reply

Your email address will not be published.