Skip to main content

Polymorphism and Method Overloading and Overriding- Example0008

Polymorphism and Method Overloading and Overriding- Example0008
This example of Polymorphism(One of the characteristics of the  - Both Compile and Runtime shown here.
Method Overloading (Compile time Polymirphism)- Two methods defined in both parent and child Classes with same name but without differnet arguments
Method Overriding (Run time Polymorphism) - Two methods defined with same name and same inputs, but output can be different
package com.swprogramdeveloper;

Keyword: java, polymorphism, method overloading, method overriding, compile polymorphism, runtime polymorphism, arguments

//Textual presentation of an object, how it will look like in the memory
//Whatever we write in class is actually belong to object. Note: if you want something for class, need to write static
class Product {
    //Attribute
        int pid;
        String name;
        int price;

        //Constructor
        Product() {
        System.out.println(">>Product Object Constructed");
        }

        //Methods
        //Method one to initialize data
         void setProductDetails(int pid, String name, int price){
         this.pid=pid;
         this.name=name;
         this.price=price;
         }

        //Method to display data
         void showProductDetails(){
             System.out.println("-----Product ID: "+pid+"----------");
             System.out.println("Name:\t"+name);
             System.out.println("Price:\t"+price);
             System.out.println("---------------------------");
         }

        }

//IS-A relation and extends
//Example without defining method overloading and Method overriding
 class Mobile extends Product {
    // Constructor
    Mobile() {
        System.out.println("--Mobile Constructor created---");
    }
}

//IS-A relation and extends example with additional attribute for the Child Product
//Example with defining method overloading and Method overriding
class MobileA extends Product {
     // Additional attribute
    String os;
    int ram;

    // Constructor
    MobileA() {
        System.out.println("--MobileA Constructor created---");
    }

        // Method one to initialize data
        // Method name same as that of Parent Class with additional inputs
        // This is Method Overloading - Compile time Polymorphism, also called Static Polymorphism

        void setProductDetails(int pid, String name, int price, String os, int ram){
            this.pid=pid;
            this.name=name;
            this.price=price;
            this.os=os;
            this.ram=ram;
        }

    // Method one to display data
    // Method name same as that of Parent Class - Input is same, Output is different
    // This is Method Overriding - Run time Polymorphism
    void showProductDetails(){
        System.out.println("-----Product ID: "+pid+"----------");
        System.out.println("Name:\t"+name);
        System.out.println("Price:\t"+price);
        System.out.println("OS:\t" + os);
        System.out.println("RAM:\t" + ram);
        System.out.println("---------------------------");
    }

    }


public class Main {

    public static void main(String[] args) {
// write your code here

        // Creating class from extended class
        System.out.println("---Output of extended mobile class from product----");
        //without method overloading or overriding
        Mobile mobile1 = new Mobile();

        mobile1.setProductDetails(980,"mi note2",23000);
        mobile1.showProductDetails();

        //example- method overloading and method overriding
        MobileA mobile1A = new MobileA();

        mobile1A.setProductDetails(980,"mi note2",23000,"Android",128);
        mobile1A.showProductDetails();


    }
}



Output0008
==========

---Output of extended mobile class from product----
>>Product Object Constructed
--Mobile Constructor created---
-----Product ID: 980----------
Name: mi note2
Price: 23000
---------------------------
>>Product Object Constructed
--MobileA Constructor created---
-----Product ID: 980----------
Name: mi note2
Price: 23000
OS: Android
RAM: 128
---------------------------

Process finished with exit code 0

Comments

Popular posts from this blog

Basic Java Program Various Variables and Point - Example0002

Basic Java Program Various Variables and Point - Example 0002 This example is same as Example1 but with added Class Point Using Point - (It is to store x and y coordinates of a point) This is example of a basic java program which shows structure of the program. It is basic java program showing different type of Java variables. Primitive types: Integer, float, double, Boolean are shown Creating an object. Reference types and methods to that object. How to print output in a screen is shown using System Class package com.swprogramdeveloper; import java.awt.*; import java.util.Date; public class Main {     public static void main(String[] args) {         // write your code here         //variable types double and intege r         double age = 30;         int herage = (int) age + 20;         //Printing output using System Class         Sy...

Creating Class with attributes and method and using them- Example0006

Creating Class with attributes and method and using them- Example0006 This example shows creating own Class We will create a class called 'Product' with Attribute, Contructor and Methods We will see how we can create an Object of that Class and use the methods in that Class.  We can see two ways to access the attributes of the Class Keyword: java, class, attribute, constructor, methods, object, instance Creating own Class and use it ====================== package com.swprogramdeveloper; //Textual presentation of an object, how it will look like in the memory //Whatever we write in class is actually belong to object. Note: if you want something for class, need to write static class Product {     //Attribute         int pid;         String name;         int price;         //Constructor         Product() {         System.out.printl...

Basic Java Program with Variable Introduction and Class Introduction - Example0001

Basic Java Program with Variable Introduction and Class Introduction - Example0001  Example1: ========= This is example of a basic java program which shows structure of the program. It is basic java program showing different type of Java variables. Primitive types: Integer, float, double, Boolean are shown Creating an object. Reference types and methods to that object. How to print output in a screen is shown using System Class package com.swprogramdeveloper; import java.util.Date; public class Main {     public static void main(String[] args) {  // write your code here      //variable types double and integer         double age = 30;         int herage = (int) age + 20;   //Printing output using System Class         System.out.println("Hello World "+age + "---" + herage);      //float type. Note the F- it is required as the compiler will take doub...