Friday, 22 January 2016

Abstract classes

A class which is declared with abstract keyword is known as abstract class. Abstract means hidden and in java abstract classes have abstract and non abstract methods. Also java supports abstraction, it is a process of hiding the implementation details and showing only functionality to the user.

There are two ways of abstraction implementation in java and they are
  • abstract class
  • interface
Abstract Method:
A method that is declared as abstract and does not have implementation part is called abstract method

abstract void samplemethod(); // no method body

abstract class and abstract method

abstract class Car{
  abstract void run();
class Maruti extends Car{
void run(){System.out.println("running safely..");} 
public static void main(String args[]){
 Car obj = new Maruti();;

running safely..

Another example:

abstract class Cars{
abstract void drive();
class Honda extends Cars{
 void drive(){System.out.println("driving Honda");}
class Hyundai extends Cars{
 void drive(){System.out.println("driving Hyundai");}
  //method is called by programmer or user 
class AbstractionSample{
 public static void main(String args[]){
 Cars s=new Hyundai();;

driving Hyundai

No comments :

Post a Comment