answersLogoWhite

0


Best Answer

when overriding of a class or a method is necessary, they can be declared as abstract

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When do you declare a method or class abstract in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When do you declare a method final in java?

You declare a method final in Java when you do not want any subclasses of your class to be able to override the method. I have also heard that this allows the Java compiler to make more intelligent decisions. For example, it supposedly allows Java to decide when to make a method inline. (Note that this is all unconfirmed)


How nesting of methods is done in java?

No, Java only allows a method to be defined within a class, not within another method.


Abstract method with example in java?

An abstract in java is used to specify that the class/function is not yet complete. When a class in declared as abstract it means that it is not meant to be instantiated (you can't create variables of that type). This is because they are meant to be more of a guideline for other classes. When a class extends an abstract class it must either define all of the abstract methods from the abstract class or it must also be declared as an abstract class itself.


Can you call an abstract method from a non abstract method in java?

We can't call (i.e, execute) an abstract method in java because these methods don't contain any code to execute!In some special cases like when an abstract method is overridden in a subclass, and when we are using super class reference variable( which is referring that subclass object), it appears that we are calling abstract method in super class. But actually the code in the subclass method is being executed.Example:abstract class SuperClass{abstract void show(); //abstract method in super class}class SubClass extends SuperClass{void show(){ //show() of SuperClass overridden in SubClassSystem.out.println("SubClass Method");}}class Example{public static void main(String... args){SuperClass sup=new SubClass();sup.show(); //SubClass show() will be executed !!!}}


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}

Related questions

When do you declare a method final in java?

You declare a method final in Java when you do not want any subclasses of your class to be able to override the method. I have also heard that this allows the Java compiler to make more intelligent decisions. For example, it supposedly allows Java to decide when to make a method inline. (Note that this is all unconfirmed)


How nesting of methods is done in java?

No, Java only allows a method to be defined within a class, not within another method.


Abstract method with example in java?

An abstract in java is used to specify that the class/function is not yet complete. When a class in declared as abstract it means that it is not meant to be instantiated (you can't create variables of that type). This is because they are meant to be more of a guideline for other classes. When a class extends an abstract class it must either define all of the abstract methods from the abstract class or it must also be declared as an abstract class itself.


Can you call an abstract method from a non abstract method in java?

We can't call (i.e, execute) an abstract method in java because these methods don't contain any code to execute!In some special cases like when an abstract method is overridden in a subclass, and when we are using super class reference variable( which is referring that subclass object), it appears that we are calling abstract method in super class. But actually the code in the subclass method is being executed.Example:abstract class SuperClass{abstract void show(); //abstract method in super class}class SubClass extends SuperClass{void show(){ //show() of SuperClass overridden in SubClassSystem.out.println("SubClass Method");}}class Example{public static void main(String... args){SuperClass sup=new SubClass();sup.show(); //SubClass show() will be executed !!!}}


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}


Subclasses of an abstract class that do not provide an implementation of an abtract method are also abstract?

Yes. Any class that does not provide implementation to all its methods as well as its parent class methods needs to be Abstract. The Java compiler would not successfully compile a class that does not do this.


What do mean by interface in java?

An interface in Java is like an abstract class, but there are no method bodies allowed in it and it has to be declared with the interface keyword. It is Java's way of getting around the Deadly Diamond of Death. Only abstract methods and constants are allowed in it.


How do you prevent extending a class in java?

You can declare a class as "final".


What is abstraction method in java?

Abstraction in Java or Object oriented programming is a way to segregate implementation from interface and one of the five fundamentals along with Encapsulation, Inheritance, Polymorphism, Class and Object. Abstraction in Java is achieved by using interface and abstract class in Java.


What is the of abatract keyword in java 1.7?

"abstract" is used for a class that is not supposed to be instantiated directly. The reason this is used is because the class is not complete; the details are supposed to be filled out in subclasses (derived classes). An abstract class will usually have one or more abstract methods; a method that is declared by name (to make sure derived classes have this method), but without a method body.


How do you return an object in java?

With the command return, followed by an object variable. In the method header, you have to declare the return type as the class of the object.


How do you prevent class extending in java?

Declare the class as final. final class A{ ... }