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

Abstract Class in Java.pdf1

Hawassa university

Uploaded by

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

Abstract Class in Java.pdf1

Hawassa university

Uploaded by

ephraimhabtamu
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

An abstract class in Java acts as a partially implemented class that itself cannot be instantiated. It
exists only for subclassing purposes, and provides a template for its subcategories to follow.
Abstract classes can have implementations with abstract methods. Abstract methods are declared
to have no body, leaving their implementation to subclasses.

Points to Remember

o An abstract class must be declared with an abstract keyword.


o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the body of the
method.
/* File name : Employee.java */

public abstract class Employee {

private String name; private String address;

private int number;

public Employee(String name, String address, int number) { System.out.println("Constructing an


Employee");

this.name = name;

this.address = address;

this.number = number; }

public double computePay() { System.out.println("Inside Employee computePay");

return 0.0; }

public void mailCheck() { System.out.println("Mailing a check to " + this.name + " " + this.address);
}

public String toString() { return name + " " + address + " " + number; }

public String getName() { return name; }

public String getAddress() { return address; }

public void setAddress(String newAddress)

{ address = newAddress; }
public int getNumber() { return number;

Inheriting the Java Abstract Class


/* File name : Salary.java */

public class Salary extends Employee {

private double salary;

// Annual salary

public Salary(String name, String address, int number, double salary) { super(name, address, number);

setSalary(salary); }

public void mailCheck() { System.out.println("Within mailCheck of Salary class ");

System.out.println("Mailing check to " + getName() + " with salary " + salary); }

public double getSalary() { return salary; }

public void setSalary(double newSalary) { if(newSalary >= 0.0) { salary = newSalary; }

public double computePay() { System.out.println("Computing salary pay for " + getName());

return salary/52; }

/* File name : AbstractDemo.java */

public class AbstractDemo {

public static void main(String [] args)

Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00); Employee e = new Salary("John
Adams", "Boston, MA", 2, 2400.00); System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();

System.out.println("\n Call mailCheck using Employee reference--");


e.mailCheck();

abstract class Employee {

private String name;

private String address;

private int number;

public Employee(String name, String address, int number) { System.out.println("Constructing an


Employee");

this.name = name;

this.address = address;

this.number = number;

public double computePay() { System.out.println("Inside Employee computePay");

return 0.0;

public void mailCheck() {

System.out.println("Mailing a check to " + this.name + " " + this.address);

public String toString() {

return name + " " + address + " " + number;

public String getName() {

return name;

public String getAddress() {

return address;

}
public void setAddress(String newAddress) {

address = newAddress; }

public int getNumber() {

return number;

class Salary extends Employee { private double salary;

// Annual salary

public Salary(String name, String address, int number, double salary) {

super(name, address, number);

setSalary(salary); }

public void mailCheck() {

System.out.println("Within mailCheck of Salary class ");

System.out.println("Mailing check to " + getName() + " with salary " + salary);

public double getSalary() { return salary; }

public void setSalary(double newSalary) { if(newSalary >= 0.0) { salary = newSalary; }

public double computePay() { System.out.println("Computing salary pay for " + getName());

return salary/52;

Java Abstract Methods


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.
/* File name : AbstractDemo.java */

public class AbstractDemo {

public static void main(String [] args) {

Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00); System.out.println("salary: " +


s.computePay());

abstract class Employee {

private String name;

private String address;

private int number;

public Employee(String name, String address, int number) { System.out.println("Constructing an


Employee");

this.name = name;

this.address = address;

this.number = number;

public abstract double computePay();

// Remainder of class definition public String getName() {

return name;

public void setName(String name) {

this.name = name;

public String getAddress() {

return address;

public void setAddress(String address) {


this.address = address;

public int getNumber() {

return number;

public void setNumber(int number) {

this.number = number;

class Salary extends Employee {

private double salary;

// Annual salary

public Salary(String name, String address, int number, double salary) {

super(name, address, number);

this.salary = salary;

} public double computePay() {

System.out.println("Computing salary pay for " + getName());

return salary/52;

// Remainder of class definition

Java Encapsulation
Encapsulation is one of the four fundamental OOP concepts. The other three
are inheritance, polymorphism, and abstraction.

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the
data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden
from other classes, and can be accessed only through the methods of their current class.
Therefore, it is also known as data hiding.

/* File name : EncapTest.java */

public class EncapTest {

private String name;

private String idNum;

private int age;

public int getAge() {

return age;

public String getName() {

return name;

public String getIdNum() {

return idNum;

} public void setAge( int newAge) {

age = newAge;

public void setName(String newName) {

name = newName;

public void setIdNum( String newId) {

idNum = newId;

The public setXXX() and getXXX() methods are the access points of the instance variables of
the EncapTest class. Normally, these methods are referred as getters and setters. Therefore, any
class that wants to access the variables should access them through these getters and setters.

Benefits of Encapsulation
• The fields of a class can be made read-only or write-only.
• A class can have total control over what is stored in its fields.

Java Encapsulation: Read-Only Class

A read-only class can have only getter methods to get the values of the attributes, there should
not be any setter method.

Example: Creating Read-Only Class

In this example, we defined a class Person with two getter methods getName() and getAge().
These methods can be used to get the values of attributes declared

as private in the class.

// Class "Person"

class Person {

private String name = "Robert";

private int age = 21;

// Getter methods

public String getName() {

return this.name;

public int getAge() {

return this.age;

public class Main {

public static void main(String args[]) {

// Object to Person class

Person per = new Person();

// Getting and printing the values

System.out.println("Name of the person is: " + per.getName()); System.out.println("Age of the person is:
" + per.getAge());
}

Java Encapsulation: Write-Only Class

A write-only class can have only setter methods to set the values of the attributes, there should
not be any getter method.

Example: Creating Write-Only Class

In this example, we defined a class Person with two setter methods setName() and setAge().
These methods can be used to set the values of attributes declared as private in the class.

// Class "Person"

class Person {

private String name;

private int age;

// Setter Methods

public void setName(String name) {

this.name = name;

public void setAge(int age) {

this.age = age;

} public class Main { public static void main(String args[]) {

// Object to Person class

Person per = new Person();

// Setting the values

per.setName("Robert");

per.setAge(21);
}

You might also like