Java Abstraction

 Abstraction:

         Abstraction means displaying only essential information and hiding the details.

       Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.

Abstract Methods and Classes

A method without a body (no implementation) is known as an abstract method.

 A method must always be declared in an abstract class, in other words, you can say that if a class has an abstract method, it should be declared abstract as well. 

abstract class shape

{

   abstract void draw();

}

 we cannot create objects for abstract classes.

Example 01:

//Abstract Class in Java Programming

abstract class Shape

{

abstract void draw();


void message()

{

System.out.println("Message From Shape");

}

}


class rectangleShape extends Shape

{

@Override

void draw() {

System.out.println("Draw Rectangle Using Length & Breadth..");

}

}

public class abstractDemo {


public static void main(String args[]) {

rectangleShape o =new rectangleShape();

o.draw();

o.message();

}

}

/*An abstract class is a class marked with the abstract keyword.

* It, contrary to non-abstract class, may contain abstract - implementation-less - methods.

* It is, however, valid to create an abstract class without abstract methods.


An abstract class cannot be instantiated. It can be sub-classed (extended) as long as the

sub-class is either also abstract, or implements all methods marked as abstract by super classes.


Abstraction can be achieved with either abstract classes or interfaces .

Abstract class must have one abstract method.

We can’t create the object using an abstract class.

Abstract class can have abstract and non-abstract methods.

*/

Post a Comment

0 Comments