Quantcast
Channel: Oracle Fusion Apps | Oracle Fusion | Oracle Apps Training
Viewing all articles
Browse latest Browse all 103

Abstract Class in JAVA

$
0
0

Objective:

In the previous article we studied about Overloading, Inheritance and Polymorphesim in Java. In this article we will see the detailed description of Abstract class and method in Java.

Abstract class:

It is a type of class but this class is not full implemented class because it is not with abstract keyword and it will have 0 to n abstract class and it can also have concrete methods.

Abstract class are also known as Non-concrete methods.

Concrete methods:

It cant create instance of abstract class.

We can create only refrences.

We cant apply static and final keyword to abstract methods but we can apply static and final keywords to non- abstract methods.

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, then the class itself must be declared abstract, as in:

public abstract class GraphicObject {
  // declare fields
  // declare nonabstract methods
  abstract void draw();
}
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

abstract class GraphicObject {
   int x, y;
   ...
   void moveTo(int newX, int newY) {
       ...
   }
   abstract void draw();
   abstract void resize();
}

Each nonabstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods:

class Circle extends GraphicObject {
   void draw() {
       ...
   }
   void resize() {
       ...
   }
}
class Rectangle extends GraphicObject {
   void draw() {
       ...
   }
   void resize() {
       ...
   }
}

 

Example:

abstract class Bike{

  abstract void run();

}

 

class Honda4 extends Bike{

void run(){System.out.println("running safely..");}

 

public static void main(String args[]){

 Bike obj = new Honda4();

 obj.run();

}

}

 

//output:

compiled by: javacHonda4.java

Runby: javaHonda4

running safely..

 

Abstract keyword is used to make a class abstract.

Abstract class can’t be instantiated.

We can use abstract keyword to create an abstract method, an abstract method doesn’t have body.

If a class have abstract methods, then the class also needs to be made abstract using abstract keyword, else it will not compile.

Its not necessary to have abstract classes to have abstract method.

If abstract class doesn’t have any method implementation, its better to use interface because java doesn’t support multiple class inheritance.

The subclass of abstract class must implement all the abstract methods unless the subclass is also an abstract class.

All the methods in an interface are implicitly abstract unless the interface methods are static or default. Static methods and default methods in interfaces are added in Java 8, for more details read Java 8 interface changes.

Abstract classes can implement interfaces without even providing the implementation of interface methods.

Abstract classes are used to provide common method implementation to all the subclasses or to provide default implementation.

We can run abstract class like any other class if it has main() method.

 

e1.png

 

e2.png

 

e4.png

 

e6.png



//OUTPUT:

e3.png


Viewing all articles
Browse latest Browse all 103

Trending Articles