What does abstract java class means?
- It is one of the fundamental feature of Object Oriented Programming language
- It is super class and cannot be instantiated
- It is used to state and define general characteristic
- An object cannot be created using abstract class if trying to instantiated; it produces compile time error
- The abstract class declares using “abstract” java keyword
- An abstract class may be abstract method, member variables and concrete methods
- It helps reduce the complexity and also improve the maintainability of the system
Abstract java class Characteristic:
You cannot create the abstract java class instance. Let see
in example mentioned below
public abstract class AbsExample {
public String name;
public void display(String
name){
this.name = name;
}
public static void main(String[]
args) {
AbsExample
abs = new AbsExample(); //
Cannot instantiate the type AbsExample
}
}
We can access the abstract class using its sub class only
and we can create n number of subclass for single abstract class. Once
subclasses extend to any parent java class then it cannot be extend by any
other parent java class.
public abstract class AbsExample{
public String name;
public void display(String
name){
this.name = name;
System.out.println(this.name);
}
}
Create a subclass which extend to parent java class
public class SubChildAbs extends AbsExample{
public static void main(String[]
args) {
AbsExample
absExample = new SubChildAbs();
absExample.display("Hello
Abstract Class");
}
}
Output: “Hello Abstract Class”
An abstract method is a method that can declare in abstract
class without implementation as method in below example
abstract int calculation(int a,int b);
If any class includes abstract method, then class itself
declared as Abstract class. Otherwise it will give you below compile time error
public class HelloWorld
{
//The type HelloWorld must be
an abstract class to define abstract methods
void display(){
System.out.println("Display
Hello World");
}
//The abstract method
helloworldMessage in type HelloWorld can only be defined by an abstract class
abstract void helloworldMessage();
}
Abstraction Examples:
In simply word, I can say that abstraction is used to hiding
the implementation or system details behind the simple interface.
Let’s take an example of CAR
We can see the color, modal, look, seating capacity, space,
gear but you cannot see the internal mechanism of car like how engine work?
Another example: Serializable interface
As we know that serializable interface as known as marker
interface and which has no method defined in it but when we implement serializable
interface so it serialized the object. So here seriablable interface is
abstract layer for us but internally JVM serialize the object. (Hiding the
implementation or system details behind the simple interface.)
Advantage of abstract class:
- We can separate the things and grouped to another type
- It Simplifies the representation of domain modal
- We can separate in group the things which frequently changes their properties and methods so main group type need not undergo changes
Summary:
It is one of the main features of OOPs fundamental which
helps to reduce the complexity and also improve the maintainability of the
system. It used to state and define general characteristic.
No comments:
Post a Comment