Abstraction and Polymorphism in Java

Abstraction:

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 flowkl;
  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.

Polymorphism:

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 flowkl;
  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. }

OUTPUT:

  1. car has four wheels


ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Help Dad unlock new skills this summer in just $10 on Udemy

Development Category (English)300x250

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Best Video Courses