Professional Documents
Culture Documents
LECTURE FIVE
POLYMOPHISM, ABSTRATION,ENCAPSULATION,INHERITANCE,
PACKAGE AND AGGREGATION
Abstract Method
• 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.
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.
NB: You have to write the full functionality of this method in another non-abstract class, taking the same method
name. See above example
class EncapsulationDemo{
private int ssn; private String empName; private int empAge;
public int getEmpSSN () { //Getter and Setter methods
return ssn; }
public String getEmpName () {
return empName; }
public int getEmpAge () {
return empAge; }
public void setEmpAge (int newValue) {
empAge = newValue; }
public void setEmpName (String newValue) {
empName = newValue; }
public void setEmpSSN (int newValue) {
ssn = newValue; }
}
public class EncapsTest{ public static void main(String args[]){
EncapsulationDemo obj = new EncapsulationDemo();
obj.setEmpName("Mario");
obj.setEmpAge(32);
obj.setEmpSSN(112233);
System.out.println("Employee Name: " + obj.getEmpName());
System.out.println("Employee SSN: " + obj.getEmpSSN());
System.out.println("Employee Age: " + obj.getEmpAge());
}}