Thursday, 4 February 2016

What is Abstract in java:

What is Abstract in java:
===================

Abstract is a keyword applicable for methods,classes

If we dont know the implementaion of a method just we have a requirement then declare a method as abstract.
syntax:
--------
abstract access specifier void methodname();
ex:
abstract public void hello();

Every abstract method ends with semicolon and it doesnt have any method body.it contains only declarations.

If your class is having atleast one abstract method compulsory you should declare that class as abstract.

When ever we declare class as abstract we can not create or instantiate the object.


Child classes are responsible to implement abstract methods.
If child classes are also  not providing implementation then we need to declare that class as abstract

conclusions:
--------------
1.If your class is having atleast one abstract method,compulsory you need to declare that class abstract otherwise we are gooing to get compile time error.
2.If you dont want create a object then declare that class as abstract
3.As it is a abstract class we can not create object for that class

Example on abstract
--------------------
package blog;

abstract class Hello
{

public void test1() {

System.out.println("test1.....");
}

abstract public void test3();//abstract method
}

class Display extends Hello
{

@Override
public void test3() {
System.out.println("abstract class implemented method");

}

public void test2() {
System.out.println("test2......");
}


}

public class AbstractExample {
public static void main(String[] args) {

Display display = new Display();
display.test1();
display.test2();
display.test3();

//we can not create object as it is abstract class
//Hello hello = new Hello();
}
}


3 comments: