Friday, November 30, 2018

Method Hiding and Method Overriding


//Shadowing replaces the complete element of the parent.In simple word it can change a
//variable element of parent to a method or function

//Shadowing  is useful when get a requirement where the vocabulary will be kept
//same but the data type needs to be change.

//Overriding changes implementation while Shadowing replaces element with a

//complete new element only the interface vocabulary is same


//Method Hiding
namespace MethodHiding
{
    class A
    {
        public void PrintName()
        {
            Console.WriteLine("Print A");
        }
    }
    class B : A
    {
        public new void PrintName()
        {
            Console.WriteLine("Print B");
        }
    }
    class C : B
    {
        public new void PrintName()
        {
            Console.WriteLine("Print C");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            objB.PrintName();//B

            C objC = new C();
            objC.PrintName();//C

            A objAB = new B();
            objAB.PrintName();//A

            B objCB = new C();
            objCB.PrintName();//B
        }
    }
}

//Method Overriding
namespace MethodOverriding
{
    class A
    {
        public virtual void PrintName()
        {
            Console.WriteLine("Print A");
        }
    }
    class B : A
    {
        public override void PrintName()
        {
            Console.WriteLine("Print B");
        }
    }
    class C : B
    {
        public override void PrintName()
        {
            Console.WriteLine("Print C");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            objB.PrintName();//B

            C objC = new C();
            objC.PrintName();//C

            A objAB = new B();
            objAB.PrintName();//B


            B objCB = new C();
            objCB.PrintName();//C
        }
    }
}

Thursday, November 22, 2018

Pop vs Push & Shift vs Unshift

pop(): Remove an item from the end of an array
let cats = ['Bob', 'Willy', 'Mini'];
cats.pop(); // ['Bob', 'Willy']
pop() returns the removed item.

push(): Add items to the end of an array
let cats = ['Bob'];
cats.push('Willy'); // ['Bob', 'Willy']
cats.push('Puff', 'George'); // ['Bob', 'Willy', 'Puff', 'George']
push() returns the new array length.


shift(): Remove an item from the beginning of an array
let cats = ['Bob', 'Willy', 'Mini'];
cats.shift(); // ['Willy', 'Mini']
shift() returns the removed item.

unshift(): Add items to the beginning of an array
let cats = ['Bob'];
cats.unshift('Willy'); // ['Willy', 'Bob']
cats.unshift('Puff', 'George'); // ['Puff', 'George', 'Willy', 'Bob']
unshift() returns the new array length.

Splice vs Slice

  1. The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object.
  2. The splice() method changes the original array and slice() method doesn’t change the original array.
  3. The splice() method can take n number of arguments and slice() method takes 2 arguments.
Splice with Example
Argument 1: Index, Required. An integer that specifies at what position to add /remove items, Use negative values to specify the position from the end of the array.
Argument 2: Optional. The number of items to be removed. If set to 0(zero), no items will be removed. And if not passed, all item(s) from provided index will be removed.
Argument 3…n: Optional. The new item(s) to be added to the array.
var array=[1,2,3,4,5];
console.log(array.splice(2));
// shows [3, 4, 5], returned removed item(s) as a new array object.
 
console.log(array);
// shows [1, 2], original array altered.
 
var array2=[6,7,8,9,0];
console.log(array2.splice(2,1));
// shows [8]
 
console.log(array2.splice(2,0));
//shows [] , as no item(s) removed.
 
console.log(array2);
// shows [6,7,9,0]

Slice with Example
Argument 1: Required. An integer that specifies where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array.
Argument 2: Optional. An integer that specifies where to end the selection but does not include. If omitted, all elements from the start position and to the end of the array will be selected. Use negative numbers to select from the end of an array.
var array=[1,2,3,4,5]
console.log(array.slice(2));
// shows [3, 4, 5], returned selected element(s).
 
console.log(array.slice(-2));
// shows [4, 5], returned selected element(s).
console.log(array);
// shows [1, 2, 3, 4, 5], original array remains intact.
 
var array2=[6,7,8,9,0];
console.log(array2.slice(2,4));
// shows [8, 9]
 
console.log(array2.slice(-2,4));
// shows [9]
 
console.log(array2.slice(-3,-1));
// shows [8, 9]
 
console.log(array2);
// shows [6, 7, 8, 9, 0]




Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...