0% found this document useful (0 votes)
3 views

abstract classes

An abstract class in Java, declared with the 'abstract' keyword, cannot be instantiated directly and must be subclassed. It can contain both abstract methods (without bodies) and non-abstract methods (with bodies), as well as constructors and static methods. Key points include that a class with any abstract methods must itself be declared abstract, and if a subclass does not implement all abstract methods, it must also be declared abstract.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

abstract classes

An abstract class in Java, declared with the 'abstract' keyword, cannot be instantiated directly and must be subclassed. It can contain both abstract methods (without bodies) and non-abstract methods (with bodies), as well as constructors and static methods. Key points include that a class with any abstract methods must itself be declared abstract, and if a subclass does not implement all abstract methods, it must also be declared abstract.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Abstract Class in Java

In Java, abstract class is declared with the abstract keyword. It may


have both abstract and non-abstract methods(methods with bodies).
An abstract is a Java modifier applicable for classes and methods in Java
but not for Variables. In this article, we will learn the use of abstract
classes in Java.
What is Abstract Class in Java?
Java abstract class is a class that can not be initiated by itself, it needs
to be subclassed by another class to use its properties. An abstract
class is declared using the “abstract” keyword in its class definition.
Illustration of Abstract class
abstract class Shape
{
int color;
// An abstract function
abstract void draw();
}
In Java, the following some important observations about abstract
classes are as follows:
1 : An instance of an abstract class can not be created.
2 : Constructors are allowed.
3 : We can have an abstract class without any abstract method.
4 : There can be a final method in abstract class but any abstract
method in class(abstract class) can not be declared as final or in
simpler terms final method can not be abstract itself as it will yield an
error: “Illegal combination of modifiers: abstract and final”
5: We can define static methods in an abstract class
6: We can use the abstract keyword for declaring top-level classes
(Outer class) as well as inner classes as abstract
7: If a class contains at least one abstract method then compulsory
should declare a class as abstract
8: If the Child class is unable to provide implementation to all abstract
methods of the Parent class then we should declare that Child class as
abstract so that the next level Child class should provide
implementation to the remaining abstract method
Examples of Java Abstract Class
1. Example of Abstract Class that has Abstract method
Below is the implementation of the above topic:
// Abstract class
abstract class Sunstar {
abstract void printInfo();
}

// Abstraction performed using extends


class Employee extends Sunstar {
void printInfo()
{
String name = "ali";
int age = 21;
float salary = 222.2F;
System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
}
// Base class
class Base {
public static void main(String args[])
{
Sunstar s = new Employee();
s.printInfo();
}
}
Output
ali
21
222.2
2. Abstract Class having constructor, data member, and methods
Elements abstract class can have

data member
abstract method
method body (non-abstract method)
constructor
main() method.
Below is the implementation of the above topic:
// Java Program to implement Abstract Class
// having constructor, data member, and methods
import java.io.*;
abstract class Subject {
Subject() {
System.out.println("Learning Subject");
}
abstract void syllabus();
void Learn(){
System.out.println("Preparing Right Now!");
}
}
class IT extends Subject {
void syllabus(){
System.out.println("C , Java , C++");
}
}

class GFG {
public static void main(String[] args) {
Subject x=new IT();

x.syllabus();
x.Learn();
}
}
Output
Learning Subject
C , Java , C++
Preparing Right Now!

You might also like