Java – abstract methods have no body?

I'm a novice in Java (I've been studying for 4 months now) So my question may seem too simple My understanding is that abstract methods have no body and cannot provide implementation

So how does this work?

public abstract void fillRect (int x,int y,with,height);

I didn't point out the problem clearly We have abstract methods If I don't provide text, just parameters, why does it draw a rectangle

for example

public void paint (Graphics g) {

g.fillRect (5,5,30,30);

}

Solution

You need to know two things

-Declaration: the prototype or structure of a method For example:

public int add(int a,int b);

-Definition: implementation of method

public int add(int a,int b) {
        this.a = a;
        this.b = b;
        return a + b;
    }

Abstract methods can now have a declaration, a structure or a prototype But it has no definition The definition should be done in a class that extends a class that contains abstract methods:

class A {
        public abstract int add(int a,int b); //just declaration- no body
    }
    class B extends A {
        /*must override add() method because it is abstract in class A i.e class B must have a body or deFinition of add()*/ 
        int a,b;
        public int add(int a,int b) {
            this.a = a;
            this.b = b;
            return a + b;
        }
    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>