Abstract Class in Java.pdf1
Abstract Class in Java.pdf1
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
this.name = name;
this.address = address;
this.number = number; }
return 0.0; }
public void mailCheck() { System.out.println("Mailing a check to " + this.name + " " + this.address);
}
public String toString() { return name + " " + address + " " + number; }
{ address = newAddress; }
public int getNumber() { return number;
// Annual salary
public Salary(String name, String address, int number, double salary) { super(name, address, number);
setSalary(salary); }
return salary/52; }
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();
this.name = name;
this.address = address;
this.number = number;
return 0.0;
return name;
return address;
}
public void setAddress(String newAddress) {
address = newAddress; }
return number;
// Annual salary
setSalary(salary); }
return salary/52;
this.name = name;
this.address = address;
this.number = number;
return name;
this.name = name;
return address;
return number;
this.number = number;
// Annual salary
this.salary = salary;
return salary/52;
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.
return age;
return name;
return idNum;
age = newAge;
name = newName;
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.
A read-only class can have only getter methods to get the values of the attributes, there should
not be any setter method.
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
// Class "Person"
class Person {
// Getter methods
return this.name;
return this.age;
System.out.println("Name of the person is: " + per.getName()); System.out.println("Age of the person is:
" + per.getAge());
}
A write-only class can have only setter methods to set the values of the attributes, there should
not be any getter method.
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 {
// Setter Methods
this.name = name;
this.age = age;
per.setName("Robert");
per.setAge(21);
}