answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

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

Wiki User

10y ago

You would typically want to define an abstract class when you want to create an outline of what you want your child classes to do/implement. But, at the same time want to ensure that certain functionality of all your child classes is defined by you. An Abstract class can have both method definitions as well as implementations. So, both of the above can be done in an abstract class. Whereas, an Interface can only have method declarations with no definitions

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why might you want to define an abstract class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is an interface class and what is an abstract class?

The term interface class does not exist in C#. If this is a term describing a class being an interface to other component (human, subsystems, etc), it is very application specific. The designer of that application should know the abstraction.However, C# does have another type called interface. An interface is NOT a class. An interface defines the intention of some behaviors that classes may be extended it and provides the implementation. The intention, is nothing but method signatures, which defines the return data type, the method name, and any method arguments and associated data type. The implementation is the code of the method. Interface is used for separating the concern of design and implementation.Abstract class is a class with abstract keyword. It can be just like a class without that keyword (then, why it is an abstract class?). But it may have some methods or properties defined as abstract. These abstract methods, like the method signatures of an interface, defines the intention.The subclasses of such an abstract class would need to implement those abstract methods (providing the code).There are more common, differences between interfaces and abstract classes, please see answer(s) of those related questions in C# category.


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.


Is it necessary to implement all the methods of abstract classes in derived class?

An Abstract class is a way to organize inheritance, sometimes it makes no since to implement every method in a class (i.e. a predator class), it may implement some to pass to its children but some methods cannot be implemented without knowing what the class will do (i.e. eat() in a Tiger class). Therefore, abstract classes are templates for future specific classes. A disadvantage is that abstract classes cannot be instantiated, but most of the time it is logical not to create a object of an abstract class. heloooooo


Why httpservlet class called abstract?

Because, the creators of the servlet framework cannot give all the functionality that you might want in your application. So, if they make their class abstract, you the developer will be providing all the functionality you need and still stick to the framework standards defined by them.


What are the differences between class and abstract class?

Below is the main difference between the 3 components:Concrete class - Provides implementation for all its methods & also for methods from extended abstract classes or implemented interfacesAbstract class - Does not provide implementation for one or more of its methodsInterface - Does not provide implementation for any of its methods

Related questions

What is an interface class and what is an abstract class?

The term interface class does not exist in C#. If this is a term describing a class being an interface to other component (human, subsystems, etc), it is very application specific. The designer of that application should know the abstraction.However, C# does have another type called interface. An interface is NOT a class. An interface defines the intention of some behaviors that classes may be extended it and provides the implementation. The intention, is nothing but method signatures, which defines the return data type, the method name, and any method arguments and associated data type. The implementation is the code of the method. Interface is used for separating the concern of design and implementation.Abstract class is a class with abstract keyword. It can be just like a class without that keyword (then, why it is an abstract class?). But it may have some methods or properties defined as abstract. These abstract methods, like the method signatures of an interface, defines the intention.The subclasses of such an abstract class would need to implement those abstract methods (providing the code).There are more common, differences between interfaces and abstract classes, please see answer(s) of those related questions in C# category.


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 the abstract of to create?

I assume you want an abstract noun - that might be "creation".


Is it necessary to implement all the methods of abstract classes in derived class?

An Abstract class is a way to organize inheritance, sometimes it makes no since to implement every method in a class (i.e. a predator class), it may implement some to pass to its children but some methods cannot be implemented without knowing what the class will do (i.e. eat() in a Tiger class). Therefore, abstract classes are templates for future specific classes. A disadvantage is that abstract classes cannot be instantiated, but most of the time it is logical not to create a object of an abstract class. heloooooo


Why httpservlet class called abstract?

Because, the creators of the servlet framework cannot give all the functionality that you might want in your application. So, if they make their class abstract, you the developer will be providing all the functionality you need and still stick to the framework standards defined by them.


What are the differences between class and abstract class?

Below is the main difference between the 3 components:Concrete class - Provides implementation for all its methods & also for methods from extended abstract classes or implemented interfacesAbstract class - Does not provide implementation for one or more of its methodsInterface - Does not provide implementation for any of its methods


