Abstraction and Polymorphism in Java


Sometimes we need not to use super class or we want that some particular method must be defined in the sub class then we use abstract classes and methods. In abstract class, we have to leave atleast one incomplete method. This method is called abstract and defined by writing abstract before method.

  1. package findalltogether;
  2. //defining abstract class
  3. abstract class AbsClass
  4. {
  5. // abstract method, no body for this method
  6. public abstract void method();
  7. }
  8. class SubClass extends AbsClass
  9. {
  10. // Definition of abstract method in sub class
  11. // body is here
  12. public void method()
  13. {
  14. System.out.println("method is completed by sub class");
  15. }
  16. }
  17. public class Tutorials
  18. {
  19. public static void main(String args[])
  20. {
  21. //object of sub class
  22. SubClass object = new SubClass();
  23. object.method();
  24. }
  25. }

Properties of abstraction:

  1. Class is defined with keyword abstract.
  2. At least one method must be abstract. Abstract method is defined by starting with keyword abstract and removing body. Abstract method is end with semicolon.
  3. Abstract method can be defined only in abstract class.
  4. An abstract class may or may not has any abstract method.
  5. Sub class must define that method, otherwise sub class must also be an abstract class.
  6. We can not make any object of an abstract class, i.e. we can not use it. An abstract class can only be extended.


I will start with two sentences:

  1. Car has four wheels.
  2. Car is a four wheeler.

Till now we have called classes by creating objects. Class (which is calling) has those objects of other classes. It is called 'has-a' relationship. But in inheritance we used to inherit properties of other class directly. Sub class has not super class, but sub class is itself super class. It is 'is-a' relationship. So we can call a sub class by giving reference of super class. In other words, we can say that sub class is an instance of super class.

  1. package findalltogether;
  2. class FourWheeler
  3. {
  4. public void wheel()
  5. {
  6. System.out.println("I have 4 wheels");
  7. }
  8. }
  9. class Car extends FourWheeler
  10. {
  11. public void wheel()
  12. {
  13. System.out.println("car has four wheels");
  14. }
  15. }
  16. public class Tutorials
  17. {
  18. public static void main(String args[])
  19. {
  20. // object of Car class with reference of FourWheeler
  21. // Car is a FourWheeler
  22. FourWheeler obj = new Car();
  23. obj.wheel();
  24. // we can not write
  25. // Car obj2 = new FourWheeler();
  26. // we can do upper casting, not lower
  27. }
  28. }


  1. car has four wheels


About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Java is object oriented and platform independent programming language which runs on JVM. Lets see advantages of java ...
Introduction to Java
In this tutorial, we will learn how to give/ take input/ output to console. What are classes, main() and comments? Wh...
First program in Java
Variable is a memory location which stores data. In Java, we have to define what type of variable (integer, text, str...
Variables and data types in Java

Login or Sign up to leave comment.