Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Overloading' - 2 code snippet(s) found

 Sample 1. Tricky Code for Overloading and Overriding

class BuggyBread1 {
public String method() {
return "Base Class - BuggyBread1";
}
}

class BuggyBread2 extends BuggyBread1{

private static int counter = 0;

public String method(int x) {
return "Derived Class - BuggyBread2";
}

public static void main(String[] args) {
BuggyBread1 bg = new BuggyBread2();
System.out.println(bg.method()); // prints Base Class - BuggyBread1
}
}

   Like      Feedback     overloading   overriding   tricky code examples  inheritance


 Sample 2. Constructor Overloading

public class Employee {
public String name;
public int age;
public int salary;

// No Argument Constructor
Employee(){
    this.name = "";
    this.age = 0;
    this.salary = 0;
}

// Single Argument Constructor
Employee(String name){
    this.name = name;
    this.age = 0;
    this.salary = 0;
}

// Argument Constructor
Employee(String name,int age){
    this.name = name;
    this.age = age;
    this.salary = 0;
}

// Argument Constructor
Employee(String name,int age,int salary){
    this.name = name;
    this.age = age;
    this.salary = salary;
}
}

   Like      Feedback     constructor  constructor overloading



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus