answersLogoWhite

0


Best Answer

In an abstract class some methods could be abstract meaning a sub-class must provide the actual implementation code or non-abstract in those cases the functionality is common to all or most of the sub-classes. With respect to the non-abstract classes, some sub-classes could override those as needed unless they're defined final.

Example of such classes found in the core J2SE API include:

java.awt.Component, java.awt.geom.Point2D, java.io.InputStream, java.io.Reader, java.util.AbstractCollection, and many others.

Take java.util.AbstractCollection as a typical example. It provides placeholders for abstract methods iterator() and size(), and provides the concrete methods clear(), contains(), isEmpty(), etc. which use the former abstract methods to perform a generic function as defined in the specific implementation of a sub-class. The abstract class defers the details of the specific implementation to its sub-classes. The class java.util.HashSet extends AbstractCollection and defines the non-abstract implementation code for both iterator() and size() methods.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When to use a non-abstract method in an abstract class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you create a concrete method inside abstract class?

The same way you create a concrete method in a concrete class. When a class is abstract, it can contain abstract methods. That doesn't mean that all methods must be abstract. Hope this helps.


Why don't use abstract keyword when we declare a method in interface?

The abstract keyword signifies that the particular method will have no features in the class where it is declared and it is upto the child class to provide the functionality. In case of an interface, the method is already abstract by default and has no code inside it. So there is no actual point in using the abstract keyword there.


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.


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}


When do you declare method or class final?

final is a keyword.it can be used for three posibilities. they are ->we can assign the variable as final. to declaring the variable as final to avoid the reusability of a variable. now i display small program for this type. class A { final int i=10; a(int b) { i=b; System.out.println(i); } } public static void main(String args[]) { A x=new A(20); } } to execute this program .we have an error message.i.e.,the variable i can not be override. ->to use method have final keyword it can not be override.and also ->to use classes has final they can not be inhereted to sub classes.

Related questions

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.


How do you create a concrete method inside abstract class?

The same way you create a concrete method in a concrete class. When a class is abstract, it can contain abstract methods. That doesn't mean that all methods must be abstract. Hope this helps.


Why don't use abstract keyword when we declare a method in interface?

The abstract keyword signifies that the particular method will have no features in the class where it is declared and it is upto the child class to provide the functionality. In case of an interface, the method is already abstract by default and has no code inside it. So there is no actual point in using the abstract keyword there.


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.


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}


When do you declare method or class final?

final is a keyword.it can be used for three posibilities. they are ->we can assign the variable as final. to declaring the variable as final to avoid the reusability of a variable. now i display small program for this type. class A { final int i=10; a(int b) { i=b; System.out.println(i); } } public static void main(String args[]) { A x=new A(20); } } to execute this program .we have an error message.i.e.,the variable i can not be override. ->to use method have final keyword it can not be override.and also ->to use classes has final they can not be inhereted to sub classes.


What is abstract base class?

An abstract class is a class that cannot be directly instantiated. The purpose of such a class is to put some logic in a base class and force derived classes to implement the remaining functionality. Since the full functionality is only available in the derived class, the base class is declared as abstract so that it cannot be instantiated directly.


What is difference between interface and abstract class?

All the methods declared inside an Interface are abstract. Where as abstract class must have at least one abstract method and others may be concrete or abstract. In Interface we need not use the keyword abstract for the methods.


What happens if an abstract modifier is applied to class. Explain?

The classes which have one or more abstract methods are abstract. To declare a class as abstract, use the abstract keyword in front of the class keyword, before the class declaration. Abstract classes cannot be instantiated. Similarly the new keyword cannot be used to create an object of the abstract class. Remember that the constructors and static variables cannot be declared as abstract. Any subclass of an abstract class must either implement all of the abstract methods in the superclass or be itself declared abstract.


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 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.


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.]