Write a Java Program to illustarte the use of abstract methods?

Abstract MethodsAn 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 declaredabstract.It does not define void test() from class IllegalAbstractClass.public class IllegalAbstractClass {1 errorYou 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.


What are abstract classin java?

An Abstract class is a special kind of class that cannot be instantiated. It has one or more methods which are not implemented in the class. These methods are declared abstract and they do not contain any code inside them.Ex:abstract class Parent {public abstract String getSon();public abstract String getDaughter();........//More methods that contain specific behaviour/code in them}The above is an abstract class "Parent" that has a lot of functionality but it has declared two abstract methods which have no code inside them. Any class that has one or more abstract methods has to be abstract. This abstract class cannot be instantiated.i.e., the below piece of code will not work. The code will not even compile.Parent object = new Parent();Purpose of Abstract Classes:Abstract classes are generally used where you want an amount of behaviour to be used by the class that extends the abstract class while at the same time giving options to the child class to provide a certain amount of behaviour itself.A Child Class extending the Abstract Class:public class Child extends Parent {public String getSon() {return "Sons Name";}public String getDaughter(){return "Daughters Name";}...... //Code specific to the Child class}


What is purpose of abstraction in java?

Abstract keyword used for method declaration declares the methods without implementations. Abstract class in java have abstract methods that is not implemented in abstract class, but implemented in subclasses in java program. If the class in java program is not required to get instantiated than that class use the abstract keyword but this class rather is available for other classes to extend by other classes. Abstract keyword will be used in method declaration to declare that method without providing the implementation in that java program. In other words we can say that, it formally unfinished class as well as method, that marked with the help of keyword abstract. Defining abstract is a way of preventing someone from instantiating a class that is supposed to be extended first. In java program abstract class is deliberately missing similar to like an interface which will missing all method bodies in the program. Abstract class provides a way to extend an actual class. We will not use new on abstract classes but will use abstract references in the java program, that always point to objects of the class that extends an abstract class. In java program for practical use of an abstract class, we will define a non-abstract class that extends an abstract one. This will use any of the inherited non-abstract methods. Most of the time abstract class may extend another abstract class. In that condition it need not implement all in the non-abstract methods. An abstract keyword used both on classes and methods. In case of class declared with an abstract keyword may not be instantiated that is the only thing that abstract keyword doing.


What is abstaction in java?

The concept of abstraction is concept talked about when talking about inheritance. When you make a class abstract, it means that the class is a general "abstract" idea, not something you want to instantiate (create an object from.) However, abstract classes are useful for when you want to create real sub-classes of the abstract class. For example, you could have an abstract class named "animal" that had the general characteristics of all animals, then you can have regular sub-classes that inherit "animal", like "dog", "cat" or "horse." The reason for making the "animal" class abstract is to make sure that one can't create a generic "animal" object, but so they can create objects that inherit the idea of "animal."


Does the abstract classes contain implementation for methods?

An Abstract class is a special kind of class that cannot be instantiated. It has one or more methods which are not implemented in the class. These methods are declared abstract and they do not contain any code inside them.Ex:abstract class Parent {public abstract String getSon();public abstract String getDaughter();........//More methods that contain specific behaviour/code in them}The above is an abstract class "Parent" that has a lot of functionality but it has declared two abstract methods which have no code inside them. Any class that has one or more abstract methods has to be abstract. This abstract class cannot be instantiated.i.e., the below piece of code will not work. The code will not even compile.Parent object = new Parent();Purpose of Abstract Classes:Abstract classes are generally used where you want an amount of behaviour to be used by the class that extends the abstract class while at the same time giving options to the child class to provide a certain amount of behaviour itself.A Child Class extending the Abstract Class:public class Child extends Parent {public String getSon() {return "Sons Name";}public String getDaughter(){return "Daughters Name";}...... //Code specific to the Child class}You can also have methods that have code implementation in the abstract class but those methods cannot have the abstract keyword in their method declaration.


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;}}