Showing posts with label overloading. Show all posts
Showing posts with label overloading. Show all posts

Saturday, 15 May 2021

Php: Adding dynamic properties, overloading

If you set a value to an undefined property of object, Php internally defines the property and set the value.

 

Let’s see it with an example.

 

dynamic_properties_demo.php

#!/usr/bin/php

<?php

    class Employee{

    }

    $emp1 = new Employee();

    $emp1->id = 1;
    $emp1->first_name='Krishna';

    echo "id : $emp1->id\n";
    echo "first_name: $emp1->first_name";

?>

 

Output

$./dynamic_properties_demo.php 

id : 1
first_name: Krishna

 

As you see above example, I defined Employee class with no properties. But I defined properties id, first_name to the Employee object $emp1 dynamically.

 

Visibility vs dynamic properties

Due to the feature of dynamically adding properties to the object, you may feel that sometimes ‘private properties are visible in subclasses’, But that is not true. Let’s see this with an example.

 

dynamic_properties_demo_2.php

#!/usr/bin/php

<?php

    class Employee{
        private $org = 'ABC Corp';

        public function get_org(){
            return $this->org;
        }
    }

    class Manager extends Employee{

    }

    $emp1 = new Manager();

    $emp1->org = 'XYZ Corp';

    echo "\$$emp1->org : $emp1->org\n";
    echo "\$Actual organization: {$emp1->get_org()}";
?>

 

Output

$./dynamic_properties_demo_2.php 

$XYZ Corp : XYZ Corp
$Actual organization: ABC Corp

 

As you see Employee class, I defined a private property $org. Manager is a subclass of Employee class.

 

I created an instance of Manager class and defined a dynamic property org (which is same as parent class private property). This new dynamic property ‘org’ is nothing to do with the actual private property defined in parent class, you can confirm the same by calling get_org method of Employee class.

 

 

 

 

 

 

Previous                                                    Next                                                    Home

Sunday, 27 January 2019

Groovy: Overloading of isCase method


In my previous post, I explained how can we use custom types in switch statement by overloading isCase method. We can even overload isCase method to get more flexibility.

HelloWorld.groovy

class Employee{
 int id
 String name

 Employee(int id, String name){
  this.id = id
  this.name = name
 }

 boolean isCase(Employee emp) {
  return emp.id == this.id
 }

 boolean isCase(int id){
  return id == this.id
 }
}

emp1 = new Employee(1, "Krishna")
emp2 = new Employee(2, "Ram")
emp3 = new Employee(1, "Ram")

switch(emp1){
 case emp2 : println "Both emp1 and emp2 has same id"; break
 case emp3 : println "Both emp1 and emp3 has same id"; break
 default : println "No match found"
}

switch(1){
 case emp1 : println "emp1 has id 1"; break
 case emp3 : println "emp3 has id 1"; break
 default : println "No match found"
}

Output
Both emp1 and emp3 has same id
emp1 has id 1



Previous                                                 Next                                                 Home

Saturday, 8 February 2014

Overloading Methods

Methods within a class can have the same name if they have different parameter lists.

For Example
   int sum(int a, int b);
   int sum(int a, int b, int c);

Both methods have the same name sum, and parameters are different.

Overloading methods is differentiated by the number and type of arguments passed to the method.

The return type is not sufficient to differentiate two overload methods

Example
class OverloadEx{
 void print(String s){
  System.out.println("I am String " + s);
 }

 void print(int a){
  System.out.println("I am integer " + a);
 }

 void print(float f){
  System.out.println("I am float " + f);
 }

 public static void main(String args[]){
  OverloadEx obj = new OverloadEx();
  obj.print("HI");
  obj.print(9);
  obj.print(10.01f);
 }
}
   
Output
I am String HI
I am integer 9
I am float 10.01
   
Class OverloadEx, overloaded the method print. 

Some points to remember
1. The return type is not sufficient to differentiate two overloaded methods.
  When you try to differentiate two methods with the return type, then you will get compile time error like “Method is already defined”.
class OverloadEx{
 String print(int s){
  System.out.println("I am integer " + s);
 }
 
 void print(int a){
  System.out.println("I am integer " + a);
 }
}
       
When you try to compile you will get the below error
OverloadEx.java:7: error: method print(int) is already defined in class OverloadEx
        void print(int a){
        ^
1 error

2. By default a decimal number considered to be an integer
class OverloadEx{
 void print(int s){
  System.out.println("I am integer " + s);
 }

 void print(byte a){
  System.out.println("I am byte " + a);
 }

 void print(short a){
  System.out.println("I am short " + a);
 }

 void print(long a){
  System.out.println("I am long " + a);
 }

 public static void main(String args[]){
  OverloadEx obj = new OverloadEx();
  obj.print(10);
 }
}
       
Output
I am integer 10

Above program overloads method print, with 4 parameters byte, short, int and long. Program calls the print() with value 10. By default numbers consider to be primitive. So print with int argument will be called.

3. By default a real value considers as double
class OverloadEx{
 void print(float s){
  System.out.println("I am float " + s);
 }

 void print(double a){
  System.out.println("I am double " + a);
 }

 public static void main(String args[]){
  OverloadEx obj = new OverloadEx();
  obj.print(10.01);
 }
}
       
Output
I am double 10.01
          
4. What is the output of the below program ?      
class OverloadEx{
 void print(float s){
  System.out.println("I am float " + s);
 }

 public static void main(String args[]){
  OverloadEx obj = new OverloadEx();
  obj.print(10.01);
 }
}
       
A real number considers as double by default. So program gives compile time error like below
OverloadEx.java:9: error: method print in class OverloadEx cannot be applied to given types;
obj.print(10.01);
^
required: float
found: double
reason: actual argument double cannot be converted to float by method invocation conversion
1 error
  
5. What is the output of the below program ?             
class OverloadEx{
 void print(float s){
  System.out.println("I am float " + s);
 }

 public static void main(String args[]){
  OverloadEx obj = new OverloadEx();
  obj.print(10.01f);
 }
}

Output       
I am float 10.01
 
          


Constructor varargs                                                 vararg overloading                                                 Home