Posts

Showing posts from September, 2019

Overloading Cases

package com.sk.demo; class Overloading{ public static void main(String[] args){ A a = new A(); a.m1("String"); // string type method will call a.m1(new Object()); // Object type method will call a.m1(new A()); // Object type method will call a.m1(null); // string type method will call since it is applicable in child level also then no need to go for parent level System.out.println("------------ For B class ------------"); B b = new B(); b.m1(10); b.m1(new Object()); b.m1(null); // since null not applicable for int type then  System.out.println("-------------- For C class -------------"); C c = new C(); c.m1("Hello"); c.m1(new StringBuffer("Hi")); c.m1(new Object()); /* error: reference to m1 is ambiguous                 c.m1(null);                  ^ both method m1(String) in C and...

Mutable and Immutable

import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Test2{ public static void main(String[] args) { List<Mutbl> imlist1 = new ArrayList<Mutbl>(); List<Mutbl> imlist2 = new ArrayList<Mutbl>(); // immuatble System.out.println("------------------IMMUTABLE EXAMPLE-----------------------"); Mutbl imm1 = new Mutbl("P1", "S1"); Mutbl imm2 = new Mutbl("P2", "S2"); imlist1.add(imm1); imlist1.add(imm2); System.out.println(Arrays.toString(imlist1.toArray())); imm1.setP1("PP1"); imm1.setP2("SS1"); imm2.setP1("PP2"); imm2.setP2("SS2"); imlist2.add(imm1); imlist2.add(imm2); System.out.println(Arrays.toString(imlist2.toArray())); System.out.println("value of list 1 changes indirectly due to immutabality"); System.out.println(Arrays.toString(imlist1.toArra...