Overloading - Functions

Run Settings
LanguageJava
Language Version
Run Command
class Main { public static void main(String[] args) { //System.out.println("Hello World!"); //Instance of Data Class //As u can see -> we have 3 instance of Data class and our COUNTER exactly gives us the same number, //since we increase it the COUNTER also increase. Data data = new Data(2343,"Edimarf Satumbo", 1997, 2020); Data data1 = new Data(3345,"Aderito Claudio", 1997, 2020); Data data2 = new Data(4555,"Antonio Cambola", 1996, 2020); //We call the allData() -> to get our data displayed on the Screen //System.out.println(data.allData()); //Age calculation gonna be called here // System.out.println("Your current age is : " + " " + data.ageCalculation()); System.out.println(data.allData() + "\n" + "Your current age is : " + " " + data.ageCalculation() +"\n" + "Future age :" + data.ageCalculation(12) + "\n"); System.out.println(data1.allData() + "\n" + "Your current age is : " + " " + data1.ageCalculation() +"\n" + "Future age :" + data1.ageCalculation(12) + "\n"); System.out.println(data2.allData() + "\n" + "Your current age is : " + " " + data2.ageCalculation() +"\n" + "Future age :" + data2.ageCalculation(12) + "\n"); //System.out.println("Your current age is : " + " " + data2.ageCalculation()); } }
//Hope you enjoy guys class Data { //Data is a simple class which takes ID NAME AND COUNTER //They are stored and displayed -> No setters and getters PS: Just a simple class to check the overload concept private int ID; private String NAME; private int BOR_YEAR; private int CURRENT_YEAR; private static int COUNTER = 0; public Data() { System.out.println("Data class -> constructor"); } public Data(int ID, String NAME, int BOR_YEAR, int CURRENT_YEAR) { this.ID = ID; this.NAME = NAME; this.BOR_YEAR = BOR_YEAR; this.CURRENT_YEAR = CURRENT_YEAR; //Static variable -> will count how many times this class will be instanciate this.COUNTER +=1; //System.out.println("The number of instance of this class : " + Data.COUNTER); } //Will print the all data on the Screen //here we are showing all the data from our class on the Screen //COUNTER -> It means the number of instance of this class is created public String allData() { //System.out.println(Data.COUNTER); return "NAME : " + NAME + ", " + "YOUR ID : " + ID + ", " + "Born Year: " + BOR_YEAR + "," + " Current Year: " + CURRENT_YEAR; } //Here comes the Overloading Part public int ageCalculation() { int result = CURRENT_YEAR - BOR_YEAR; return result; } //your age from now to 20 years or the year that u prefare; public int ageCalculation(int numb) { int result = (CURRENT_YEAR - BOR_YEAR) + numb; return result; } }
Editor Settings
Theme
Key bindings
Full width
Lines