Skip to main content

abstract class in java with example

An abstract class is a class, that is declared as abstract. Abstract class contains 0 or more abstract methods. Abstract classes can not be instantiated but that can be sub classed.

          An abstract method is a methods that is declared without any implementation. Like this,

1
2
3
abstract class Car {
   abstract void show();
}

          •  A class with abstract methods is called abstract class. The both abstract method and abstract class must be declared with abstract.
          •  In case where you want to use implementation inheritance then it is usually provided by an abstract base class. Abstract classes are excellent candidates inside of application frameworks.
          •  Abstract classes let you define some default behavior and force sub-classes to provide any specific behavior.
         
          Here is the abstract class example with abstract methods as well as concrete methods.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.javatbrains.abstractclass;

abstract class Car {
     // every car will have a regno;
     int regno;

     // to store regno value
     Car(int no) {
          regno = no;
     }

     /*
      * Every car will have a fuel tank. The mechanism to fill           the tank is same
      * for all cars
      */
     void fillTank() {
          System.out.println("Take the key and fill tank");
     }

     /*
      * Every car will have steering. But different cars will have different
      * steering mechanism
      */
     abstract void steering(int direction);

     /*
      * Every car will have breaks. But different cars will have different
      * breaking mechanism
      */
     abstract void breaking(int force);
}

          Here we will create another concrete class name called Maruti. This class is the sub class of the above abstract class Car.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package com.javatbrains.abstractclass;

class Maruti extends Car {

     Maruti(int no) {
          super(no);
     }

     @Override
     void steering(int direction) {
          System.out.println("Maruti uses manual steering. Please drive the car");
     }

     @Override
     void breaking(int force) {
          System.out
                   .println("Maruthi uses hydraulic breakes. Apply breakes to stop the car");
     }

}

          Here is the another concrete class name called Santro. This is also the sub class of the abstract class Car.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package com.javatbrains.abstractclass;

class Santro extends Car {

     Santro(int no) {
          super(no);
     }

     @Override
     void steering(int direction) {
          System.out.println("Santro uses power steering. Start the car");
     }

     @Override
     void breaking(int force) {
          System.out.println("Santro uses gas breakes. Apply breakes to stop the car");
     }

}

          Now, here we will write a class to all the above classes name called UseCar.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package com.javatbrains.abstractclass;

public class UseCar {

     public static void main(String[] args) {
          // Take new Maruti and Santro cars
          Maruti ma = new Maruti(1234);
          Santro sa = new Santro(007);
          // create a reference to car class
          Car car = ma; // (or) Car car = sa;
          // now use it
          car.fillTank();
          car.steering(10);
          car.breaking(20);
     }

}
OutPut:
     Take the key and fill tank
     Maruti uses manual steering. Please drive the car
     Maruthi uses hydraulic breakes. Apply breakes to stop the car

Key points of Abstract class:
           •  An abstract class is a class with 0 or more abstract methods
           •   An abstract class can have variables, concrete methods and abstract methods.
           •  we cannot create an object to abstract class. But we can create a reference variable to abstract class.
           •   All the abstract methods of the abstract class should be implemented in its sub class.
           •  If an abstract method is not implemented then the sub class should be declared as abstract.
           •   The reference of abstract super class can not refer to the individual methods of the sub class.
           •   The abstract class can not be both abstract and final.
           •   An abstract class must be sub classed.
           •  An abstract class will have constructors, and those constructors are always called when a concrete sub class is instantiated.
         
          If you want to read about the interface with examples. Click Here.

When should you use, abstract class or interfaces?
            •   Consider using abstract classes if any of these statements apply to your situation:
        1. You want to share code among with several closely related classes.
        2. You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public.
       3. You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
           •  Consider using interface, if any of these statements apply to your situation:
       1. You expect that unrelated classes would implements your interface. For example, interfaces comparable and cloneable are implements many unrelated classes.
       2. You want to specify the particular behaviour of a data type, but not concerned about who implements its behaviour.
       3. You want to take advantage of multiple inheritance of type.

Comments

Popular posts from this blog

JVM, JRE and JDK in Java

JVM, JRE and JDK are the most basic common concepts to know in java. These are the basic features to understand how Java architecture works? JVM stands for Java Virtual Machine, which doesn't have any physical directories created in java installation. JRE stands for Java Runtime Environment, which creates the directory under Java installation path and also present in JDK. JDK stands for Java Development Kit, which creates the directory in Java installation path and also it has it's own JRE. Since we have already learn that Java is platform independent means if we have implemented any of the java class in one environment, it will be executed in any other environment and provides the same output. But, JVM, JRE and JDK all are platform dependent . So that, for windows, linux, unix, mac, solaris..etc has it's own JVM, JRE and JDK. One will be not compatible with other environments. While installing the Java, we might come to know a bit about JRE and JDK. But, JVM is the other...

Prime, Fibonacci and Factorial number with example in java

Prime number, Fibonacci series and Factorial number programs are most commonly asked questions in interview. Read this article to know what is and how to write programs for prime number, fibonacci series and factorial number. Prime Number: prime number is natural number greater than 1 that has no positive divisor other than 1 and itself. A natural number greater than 1 is not a prime number, is called Composite number . For example, 7 is a prime number. Because it can divide with 1 and 7 only. Where as 8 is composite number. Since it has the divisor 2 and 4 in addition to the 1 and 8. The below example represents the finding the passing number is prime number or not. If the passing number is prime number it will print true otherwise it will print false. package com . javatbrains . practice ; public class PrimeNumber { public boolean isPrimeNumber ( int number ) { if ( number <= 1 ) return false ; // There's only one ...

How to insert images into database using JDBC?

How to insert images into database table by using JDBC? To know about these topic follow the below lines,           Before going to write a JDBC program for inserting images into database table, we need to create a specific table for inserting images column datatype. For creating the database table, execute the below command in MySQL command prompt. 1 2 3 4 CREATE TABLE ` images ` ( ` image_id ` int ( 11 ) NOT NULL AUTO_INCREMENT, ` image_flag ` int ( 11 ) NOT NULL , ` image ` longblob NOT NULL , PRIMARY KEY ( ` image_id ` ));           After executing the above query in MySQL command prompt, execute the below query to know weather the above table query is created or not. 1 Show tables;            When you have executed the above query that will show you the list of table which all exists in the current database. Current tab...