Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
102 views

Javaabstractclassmethods

An abstract class is a class that is declared with the abstract keyword. An abstract class cannot be instantiated, and can contain abstract methods that have no body. If a class contains an abstract method, then the class itself must be declared abstract. To use an abstract class, another class must inherit from it and implement all of its abstract methods. Abstract methods define a method signature but no body, and end with a semicolon instead of curly braces.

Uploaded by

pradnya sadigale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Javaabstractclassmethods

An abstract class is a class that is declared with the abstract keyword. An abstract class cannot be instantiated, and can contain abstract methods that have no body. If a class contains an abstract method, then the class itself must be declared abstract. To use an abstract class, another class must inherit from it and implement all of its abstract methods. Abstract methods define a method signature but no body, and end with a semicolon instead of curly braces.

Uploaded by

pradnya sadigale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Pradnya Sadigale

 A class which contains the abstract keyword in its


declaration is known as abstract class.
 Abstract classes may or may not contain abstract
methods, i.e., methods without body ( public void
get(); )
 But, if a class has at least one abstract method, then
the class must be declared abstract.
 If a class is declared abstract, it cannot be
instantiated.
 To use an abstract class, you have to inherit it from
another class, provide implementations to the
abstract methods in it.
 If you inherit an abstract class, you have to provide
implementations to all the abstract methods in it.
Example of Abstract Class
public abstract class Employee
{
private String name;
private String address;
public Employee(String name, String address, int number)
{
System.out.println("Constructing an Employee");
this.name = name;
}
public double computePay()
{
System.out.println("Inside Employee computePay");
return 0.0;
}
}
Now lets try to instantiate the Employee
class in the following way −
public class AbstractDemo
{
public static void main(String [] args)
{
Employee e = new Employee("George W.“);
System.out.println("\n Call Employee ");
e.mailCheck();
}
}

Employee.java:46: Employee is abstract; cannot be instantiated


Employee e = new Employee("George W”);
^ 1 error
If you want a class to contain a particular method
but you want the actual implementation of that
method to be determined by child classes, you
can declare the method in the parent class as an
abstract.
 abstract keyword is used to declare the method
as abstract.
 You have to place the abstract keyword before
the method name in the method declaration.
 An abstract method contains a method signature,
but no method body.
 Instead of curly braces, an abstract method will
have a semoi colon (;) at the end.
Following is an example of the abstract method.

public abstract class Employee


{
private String name;
public abstract double computePay();
}

Declaring a method as abstract has two


consequences −
 The class containing it must be declared as
abstract.
 Any class inheriting the current class must
either override the abstract method or declare
itself as abstract.
Suppose Salary class inherits the Employee
class, then it should implement computePay()

public class Salary extends Employee


{
private double salary;
double computePay()
{
System.out.println("Computing salary " +
getName());
return salary/52;
}
}

You might also like