answersLogoWhite

0


Best Answer

Abstract Methods

An abstract method is a method that's been declared as abstract but not implemented. In other words, the method contains no code. You mark a method abstract when you want to force subclasses to provide the implementation. (remember the reason why you would want to make a class abstract from the previous paragraph)

Ex: public abstract void showSample();

Notice that the abstract method ends with a semicolon instead of curly braces. It is illegal to have even a single abstract method in a class that is not explicitly declared abstract! Look at the following illegal class:

public class IllegalAbstractClass{

public abstract void test();

}

The preceding class will produce the following error if you try to compile it:

IllegalClass.java:1: class IllegalAbstractClass must be declared

abstract.

It does not define void test() from class IllegalAbstractClass.

public class IllegalAbstractClass {

1 error

You can, however, have an abstract class with no abstract methods. The following example will compile fine:

public abstract class LegalAbstractClass{

void test() {

// you can write lots of code here

}

}

In the preceding example, test() is not abstract. Three different clues tell you it's not an abstract method:

• The method is not marked abstract.

• The method declaration includes curly braces, as opposed to ending in a semicolon. In other words, the method has a method body.

• The method contains actual implementation code.

Any class that extends an abstract class must implement all abstract methods of the superclass, unless the subclass is also abstract. The rule is this:

The first concrete subclass of an abstract class must implement all abstract methods of the superclass.

Concrete just means nonabstract, so if you have an abstract class extending another abstract class, the abstract subclass doesn't need to provide implementations for the inherited abstract methods. Sooner or later, though, somebody's going to make a nonabstract subclass (in other words, a class that can be instantiated), and that subclass will have to implement all the abstract methods from up the inheritance tree.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a Java Program to illustarte the use of abstract methods?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is an abstract class Write the syntax Declare a pure virtual function in an abstract class?

an abstract class is nothing but class which contains both abstract and concrete methods for abstract class we r nt create object Syntax for pure abstract class is abstract class x { public void abstract y(); public void abstract z(); public void abc() { }


What is the difference between an interface and an abstract class?

We can't instantiate both interfaces and abstract classes.The only one difference between them is that an interface can't contain concrete(fully defined) methods where as an abstract class may contain them.An abstract class not necessarily contain abstract methods. we can make a class as abstract class even it does not has any abstract methods.When there is a need to write both abstract and concrete methods in a single unit we have to use an abstract class instead of an interface since an interface cant contain concrete methods.All the fields(or properties) of an interface are by default 'static final' even when you don't mention explicitly. And all methods are 'public abstract'.But in an abstract class we can have any type of fields and methods.


Write a java code to draw a circle inside in an ellipse?

write a program draw circle and ellipse by using oval methods in java


Write a program in c language to implement framing methods like character stuffing?

A program in c language to implement framing methods like character stuffing can be grave sizeCRC-32 and the variable c50.


When should you write the abstract of your paper apa style?

Write the abstract when your paper is near completion.


Is write an abstract noun?

No. Write is a verb.


How do you achieve abstraction in java?

Abstraction in Java is achieved using interfaces and abstract class. abstract means something which is not complete or concrete but abstraction itself is a great programming concept and allow you to write code which is more flexible to change.


Explain the meaning of abstract keyword in relation to classes and methods?

The abstract keyword is used to denote abstract classes and abstract methods in Java.An abstract method is a method which is defined, but not implemented:public abstract void doStuff();An abstract class is a class which contains one or more abstract methods*, and which cannot be instantiated directly. All subclasses of an abstract class must implement the abstract methods.* Note that abstract classes can include no abstract methods, but this rather defeats the purpose of using an abstract class.The quintessential example of an abstract class is the Shape class.// Definition of our abstract classpublic abstract class Shape {// Notice how we can actually declare and implement variables and methods.// This is what differentiates between an abstract class and an interface.// The location of this shapeprivate double x,y;// Change our location - all subclasses will have this by defaultpublic void moveTo(final double newX, final double newY) {x = newX;y = newY;}// Definitions of our abstract classes.// All classes which extend from Shape must implement these.public abstract double getArea();public abstract double getPerimiter();}// Definition of our concrete example classpublic class Rectangle extends Shape {// Beyond the x,y location of Shape, Rectangle must have width and heightprivate double width, height;// Implement abstract methodspublic double getArea() {return width * height;}public double getPerimiter() {return width + width + height + height;}}


How do you write an abstract?

An abstract explains the general purpose of an experiment, or the gist of a text.


How do you write algorithm for java program?

An "algorithm" is a method to solve a problem. These methods are more or less independent of the language. First you think about how you will solve a certain problem, step by step. Then you translate this into a computer program.


How do you write a abstract?

I really dont know how write an abstract paper .But if someone one could help me that would be awesome! plz somebody help me plz!!!..../


Where can someone find information how to write an Abstract?

You can find information on how to write an Abstract online at websites such as Writing Center and Emerald Insight. It is also best to review other Abstract works before starting your own to get a general idea.