Java: @Override

This entry is part 51 of 54 in the series Learn Java

When creating an overriding method, you may want to specify that you want to override a method in the parent class.

If you specify explicitly with @Override, the compiler can give a warning if you have made a mistake such as using a non existing method.

As shown in the example below, the fan method in line 21 will definitely override the fan method in line 7.

package overridetest;

class Music {

    int i = 1;

    int fan() {
        return i;
    }

    static String groupie() {
        return "Music";
    }
}

class Rock extends Music {

    int i = 2;

    @Override
    int fan() {
        return i + 10;
    }

    static String groupie() {
        return "Rock";
    }
}

public class OverrideTest {

    public static void main(String args[]) {
        Rock r = new Rock();
        System.out.println(r.i);
        System.out.println(r.fan());
        System.out.println(Rock.groupie());
    }
}

atoverride

 

Java: Calling methods

This entry is part 16 of 54 in the series Learn Java

In Java, a method is just like a function in C.

The method that we are creating here will simply print out a text message. To invoke (call) it from the main method an instance of the MyApp class must first be created.

The dot operator is then used after the instance’s name in order to access its members, which includes the PrintOut method.

package javaapplication15;

public class JavaApplication15 {

     static class MyApp {

        void  PrintOut() {
            System.out.println("Hello");
        }
    }

    public static void main(String[] args) {
        MyApp m = new MyApp();
        m.PrintOut();
    }
}

method

With the declaration

MyApp m = new MyApp();

m is now an instance of the class MyApp.  We can now use m to access a method of the class PrintOut as in

m.PrintOut();

C#: Generic method for reversing an array

This entry is part 62 of 71 in the series C#

An array could be comprised of integer or string.

Here, we work out a method to reverse an array be it a string array or an integer array.

using System;

namespace ReverseArray
{
    public static class Extensions
    {
        public static void ReverseArray<T>(this T[] array)
        {
            array.ReverseArray(0, array.Length - 1);
        }

        public static void ReverseArray<T>(this T[] array, int lower, int upper)
        {
            for (int i = lower, j = upper; i < j; i++, j--)
            {
                var temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8 };
            arr1.ReverseArray();

            foreach (int i in arr1)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();

            int[] arr2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            arr2.ReverseArray(2, 6);

            foreach (int n in arr2)
            {
                Console.Write("{0} ", n);
            }
            Console.WriteLine();

            string[] arr3 = { "The", "End", "Of", "The", "Innocent"};
            arr3.ReverseArray();

            foreach (string s in arr3)
            {
                Console.Write("{0} ", s);
            }

            Console.ReadKey();
        }
    }
}

genericarray

PHP & OOP: Method visibility

This entry is part 10 of 11 in the series PHP & OOP

Class methods may be defined as public, private, or protected.

Methods declared without visibility keyword are defined as public.

<?php

class MyClass
{
    // Declare a public constructor
    public function __construct()
    {
    }

    // Declare a public method
    public function MyPublic()
    {
        echo "I am a public function";
        echo "<br>";
    }

    // Declare a protected method
    protected function MyProtected()
    {
        echo "I am a protected function";
        echo "<br>";
    }

    // Declare a private method
    private function MyPrivate()
    {
        echo "I am a private function";
        echo "<br>";
    }

    // This is public
    function printString()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate();
    }
}

$myclass = new MyClass;
$myclass->MyPublic(); // Works
echo "--<br>";
// $myclass->MyProtected(); Fatal Error
// $myclass->MyPrivate(); Fatal Error
$myclass->printString(); //Public, Protected and Private work

?>

We look at these 2 statements:

$myclass->MyProtected();

-Fatal Error as MyProtected() is protected and can only be accessed by inherited class.

$myclass->MyPrivate();

Fatal Error as MyPrivate() is a private function and can only be accessed in its own class.