answersLogoWhite

0


Best Answer

In most cases, you will want to use abstract classes IN ADDITION to interfaces.

You should use an abstract class in the following circumstance:

  1. the abstract class is clearly part of class hierarchy, and does not just describe some sort of basic functionality. Specifically, abstract classes are a good idea where they will be directly inherited from to create a concrete concept, but the abstract class itself is too indistinct to have any specific instance of it created.
  2. You want to provide an implementation for a set of methods that will be reused by a significant number of other classes, all of which can be fit into a class hierarchy.

In practice, abstract classes are a good way to collect common code into one place, to make maintenance easier.

For instance, say you have a class and interface structure like this:

Class A

Interface X

Class B extends A implements X

Class C extends A implements X

Both B and C will have the all the methods declared in X; if the implementation of those methods is the same (or can be made the same), then X is a good candidate for changing to an abstract method:

Class A

Abstract Class X extends A

Class B extends X

Class C extends X

Thus, you have removed the code duplication that was happening when using interfaces.

Note that doing the above is NOT a good idea if any class which implement interface X cannot be made a subclass of the new abstract class X.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such.

A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Abstract classes are those classes that cannot be instantiated. Those classes are not fully implemented. Abstract class is base class for the family of child classes. It may contain some basic features but for some features only signature are given, the body of those methods must be written after inheriting this abstract class. Only if child implements all of the abstract methods it might be used (instantiated), otherwise child class is abstract too.

To summarize it, you cannot instantiate class that is partial (missing all/some implementation).

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

In case of interface, we can enforce the child classes to implement all the methods but in case of abstract classes, you can provide implementation for as many methods as you want. In some cases this might be confusing to novice developers.

Interfaces are easier because, Complete implementation is handed over to the derived classes. depending on the requirement they can provide their own implementation.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

An abstract class is a class that is used to inherit other classes from, but that does not serve a useful purpose to instantiate all by itself.

For example, you might have an abstract class called Shape, which contains properties that all shapes possess (color, number of sides, size, etc.), and then create specific shape classes (square, triangle, etc.) that all inherit from the class.

It wouldn't be useful to create a Shape class by itself, which is why it's abstract.

The benefit of using abstract classes are twofold. First, you can take the common properties of the derived classes and put them in one place. Second, you can cast the derived class to its abstract class when you need to pass it around in methods. C# Example:

void Test()

{

Shape triangle = new Triangle();

Shape square = new Square();

CreateShape(triangle);

CreateShape(square);

}

void CreateShape(Shape shape)

{

Console.WriteLine("I have " + shape.sides + " side(s)");

}

Because the CreateShape object is expecting a Shape, it can accept both Square and Triangle objects. (Note: This is not unique to abstract classes. You can do a similar thing with any derived class).

The CreateShape method can accept any object that was derived from the Shape class. It can only access properties and methods that were defined in the abstract class. You can cast the abstract class to the derived class, but would get an error if you passed an object of another type. For example, suppose the Triangle class had a Angle property, and you modified CreateShape like this:

void CreateShape(Shape shape)

{

Console.WriteLine("My angle is " + ((Triangle)shape).Angle);

}

This would work find if you called CreateShape(triangle), but would throw an exception if you called CreateShape(square);

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

No, not directly.

You cannot use new operator to create either. However, you may create an instance of derived class from the abstract class, and treat (by casting, for example) as an instance of the abstract class.

Similar to an interface: create an object from a class that implements the target interface, then treat that object as an instance of that interface.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The questions I would ask the designer (could be you, could be some one else):

  • Do you have default behavior (implementation) in mind? If the answer is yes, then class-hierarchy (not necessary an abstract class) is more favorable than an interface
  • Is there any data member involve? If yes, then class is more favorable.
  • Are there more than 1 class involved and each derived from different base classes? If yes, then an interface is more favorable
    • Note If you cannot modify either of the base classes involved, an interface may be the only way
This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Yes. Although an instance of an abstract class cannot be created directly, it is a very good place to implement default behaviors (the interface) for the derived classes to eliminate duplicate codes, yet the derived class has the option to override the default method implementation with its own special way.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

An abstract class has at least abstract methods. An interface has only abstract actions. For example, the javax.swing.AbstractAction class has only one abstract method that must be overriden: public void actionPerformed(ActionEvent e). The java.awt.event.ActionListener interface has the same method that must be overriden; however, it is its only method. The AbstractAction class is a subclass of the Action class, which implements the ActionListener interface. The AbstractAction class has overriden all but one of the Action class's methods.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Because that's the whole idea of declaring a class abstract - to avoid objects from being instantiated. The general idea is that an abstract class is "incomplete" - some details have to be filled out in derived (inherited) classes.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you use abstract classes instead of interfaces?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do OO analysis and design facilitate reusability?

Through the use of interfaces and abstract classes that define common behaviors


What is difference in use between interfaces and abstract classes in java?

While neither abstract classes nor interfaces can be instantiated in Java, you can implement methods in abstract classes. Interfaces can only define methods; no code beyond a method header is allowed.


Real time example for interface vs abstract class in java?

