What is Method Overloading?

Method Overloading:

One or more methods  in a class having same name, but different number or type of arguments, is called method overloading.

When a overloaded method is called, the method with matching number and type of arguments gets executed.

If there is no exact match of argument types, type promotion is applied, before invoking the method.

For eg

class Abc{

void add(int i, int j){  //body of method }

void add(float i, float j, float k){ //body of method }

void add(int i,int j, int k){ //body of method }

}

NOTE:

1.Method overloading cannot be done, based only on return type of the method

2.Method overloading is compile time polymorphism, as the method which need to get called is decided/binded during compile time itself.

Question:

Can below methods be overloaded??

class Abc{

void add(int i, int j){  //body of method }

int add(int a, int b){ //body of method }

}

Sample Project interview Questions

Few of my students and other have been following up with me to provide list of commonly asked Project related Questions by Interviewer. Below are few of them I can think of

  1. Which are the Languages/Technologies used to implement your Project
  2. Which Software Model has been used
  3. Duration of Project, and your Role and Contribution to Project
  4. Which Configuration Management Tool was used for your Project?
  5. How many modules are there, and on which Modules you worked?
  6. Architecture diagram of your Project?
  7. Which Design Patterns are applied? and Which Frameworks were used?
  8. Who is the End user of your Project?
  9. What are the main Challenges you faced, during your Project?
  10. How many Lines of Code you developed?
  11. What are various Testings performed?
  12. Which Bug Tracking Tool was used?
  13. Have you applied MVC for your Project?

Please visit back for updates…

My Quora Profile

Purpose of Composite Design Pattern

"Compose objects into tree structure to represent part-whole hierarchies. 

Composite lets client treat individual objects and compositions of objects uniformly".

Composite design pattern treats each node in two ways-Composite or leaf. 
Composite means it can have other objects below it. Leaf does not have objects below it

Component
declares interface for objects in composition.
implements default behavior for the interface common to all classes as appropriate.
declares an interface for accessing and managing its child components.
Leaf
represents leaf objects in the composition. A leaf has no children.
defines behavior for primitive objects in the composition.
Composite
defines behavior for components having children.
stores child components.
implements child related operations in the component interface.
Client
manipulates objects in the composition through the component interface.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


interface Employee {

 public void add(Employee employee) throws Exception;
 public void remove(Employee employee);
 public Employee getChild(int i);
 public String getName();
 public double getSalary();
 public String getDesignation();
 public void print();
}


class Director implements Employee{

 private String name;
 private double salary;

 public Director(String name,double salary){
 this.name = name;
 this.salary = salary;
 }

 List<Employee> employees = new ArrayList<Employee>();
 public void add(Employee obj) throws Exception{
 String c_name = obj.getClass().getName();
 if(c_name.equalsIgnoreCase("Manager"))
 {
 employees.add(obj);
 }
 else
 {
 throw new Exception("non Manager not allowed to add");
 }
 
 }

 public Employee getChild(int i) {
 return employees.get(i);
 }

 public String getName() {
 return name;
 }

 public double getSalary() {
 return salary;
 }

 public String getDesignation(){
 return "Director";
 }
 public void print() {
 System.out.println("-------------");
 System.out.println("Name ="+getName());
 System.out.println("Salary ="+getSalary());
 System.out.println("-------------");

 Iterator<Employee> employeeIterator = employees.iterator();
 while(employeeIterator.hasNext()){
 Employee employee = employeeIterator.next();
 employee.print();
 }
 }

 public void remove(Employee employee) {
 employees.remove(employee);
 }
 }


class Manager implements Employee{

 private String name;
 private double salary;

 public Manager(String name,double salary){
 this.name = name;
 this.salary = salary;
 }

 List<Employee> employees = new ArrayList<Employee>();
 public void add(Employee employee) {
 employees.add(employee);
 }

 public Employee getChild(int i) {
 return employees.get(i);
 }

 public String getName() {
 return name;
 }

 public double getSalary() {
 return salary;
 }

 public String getDesignation(){
 return "Manager";
 }
 public void print() {
 System.out.println("-------------");
 System.out.println("Name ="+getName());
 System.out.println("Salary ="+getSalary());
 System.out.println("-------------");

 Iterator<Employee> employeeIterator = employees.iterator();
 while(employeeIterator.hasNext()){
 Employee employee = employeeIterator.next();
 employee.print();
 }
 }

 public void remove(Employee employee) {
 employees.remove(employee);
 }
}


class Developer implements Employee{

 private String name;
 private double salary;

 public Developer(String name,double salary){
 this.name = name;
 this.salary = salary;
 }
 public void add(Employee employee) {
 //this is leaf node so this method is not applicable to this class.
 }

 public Employee getChild(int i) {
 //this is leaf node so this method is not applicable to this class.
 return null;
 }

 public String getName() {
 return name;
 }

 public double getSalary() {
 return salary;
 }

 public String getDesignation(){
 return "Developer"; 
 }
 
 public void print() {
 System.out.println("-------------");
 System.out.println("Name ="+getName());
 System.out.println("Salary ="+getSalary());
 System.out.println("-------------");
 }

 public void remove(Employee employee) {
 //this is leaf node so this method is not applicable to this class.
 }

 }
 

public class CompositeEg {
 public static void main(String[] args) {
 try
 {
 Employee dir1 = new Director("dir1", 80000);
 Employee manager1=new Manager("mgr1",25000);
 
 Employee emp1=new Developer("dev1", 10000);
 Employee emp2=new Developer("dev2", 15000);
 
 manager1.add(emp1);
 manager1.add(emp2);
 
 dir1.add(manager1);
 
 Employee gManager=new Manager("mgr2", 50000);
 Employee emp3=new Developer("dev3", 20000);
 
 gManager.add(emp3);
 gManager.add(manager1);
 
 dir1.add(gManager);
 dir1.print();
 
 dir1.add( new Developer("ldev",19000));
 
 System.out.println("Designation: "+gManager.getDesignation());
 }catch(Exception e)
 {
 e.printStackTrace();
 }
 }
}

Design Pattern Free PDF