answersLogoWhite

0

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

14y ago

What else can I help you with?

Related Questions

How to write an extended abstract effectively?

To write an extended abstract effectively, focus on clearly stating the purpose, methods, results, and conclusions of your research in a concise and organized manner. Use clear and precise language, provide relevant details, and ensure that your abstract is well-structured and easy to follow.


How to write a scientific abstract for a conference?

To write a scientific abstract for a conference, summarize your research purpose, methods, results, and conclusions in a clear and concise manner. Use key words and phrases to highlight the significance of your study and its contribution to the field. Be sure to follow the conference's guidelines for abstract submission and format.


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


How to write an abstract for a conference presentation?

To write an abstract for a conference presentation, summarize your research topic, methods, results, and conclusions in a clear and concise manner. Be sure to highlight the significance of your work and its contribution to the field. Keep the abstract brief, typically around 250-300 words, and follow any specific guidelines provided by the conference organizers.


How can I effectively write a conference abstract?

To write an effective conference abstract, focus on summarizing your research or project clearly and concisely. Include key points like the purpose, methods, results, and conclusions. Use precise language and avoid unnecessary details. Make sure your abstract is well-structured and follows the guidelines provided by the conference organizers.


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.


How to write a good abstract for a research paper?

To write a good abstract for a research paper, summarize the key points of your study, including the research question, methods, results, and conclusions. Keep it concise, clear, and informative, providing a brief overview of your work to help readers understand its significance.


How to write a strong abstract for a research paper?

To write a strong abstract for a research paper, summarize the key points of your study, including the purpose, methods, results, and conclusions. Be concise, clear, and specific, and avoid including unnecessary details. Make sure to highlight the significance of your research and its implications.


Is write an abstract noun?

No. Write is a verb.