w.frnds........ I am just trying to an example of abstract class and interface class in real life . As these two ["interface class" is not a term in Java programming - just "interface"] classes [sic] are a concept of objest orientation so easy we can easily compare thhese with our real life . Suppose we have an abstract class called clark and an abstract method behabour of this abstract class ,which has no definition in abstract class. two other class security and receptionist inherits these clark class. So in thses two derived class there must has to be a defonation of behabour method,which depends on the derived class which types of behabour they will show........ So that is a real life example of Abstract class .Interface is also same as abstract class only the difference is it can't contain any implementation of any method in base class or super class. I think this is a sufficient example to understand abstract class and interface. [No, it is not sufficient.] If u have any doubt then u can contact me with this email id-rkmahanta26@gmail.com [Interfaces support multiple inheritance; classes do not. Interfaces contain only public members; classes do not have to. Interfaces do not have superclasses, except the implicit 'Object' supertype; they have superinterfaces. Nested interfaces are always static, never inner, unlike classes which can be inner classes. "u" is not an English pronoun. Use the tutorial and the JLS to understand interfaces and abstract classes, not this garbage answer.]


Abstract class vs interface?

Comparison between an Abstract Class and an Interface:While an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared. These rules are strict:


Why might you want to define an abstract class?

You would use an abstract class when you want a number of classes to have a similar functionality/methods list. The abstract class will have only method declarations and no definitions. So any class that extends this abstract class would have to provide the method definitions. This way you can ensure that all these classes will have a similar set of methods/features. This type of usage of an abstract class is similar to interfaces.

Related questions

When to use interfaces and when to use abstract classes?

just one word supscription interface means mediater abstract means data hiding


How do OO analysis and design facilitate reusability?

Through the use of interfaces and abstract classes that define common behaviors


What is difference in use between interfaces and abstract classes in java?

While neither abstract classes nor interfaces can be instantiated in Java, you can implement methods in abstract classes. Interfaces can only define methods; no code beyond a method header is allowed.


Why do you use interfaces instead of class?

JAVA does not support multiple inheritance(i.e more than one class cant be inherited by other classes.) but multiple inheritance is a very important concept . For this reason interface is used which is a kind of class but he difference is that it contains only constant variables and abstract methods. abstract methods contains only the declaration not the codes.


Real time example for interface vs abstract class in java?

w.frnds........ I am just trying to an example of abstract class and interface class in real life . As these two ["interface class" is not a term in Java programming - just "interface"] classes [sic] are a concept of objest orientation so easy we can easily compare thhese with our real life . Suppose we have an abstract class called clark and an abstract method behabour of this abstract class ,which has no definition in abstract class. two other class security and receptionist inherits these clark class. So in thses two derived class there must has to be a defonation of behabour method,which depends on the derived class which types of behabour they will show........ So that is a real life example of Abstract class .Interface is also same as abstract class only the difference is it can't contain any implementation of any method in base class or super class. I think this is a sufficient example to understand abstract class and interface. [No, it is not sufficient.] If u have any doubt then u can contact me with this email id-rkmahanta26@gmail.com [Interfaces support multiple inheritance; classes do not. Interfaces contain only public members; classes do not have to. Interfaces do not have superclasses, except the implicit 'Object' supertype; they have superinterfaces. Nested interfaces are always static, never inner, unlike classes which can be inner classes. "u" is not an English pronoun. Use the tutorial and the JLS to understand interfaces and abstract classes, not this garbage answer.]


Abstract class vs interface?

Comparison between an Abstract Class and an Interface:While an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared. These rules are strict:


Why might you want to define an abstract class?

You would use an abstract class when you want a number of classes to have a similar functionality/methods list. The abstract class will have only method declarations and no definitions. So any class that extends this abstract class would have to provide the method definitions. This way you can ensure that all these classes will have a similar set of methods/features. This type of usage of an abstract class is similar to interfaces.


Can you choose a Windows or Mac desktop display when you use the Linux operating system?

No. Those user interfaces are copyrighted. However, some distros have fairly similar interfaces that you can use instead.


What are the differences between an abstract class and an interface in java?

They are very different. An abstract class is a class that represents an abstract concept (google define "abstract" if you're unsure) such as 'Thoughts' or 'BankAccount'. When a class is defined as abstract it cannot be used (directly) to create an object. Abstract classes are used as super-classes so that all of their subclasses inherit all methods. Interfaces can be thought of as contracts with all of their implementing classes. They simply require all implementing classes to have methods with the same signature as that defined in the interface, but such methods can behave as appropriate. Hope that helps :)


Compare anonymous class with abstract class?

Abstract Classes contain the work abstract in it. It is used when you know that you will need to use an object of its type but do not know the inner workings yet. Anonymous classes are those classes that are constructed on the fly. You need to know its inner workings.


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.


What is difference between interface and abstract in java?

While an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared. These rules are strict: • All interface methods are implicitly public and abstract. In other words, you do not need to actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract. (You can use any kind of modifiers in the Abstract class) • All variables defined in an interface must be public, static, and final-in other words, interfaces can declare only constants, not instance variables. • Interface methods must not be static. • Because interface methods are abstract, they cannot be marked final, strictfp, or native. (More on these modifiers later.) • An interface can extend one or more other interfaces. • An interface cannot extend anything but another interface. • An interface cannot implement another interface or class. • An interface must be declared with the keyword interface. You must remember that all interface methods are public and abstract regardless of what you see in the interface definition.