Method Overriding

******************** Demonstrating method Overloading *****************************

CLASS 1


package overridingclasses;

/**
 *MobileClass.java
 * @author SHASHI
super class 
 here method and constructor is defined

 */
public class MobileClass {
    
  //variable declaration
    String Manufacture;
    int cost;
    
//constructor to set value
    public MobileClass(String manu, int price) {
        this.Manufacture= manu;          // this to assign value to variable with constructor input
        this.cost= price;
    }
    
   String getModel(){                                 // created method to display value from constructor domain
       System.out.println("Method of mobile class");
       return Manufacture;
    }
    
}


*********************************** *********************************************
CLASS 2


package overridingclasses;

/**
 *AndroidClass.java
 * @author SHASHI
 */
public class AndroidClass extends MobileClass {             //Class extending to super class  

    AndroidClass(String manu, int cost) {                   // constructor to take input common to both class
        super(manu, cost);                                         //super to call value from super class
    }

    String getModel() {                                                //method to do calculation
        System.out.println("method of android class");
        return Manufacture;
    }
}


***************************************************************************8
CLASS 3


package overridingclasses;

/**
 *OverRidingClasses.java
 * @author SHASHI

main class performing overriding  of methods
 */
public class OverRidingClasses {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         MobileClass ob1 = new AndroidClass("Huwai", 1987);  // created object of android class 
        System.out.println(ob1.getModel());         // called common method but it display only subclass method this is overriding concept.
    }

}


********************************************************************************
OUTPUT
Method of mobile class






















No comments:

Post a Comment