Download as pdf
Download as pdf
You are on page 1of 26
OOPS WorkBook Master The Content AUTHOR SOM PRAKASH RAI \ +91-999-0699-111 @ www jtcindia.org eS TC (opie Jte 1: Example using Method Parameter Jtc 2: Example using Method Invocation Jte 3: Example using Invoking Overloaded Methods Jte 4: Example using Call By Value / Reference Jte 5: Example Example using Constructor Chaining Ste 6 — Ite 10: Example using Class Loading & Static Blocks Execution Jte 11 — Jte 13: Example using Loading the Class Dynamically Jte 14: Example to show that class will be loaded only once Jte 15: Example Example using Static Inner Class Jte 16: Example Example using Instance Inner Class Jte 17: Example using Referencing the Object from Inner Class Jte 18: Example using Abstract Class Jte 19: Example using Limitation of Multiple Inheritance Jte 20: Example using Dynamic Dispatch and Dynamic Polymorphism Re cucieare] See S Sa TC Ste 1: Example using Method Parameter 1) Jtcl.java class Jtel{ * @Author : Som Prakash Rai *@Join —: Java Training Center * @visit t www. jtcindia.org *@Call 2+91-9990399111 +H public static void m: MethodService ser serv.show('A'); byte byl = 123; serv.show(123); serv.show(by1): // serv.show(123L)3 serv.show((int) 123L); 1/ serv.display(123); serv.display((byte) 123); serv.display(by1); } (String[] args) { new MethodService(); class MethodService { void show(int ab) { System.out.printin("'- show(int) \t:" + ab); } void display(byte by1) { System.out.printin("'- display(byte) \t:" + by); } 3 See S Sa Re cucieare] 1) Jtc2.java class Jte2{ fe *@Author : Som Prakash Rai *@Join —_: Java Training Center *@visit —_: www. jtcindia.org *@Call :+91-9990399111 void main(String|| args) { System.out.printin(""ab in Main Before\t:" + ab); Mno ref = new Mno(); ref.showValue(ab); System.out.printin("ab in Main After\t:" + ab); } class Mno { void showValue(int ab) { System.out.printin(""Before show Value\t:" + ab); if (ab != 0) showValue(ab / 10); System.out.printin(""After showValu } } "+ ab): 1) Jtc3.java class Jte3{ if * @Author : Som Prakash Rai *@Join —: Java Training Center * @visit : www. jtcindia.org *@Call :+91-9990399111 +a public static void main(String|] args) { byte byl = 123; OverloadManager mngr = new OverloadManager(); Re cucieare] See S Sa es JT mngr.show(12, by1); mngr.show(byl, 123); 1 mngr.show(by1, byl): mngr.show((int) by1, by); mngr.show(byl, (int) by); String str = null; Object obj = null int arr{] = null; mngr.display(str); mngr.display(obj); mngr.display(arr); mngr.display(null); mngr.showValues(arr); mngr.showValues(str); 1 mngr.showValues(null); mngr.showValues((String) null mngr.showValues((int|]) null); } 3 class OverloadManager { void show(int ab, byte bl) { System.out.printin("** show(int,byte) **"); } void show(byte bl, int ab) { System.out.printin("** show(byteint) **"); } void display(String str) { System.out.printin("__display(String) __' 3 void display(Object obj) { System.out.printin("__ display(Object) _"); } void showValues(String str) { System.out.printin("-- show Values(String) } void showValues(int{] arr) { System.out.printin(""-- showValues(int]]) ~ } +91-999-0699-11 ©@ www.jtcindia.org TC ee [Ste 4: Example using Call By Value/Referencee J) Jte4.java class Jte4{ a *@Author : Som Prakash Rai *@Join _: Java Training Center *@visit +: www jtcindia.org *@Call_ —:+91-9990399111 «ey public static void main(String|] args) { MethodParamService ref = new MethodParamService(); System.out.printin(""=--= PRIMITIVE ====" int ab = 123: System.out.printin("In main before\t:" + ab); ref.modify(ab); System.out.printin(""In main after\t:" + ab); System.out.printIn("\n==== REFERENCE ====" User ur = new User() ur.uid = 101; ur.phone = 6526668L; System.out.printin("In Main Before\t:" + ur.uid + "\t" + ur.phone); ref.modify(ur); System.out.printin("In Main After\t:" + ur.uid + "\t" + ur.phone); System.out.printin( System.out printin("In Mai Before\t:" + ur.uid + "\t" + ur.phone); ref.change(ur); System.out.printin("In Main After\t:" + ur.uid + "\t" + ur.phone); } class MethodParamService { void modify(int ab) { System.out.printin("'-- modify(int) —"); System.out.printin("" Before Modifying\t:" + ab); ab = ab + 1000; System.out.printin(" After Modi ing\t:" + ab); } void modify(User user) { SEER EET www.jtcindia.org System.out.printin("'-- modify(User) System.out.printin("" Before Modi user = new User()s System.out.printin("Before Modifying\t:" + user.uid + user.uid = user.uid + 1000; user.phone = 9999999999991; System.out.printin("After Modifying } + "\t" + user.phone); + user.phone); + user.uid + "\t" + user.phone); void change(User user) { System.out.printin(” System.out.printin(" Before Modify’ = user.uid + 1000; user.phone = 99999999L; System.out.printin("'After Modifying data\t:" + user.uid + user = new User()s user.uid = 33333; user.phone = 8583828785L; System.out.printin("'After Modifying Ref\t:" + user.uid + "\t""+ user.phone); } } class User { int uid; long phone; " + user.uid + "\t" + user.phone); + user.phone); 1) JteS.java class JteS{ pe * @Author =: Som Prakash Rai *@Join —_: Java Training Center *@y : www. jteindia.org *@Call 1+91-9990399111 public static void main(String[] args) { new Employee(99).show(); System.out.printin(); new Employee("Chandan").show()s SEER EET www.jtcindia.org TC ee System.out.printinQ; new Employee(98, "SomPrakash").show(); System.out.println(); new Employee(45, "Vikas", 6526668).show()s System.out.printinQ; new Employee(58, "Manish", 76767632901, 85000.0F).show()s System.out.printin(); new Employee(6526668, "Rai", 78562.00F).show(); 4 $ class Employee { int eid; String name: Jong phone; float salary; Employee(int eid) { System.out.printin("—- Employee(int) - this.eid = eid; } Employee(String name) { 11 super() //By Default System.out.printin("—- Employee(String) —\t:" + this); this.name = name; } Employee(int eid, String name) { this(eid); // supers System.out.printin("—- Employee(int,String) —\t:" + this); // Employee(eid) 1! this(eid); this.name = name; } Employee(int eid, String name, long phone) { this(eid, name); System.out.printin("—- Employee(int,String,long) —\t:" + this): this.phone = phone; Employee(int eid, String name, long phone, float salary) { this(eid, name, phone); System.out.printin("-- Employee(int,String,long,float) --\t:" + this); +91-999-0699-11 © www.jtcindia.org eS TC this.salary = salary; } Employee(long phone, String name, float salary) { this(name); System.out.printin("'-- Employee(long,String, this.phone = phone; this.salary = salary; } void show() { System.out.printin(eid + "\t" + name + "\t" + phone + "\t" + salary); 3 1) Jtc6.java class Jtc6{ Pe *@Author ; Som Prakash Rai *@Join —: Java Training Center * @visit : www. jtcindia.org *@Call + 91-9990399111 * ey public static void main(String[] args) { System.out.printin("** MAIN METHOD **"); } static { System.out.printIn("-- Static Block of Jtc6 —-"); } } t) --\t:"" + this); Rieu near) SE EES Ea eS TC 1) Jtc7.java class Jte7{ i * @Author : Som Prakash Rai *@Join —: Java Training Center * @visit t www. jtcindia.org *@Call :+91-9990399111 System.out.printin("'-- Ref Created —\n' cl =new TestClasses(); System.out.printin(cl); class TestClasses { static { System.out.printin("'— Static of TestClasses — } TestClasses() { System.out.printin("'-- TestClasses() Cons —"); } } class Jte8{ fe *@Author : Som Prakash Rai *@Join —_: Java Training Center *@y + www. jtcindia.org *@Call 24+91-9990399111 System.out.printin("** MAIN METHOD **" SE EES Ea Rieu near) System.out.printin(Xyz.var); System.out.printin("\n-- Value Accessed System.out.printIn(new Xyz())3 } } class Xyz { static int var = 123; static { System.out.printIn("'-- Static of Xyz —"); } 4 1) Jtc9.java class Jte9{ fe * @Author _: Som Prakash Rai *@Join —_; Java Training Center * @visit : www. jtcindia.org *@Call :+91-9990399111 «ey public static void main(String|] args) { System.out.printIn("** MAIN METHOD **"); new Student); System.out.printIn(""-- Student Object Created —\n"); new Employee(); System.out.printIn("-- Employee Object Created -\n"); 3 } class Person { static { System.out.println("\n-- Static of Person --"); Person() { System.out.printin("'-- Person() Cons — } } class Student extends Person { Rieu near) SE EES Ea static { System.out.printIn("— Static of Student —"); Student() { System.out.printIn("— Student() Cons — } } class Employee extends Person { static { System.out.printin("'—- Static of Employee —"); } J) Jtc10.java class Jte10f fe * @Author _; Som Prakash Rai : Java Training Center + www, jtcindia.org *@Call —:+91-9990399111 #4) public static void main(String[] args) { System.out.printIn("** MAIN METHOD **"); System.out.printIn(Mno.VAL);// SOP(9090); System.out.printIn(Mno.VAL+100);// SOP(9190); } } class Mno { final static int VAL = 9090; static { System.out.printin(""** STATIC BLOCK OF Mno\t:" + VAL); } } © +91-999-0699-101 www.jtcindia.org Ss JTC ClassLoader load = Test60.class.getClassLoader(); Class cl = Class.forName(cName, false, load); System.out.printIn("—- Class Loaded \t:" + cl.getName()); System.out.printIn("SuperClass\t:" + cl.getSuperclass()); System.out.printin("\n===- "ys Object obj = cl.newInstance(); System.out.printin(obj); y else { System.out.printIn(" Provide Classname as CLA"); } 3 } //TestClasses , Person , Student and Employee class same as Previous Examples J) Stel3.java class Jte13{ ra * @Author _: Som Prakash Rai *@s Java Training Center * @visit + www. jtcindia.org *@Call —_-++91-9990399111 #4) public static void main(String|] args) throws Exception { if (args.length == 1) { String cName = args[0|; ClassLoader load = Test61.class.getClassLoader(); Class cl = load.loadClass(cName); System.out.printIn("'-- Class Loaded \t:" + cl.getName()); System.out.printin("SuperClass\t:" + cl.getSuperclass())s System.out.printin("\n===" "3 Object obj = cl.newInstance(): System.out.printIn(obj); pelse { System.out.printin(" Provide Classname as CLA"); } } //TestClasses , Person , Student and Employee class same as Previous Examples © +91-999-0699-101 www.jtcindia.org J) Jtel4.java class Jte14{ it * @Author : Som Prakash Rai *@ : Java Training Center *@ : www. jteindia.org *@Call —+91-9990399111 ay public static void main(String[] args) throws Exception { System.out.printin(""In Main Method Loading the Hello Class"); ClassLoader loader = Test62.class.getClassLoader(); Class.forName("Hello", false, loader): System.out.printIn("'—- Class Loaded Successfully -- System.out.printin('"Delete the class file and press ENTER "); System.in.read(); Hello h = new Hello(); hshow(s lay()s new Hello(12).show()s new Hello(89, "JTC").show()s new Hello().display(); System.out.printin(Hello.value); } class Hello { int ab; String msg; static int value = 1234; Hello() { System.out.printIn("— Hello() Cons —"); } Hello(int ab) { System.out.printin("'— Hello(int) Cons ~"); } Hello(int ab, String msg) { System.out.printIn("'-- Hello(int,String) Cons —"); } void show() { System.out.printIn(""** show () in Hello **"); © +91-999-0699-101 www.jtcindia.org System.out.printIn(ab); System.out.printIn(msg); void display() { System.out.printin("** display() in Hello **"); } } )) Stel5.java class Jte15{ fe *@Author : Som Prakash Rai *@Join —_: Java Training Center * @visit : www. jtcindia.org *@Call —-+91-9990399111 ay public static void main(String[] args) { System.out.printIn(MyOuterClass. MyInnerClass, LENGTH); System.out.printIn(MyOuterClass.MyInnerClass.val); MyOuterClass. MyInnerClass.display Message(); //MyInnerClass refl = null; MyOuterClass.MyInnerClass ref = null; System.out.printIn(ref.msg); ref = new MyOuterClass.MyInnerClass(" Message from Main Method /ivef=new MyOuterClass().new MyInnerClass(""Message from Main Method"); Hicef-new MyOuterClass().MyInnerClass(" Message from Main Method"); System.out.printin(ref.msg); MyOuterClass.displayInOuterClass()s new MyOuterClass().showInOuterClass(); ref.showResult(); } } class MyOuterClass { int result = 1045; static int value = 1212; © +91-999-0699-101 www.jtcindia.org static class MyInnerClass { static int val = 9090; static final int LENGTH = 10; String msg; MylInnerClass(String msg) { this.msg = msg; } static void displayMessage() { System.out.printin(""— displayMessage static in Inner Class ~"); $ void showResult() { System.out.printin("\n-- showResult () in Inner Class~"); System.out.printin("MSG\t:" + msg); System.out.printin("val\t:" + val); System.out.printn("** Member of Outer Class **"); System.out.printin("static\t:" + value); 1] System.out.printin("ins\t:"+result); System.out.printin("“ins\t:" + new MyOuterClass().result); } } void showInOuterClass() { System.out.printin("\n-- showInOuterClass —"); MylInnerClass refl = new MyInnerClass(""Message in Show Method"); System.out.printin(refl.msg); } static void displayInOuterClass() { System.out.printin("\n-- displayInOuterClass --"); MylInnerClass refl = new MyInnerClass(""Message in Display Method"); System.out.printIn(ref1.msg); $ 3 © +91-999-0699-101 www.jtcindia.org J) Stel class Jte16f fe *@Author : Som Prakash Rai *@Join —: Java Training Center *@visit —_: www jtcindia.org *@Call_—:+91-9990399111 uy public static void main(String[] args) { ASB ref = null; ref= new ASB(; System.out.println(ref); IEXSY ref2=null; X.Y ref3 = nulls X ref = new X()s /I ref3=new Y(): ref3 = refa.new Y(: ref3 = new X().new YO: System.out.printIn(ref3); System.out.printin("\n\n———--—"); Hello.display(; System.out.printin(); Hello he = new Hello("MSG in MAIN"); System.out.printIn(he.msg); // System.out.printin(he.intValue); he.show(); Hello.JTCInner1 inRef = null: inRef = new Hello("MSG IN MAIN AGAIN").new JTCInner1(6060); inRef.showDatalnInnerClass(): inRef = he.new JTCInner (4040); inRef.showDatalnInnerClass(); System.out.printIn(inRef.intValue); // System.out.printin(inRef.msg); } 3 class ASB {} class B {} class A { © +91-999-0699-101 www.jtcindia.org II class Bi} } class X { int var = 10; class ¥ {} $ class ¥ {3 Ui class XSY{} class Hello { static int VAL = 9090; String msg; Hello(String msg) { this.msg = msg; class JTCInner! { | static int stValue=9876; final st CONS = 9999; int intValue; public JTCInner1 (int intValue) { this.intValue = intValue; } void showDatalnInnerClass() { System.out.printin("\n** showData in Inner Class **"); System.out.printin(intValue); System.out.printIn(CONS); System.out.printIn(msg); // System.out.printIn(this.msg); System.out.printIn(VAL); $ $ void show() { System.out.printIn("\n-- INSTANCE Show Method "); System.out.printin(VAL); System.out.printin(msg); System.out.printIn(this.msg); JTCInnert ref = null; © +91-999-0699-101 www.jtcindia.org ref = new JTCInner1 (1111) ref= this.new JTCInner1(1111); System.out.printIn(ref.intValue); ref.showDatalnInnerClass(); } static void display() { System.out.printin("\n—- Static Display Method —" System.out.printin(VAL); // System.out.printIn(msg); Hello ref = new Hello("MSG in Display"); System.out.printin(ref.msg); JTCInnerI refl = null; 1/ refl=new JTCInner1(2222); refl = refnew JTCInnerl(2222); refi showDatalnInnerClass(); } } 1) Jte17.java class Jte17{ fe * @Author —: Som Prakash Rai *@Join —_: Java Training Center * @visit : www. itcindia.org *@Call —_-:+91-9990399111 +4) public static void main(String[] args) { Abe ref=new Abe(" WELCOME"); Abe.Pqr ref2=ref.new Pqr(3232); ref2.show(); System.out.printin(); new Abe(" THANKS").new Pqr(4141).show(); 3 } class Abe { static String stVar = "STATIC IN Outer"; String vars © +91-999-0699-101 www.jtcindia.org Abe(String var) { this.var = var; } class Pqr { final static String stVar = "STATIC IN Inner"; int var; Pqr(int var) { this.var = var; } void show() { boolean var = false: System.out.printin("\n— in Show Method -- in Inner Class —"); System.out.printin(var); System.out.printin(this); System.out, System.out.printIn(Abe.stVar); 1) Stc18.java class Jte18{ * * @Author Som Prakash Rai *@ : Java Training Center *@ + www.jtcindia.org, *@Call —:+91-9990399111 ay public static void main(String[] args) { System.out.printIn(Person.minAgeToVote); Person per = nulls /I per=new Person(): /I per.showWorkInfo(); // System.out.printin(per.name); per = new Employee(""SomPrakash", 7676763290L); © +91-999-0699-101 www.jtcindia.org SS TC ee per.showWorkInfo(); System.out.printIn(per.name + "\t" + per.phone); per = new OldStudent(); per.showWorkInfo(); per = new CurrentStudent(); per.showWorkInfo(): System.out.printin("\n**###sss0H), Person p = null; p= PersonService.getInstance("Employee"); System.out.printin(p.getPersonType()); p= PersonService.getInstance("OldStudent"); System.out.printin(p.getPersonType()); p= PersonService.getInstance("CurrentStudent"); System.out.printin(p.getPersonType()); } } abstract class Person { static int minAgeToVote = 18; String name; long phone; Person(String name, long phone) { this.name = name; this.phone = phone; System.out.printin("— Person(String,long) Cons. \t:" + this): Person() { System.out.printin("— Person() Cons.. \t:" + this); t System.out.printin(" } abstract void showWorkInfo(); abstract String getPersonType(); void show() { // Person p=new Person(); i! p.showWorkInfo(): } } class Farmer extends Person{ } ** Person Instance Block **' © +91-999-0699-101 class Employee extends Person { Employee(String name, long phone) { super(name, phone): Employee( {} String getPersonType() { return "Employee"; } void showWorkInfo() { System.out.printin("— Employee Working in Company —"); } i abstract class Student extends Person { } class CurrentStudent extends Student { void showWorklnfo() { System.out.printin(""— Attending the classes —"); 4 String getPersonType() { return "CurrentStudent"; } } class OldStudent extends Student { void showWorkInfo() { System.out.printIn("— Searching for Job —"); } String getPersonType() { return "OldStudent"; $ 3 class PersonService { static Person getInstance(String Name) { if (CName.equals("Employee")) return new Employee(); else if (CName.equals("OldStudent")) return new OldStudent(): else if (CName.equals("CurrentStudent")) return new CurrentStudent(); else © +91-999-0699-101 www.jtcindia.org return null; } } J) Jtc19.java class Jtc19{ * * @Author ; Som Prakash Rai *@J : Java Training Center *@visit —_ : www.iteindia.org *@Call —:+91-9990399111 #4) interface InterLI { void show/(); } interface Inter22 { int show(); } class Cd implements Inter 1,Inter22 { i public void show() {} ¥] public int show() {} 3 class Ab implements Inter1I { public void show() { } } class Bc implements Inter22 { public int show() { return 0; 3 } © +91-999-0699-101 www.jtcindia.org public abstract class Shape { double length; Shape(double length) { this.length = length; } final double getLength() { return this.length; } abstract double findArea(); abstract String getType(); static void display() { System.out.printin("'— Static Display in Shape ~"); } i ShapeUtil.java public class ShapeUtil { public void showShapelnfo(Shape sp) { System.out.printin("\nType\t:" + sp.getType()); System.out.println("Length\t:" + sp.getLength()); System.out.printin("Area\t:" + sp.findArea()); sp.display();// Shape.display(); 1/ sp.getSideQ; /I sp.getWidth(; System.out.printin(); if (sp instanceof Square) { Square sq = (Square) sp; System.out.printin("Side sq.display(); } else if (sp instanceof Rectangle) { Rectangle ree = (Rectangle) sp; rec.display(); System.out.printin(" Width’ 2" + squgetSide)); © +91-999-0699-10 www.jtcindia.org Square public class Square extends Shape { Square(double side) { super(side): } double findArea() { System.out.printin("** Square Find Area **"); return length * length; } double getSide() { return length; String getType() { return "Square"; } static void display() { System.out.printin("== Static Display in Square } } Rectangle.java public class Rectangle extends Shape { double widths Rectangle(double length, double width) { super(length); this.width = width; } double findArea() { System.out.printin("** Rectangle Find Area **"); return length * widths } String getType() { return "Rectangle"; } double getWidth() { return width; $ static void display() { © +91-999-0699-10 www.jtcindia.org System.out.printIn('"++ Statie Display in Rectangle ++"); } $ J) Jtc20.java class Jtc20{ I* *@Author _: Som Prakash Rai * @Join *@visit —_ : www.jteindia.org *@Call —_-:+91-9990399111 4] public static void main(String|] args) { ShapeUtil util = new ShapeUtil(): util.showShapelnfo(new Square(12.0)); util. showShapelnfo(new Rectangle(12.0, 10.0); } 3 Bree EELS Cea Pre euciee)

You might also like