Learn to implement polymorphism, inheritance, abstract methods, overriding and overloading in java

4
35182

In this tutorial we learn about how to implement interface, inheritance, abstract types, abstract methods, method overloading and method overriding using polymorphism also have brief look on dynamic binding using polymorphism.

  • To learn to implement polymorphism, inheritance, abstract methods, overriding and overloading in java follow the steps given below :

 

    1. Interface : – Interface is nothing but the collection of constant values and method definitions without implementations. In this case using an interface you can capture similarities between unrelated classes without forcing relationship between classes. A class can access only from one class but it can implement more than one interface. If we define a new interface you are defining a new reference data type. Functions of interface should be public and abstract. A class implementing an interface must use the keyword implements. Object of an interface cannot be created. Field of an interface must be final and public. Interface cannot be instantiated because interface does not have constructors.


Java Programming Course for Beginner From Scratch

Example : This example shows how an interface is used in a class.

Output :

 

    1. Inheritance, Polymorphism and Abstract Types : –

 

      • Inheritance : – It is allows a class to inherit the properties of another class. When a class extends all the properties of another class it means that class inherits all non-private variables including methods and fields. It defines a “is-a” kind of relationship between Super class (Parent class) and Sub class (child class) in java language. To describe inheritance implements and extends keywords are used. It is helpful for reusability of code and purpose is too used for method overriding. The inheritance concept is also known as reusability classes.Types of Inheritance:

 

        1. Single Inheritance : – When one class inherit the properties of another one class only then it is called as single inheritance.

Example : This example shows how single inheritance is used in a class.

Output :

 

        1. Multilevel Inheritance : – The use of a derived class as a super class is common requirement in object-oriented programming. Java uses it extensively in building its class library and java also supports this concept. It allows us to build a chain of classes.

Example : This example shows how multilevel inheritance is used in a class

Output :

 

        1. Hierarchical Inheritance : – It supports to the hierarchical design of a program. Many programming problems can be cast into a hierarchy where certain features of one level are shared by many others below the level. It is one-to-many relationship.


Ultimate Java Development and Certification Course

Example : This example shows how hierarchical inheritance is used in a class.

Output :

 

        1. Multiple Inheritance : – Java does not support multiple inheritances. So that reason java provides an alternate approach is called as interfaces, it supports the concept of multiple inheritances. Although a Java class does not be a subclass of more than one superclass, it can implement more than one interface without creating any problem.

        1. Hybrid Inheritance : – This is the combination of single inheritance and multiple inheritance. When hybrid inheritance supports multiple inheritance then hybrid inheritance not allowed.

      • Polymorphism : It is nothing but the ability to take more than one form. This can be applied to both operations as well as objects. It is tightly coupled inheritance. It means one interface, many possible implementations.

Two types of Polymorphism:

        1. Static Polymorphism: It is achieved through function overloading and operator overloading. It is always faster. It is also called as compile time polymorphism. Example of static polymorphism is method overriding using final or private methods. At the compilation time java knows which method is call by checking the arguments, so it is also known as early binding or static binding.

Types of static polymorphism :

          • Function Overloading: It is nothing but the ability of one function performs different tasks. These functions must differ by the data types. To call function the same function name is used for various instances.

Example : This example shows how function overloading is used in a java.

 

 

          • Operator Overloading : Java does not support operator overloading.

 

 

        1. Dynamic Polymorphism : It is also called as run-time polymorphism. In this case java compiler does not know which method is invoked at compilation time. Just JVM decides which method is invoked at the run-time. Method overriding is example of run-time polymorphism. In this case overridden method is invoking through the super class reference variable.

Types of Dynamic Polymorphism in java :

          • Virtual Function: This is nothing but the function whose performance can be overridden within an inheriting class by a function with the same argument or signature. Virtual function cannot be declared as private. In this function we get warning if we do not use Virtual or New keyword. You can use new keyword rather than Virtual.

Example : This example shows how dynamic polymorphism is used in a java.

Output :

 

 

      • Abstract : – When a class contains one or more abstract methods, then it should be declared abstract. You must use abstract keyword to make a class abstract. We cannot use abstract classes to instantiate objects directly. It needs to be extended and its method needs to be implemented. The abstract methods of an abstract class must be defined in its subclass. You cannot declare abstract constructors or abstract static methods. Abstract class have both method abstract as well as non-abstract methods. It also has a member variables and constructors.

Example : This example shows how abstract is used in a java.

Output :

 

 

    1. Overriding, Overloading and Abstract Methods : –

 

      • Method Overriding : Method overriding is nothing but the method in the child class should have the same name, same signature and parameters as the one in its parent class and also have the same return type. If a method declared final then it cannot be overridden. If a method declared static then it cannot be overridden but it can be re-declared. If a method cannot be inherited, then it cannot be overriden. It is used for runtime polymorphism. It must be is-a relationship. Polymorphism is applied on method overriding. It is a run-time concept. Abstract methods must be overridden. Constructors cannot be overridden. Dynamic binding is used for method overriding. Private and final method cannot be overridden.

Example : This example shows how method overriding is used in a java.

Output :

 

      • Method Overloading : It is nothing but in the same class, if name of the method remains same but the number and type of arguments or parameters are different, then it is called as method overloading. This concept is used for compile-time. Present in the same class. And can have different return types. It helps in maintain consistency in method naming, doing same task with different parameter. It helps to reduce overhead. It is also known as static polymorphism. Static method can be overloaded. Static binding is used for method overloading. It gives better performance than method overriding. Private and final methods can be overloaded. In method overloading return type should be same as the other methods of the same name. In method overloading argument list should be different.

Example : This example shows how method overloading is used in a java.

Output :

 

      • Abstract Method : Abstract method does not have any body. It is always ends with (;) semicolon. Abstract method must be overridden. It must be in an abstract class. It can never be static and final. Abstract methods are those which need to be implemented in subclass. If class has one abstract method then whole class is declared as abstract. Private method cannot be abstract.

Example : This example shows how abstract method is used in a class.

Output :

 

 

    1. Dynamic Binding : – In java dynamic binding occurs during runtime. Dynamic binding uses object to resolve binding. It is also known as late binding. Only virtual methods are resolved using dynamic binding. True polymorphism is achieved using dynamic binding.

Example : This example shows how dynamic binding is used in a java.

Output :

 

  • Hence, we successfully learnt how to implement interface, inheritance, abstract types, abstract methods, method overloading, method overriding and dynamic binding using polymorphism in java.

4 COMMENTS

  1. There are ways that you can break the portability of a Java program. You could write a program that looks for files only found on one operating system (cmd.exe for example). You could use JNI, which effectively allows you to put compiled C or C++ code into a class. You could use conventions that only work for a certain operating system (like assuming “:” separates directories).

LEAVE A REPLY

Please enter your comment!
Please enter your name here