answersLogoWhite

0

What are abstract classin java?

Updated: 8/10/2023
User Avatar

Wiki User

11y ago

Best Answer

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

}

User Avatar

Wiki User

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

Wiki User

13y ago

An abstract class is just like any other class in java just with a little difference; at least one of his methods is not implemented (a abstract method). For this reason abstract classes can not be instantiated (create a sample of this class), and his unique purpose is to be inherited an implemented by others classes.

This is a sample of a abstract class:

public abstract class MyAbstract{

public void normalMethod(){//Do something}

public abstract void abstractMethod();

}

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

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

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

An abstract class is a class that can not be directly instantiated - you cannot create objects based on such a class. The class is declared as "abstract" because it is incomplete; the only way to use it is to create subclasses, in which the missing parts (missing methods) are completed.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are abstract classin java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How many constructor can be defined in a classin java p rogramming?

how many constructer can be defined in class in overloading of java programming


What is Abstract-Interface in java?

All interfaces are abstract.


What has the author D S Malik written?

D. S. Malik has written: 'Java Programming' 'Java programming' -- subject(s): Java (Computer program language) 'Fundamentals of abstract algebra' -- subject(s): Abstract Algebra, Algebra, Abstract 'C++ Programming'


Are all java stream class an abstract?

yes


Interface in java?

include all abstract method


When do you declare a method or class abstract in java?

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


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.


Send mobile banking project using java abstract?

ikkk


Is built-in data-type are abstract data-types in java?

All built-in data types are not abstract data types.


What is interface in java?

Interface is collection of abstract methods which has only declaration and no implementation


What kind of constructs can be declared in a Java interface?

Constants and abstract methods. That's it.


How do you achieve abstraction in java?

Abstraction in Java is achieved using interfaces and abstract class. abstract means something which is not complete or concrete but abstraction itself is a great programming concept and allow you to write code which is more flexible to change.