What is abstraction ?
Abstraction
is the process of recognizing and focusing on important characteristics
of a object and leaving/filtering out the un-wanted characteristics of
that object.
Abstraction in Object Oriented Programming helps to hide the irrelevant details of an object.
An
abstract class is one that cannot be instantiated. All other
functionality of the class still exists, and its fields, methods, and
constructors are all accessed in the same manner. You just cannot create
an instance of the abstract class.
Abstract Class:
Abstract Class:
Use the abstract keyword to declare a class abstract. The keyword appears in the class declaration before the class keyword.
Example:
*************************************************************
public abstract class AbstractionSample1 {
public void getName() {
System.out.println("name of employee");
}
public void getAddress() {
System.out.println("address of an employee");
}
public static void main(String args[]) {
// AbstractionSample1 abstractionSample1 = new AbstractionSample1();//
// it does not allow because it is abstract class.So inorder to use
// methods of an abstract class we have to extend this class.
}
}
************************************************************
public class AbstractClassDemo extends AbstractionSample1 {
public void getSalary() {
System.out.println("salary of an employee");
}
public static void main(String h[]) {
// we can access the methods of and abstract class as following. Here
// abstractSample1 is reference of abstract class but initialized with
// instance of AbstractClassDemo.
AbstractionSample1 abstractionSample1 = new AbstractClassDemo();
abstractionSample1.getAddress();
abstractionSample1.getName();
// Directly we can access with AbstractClassDemo reference as follows.
// Beacuse AbstractClassDemo is subclass of AbstractionSample1, we can
// acess methods of AbstractionSample1.
AbstractClassDemo abstractClassDemo = new AbstractClassDemo();
abstractClassDemo.getSalary();
abstractClassDemo.getAddress();
abstractClassDemo.getName();
}
************************************************************
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 abstract.
The
abstract keyword is also used to declare a method as abstract.An
abstract methods consist of a method signature, but no method body.
Abstract method would have no definition, and its signature is followed by a semicolon, not curly braces as follows:
public abstract class AbstractClassSample2 {
public abstract void getEmpDetails(); //
we need add keyword abstract for method as well as class also because
any class which consists of abstract method has becomes abstract class.
}
***********************************************************
public class AbstractClassDemo2 extends AbstractClassSample2 {
@Override
/**
* If any class which extends the abstract class,
* then it should implement the all abstract methods.
* So here we have implement the getEmpDetails() method.
*/
public void getEmpDetails() {
System.out.println("get employee details");
}
public static void main(String h[]) {
// AbstractClassSample2 abstractClassSample2 = new
// AbstractClassSample2();// does not allow initation of abstract class
AbstractClassSample2 abstractClassSample2 = new AbstractClassDemo2();
abstractClassSample2.getEmpDetails();
}
}
************************************************************
No comments:
Post a Comment