Explanation:
Ok so in Java you can have multiple functions with same names but different parameters, and the JVM is smart enough to figure out which one you are calling. e.g.
Code: Select all
// Java code..
class Person {
private String name;
private int age;
public Person(String name) {
this.name = name;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// and now I can call them like
Person person = new Person("Rizjav");
Person person = new Person("rizjav", 23);I know I can pass a flag variable in the constructor and then figure out what function I need to call by checking flag's value e.g. if flag==1 then do this else do that...but I think that's not how a real OOP language should work.