150 likes | 278 Views
Key Concepts from 1301: What you should already know. Class Reference Object Constructors default constructor others constructor chaining Inheritance constructors method overriding/overloading polymorphism Variable Shadowing. What is a Class?.
E N D
Key Concepts from 1301:What you should already know • Class • Reference • Object • Constructors • default constructor • others • constructor chaining • Inheritance • constructors • method overriding/overloading • polymorphism • Variable Shadowing Object, class, basics (c) Eraj Basnayake
What is a Class? public class Box { protected int length; protected int width; protected int height; protected static int count =0; … public void setLength(…){ … } private int getLength(…){ … } … A class is a complete description of a single concept (idea, place, thing, process… ) To convert idea into code: • Variables are … • Methods are ... Object, class, basics (c) Eraj Basnayake
What is a reference? Object? a1 a0 ref2 ref1 null null What is an object? What is a reference? Box ref1; Box ref2; ref1 = new Box(); ref2 = new Box(); Object, class, basics (c) Eraj Basnayake
A more realistic picture int height int width int length a1 int height int width int length a0 a0 a1 ref1 ref2 public static int count=0; public Box(){ this(0,0,0); } public Box(int length, int width, int height){ setLength(length); setWidth(width); setHeight(height); } public Box(Box box){ this(box.getLength(), box.getWidth(), box.getHeight()); } public void setHeight(int h){ height = h;} public void setWidth(int w) { width = w;} public void setLength(int l){ length = l;} public int getHeight() { return height;} public int getWidth() { return width;} public int getLength() { return length;} //… other methods Object, class, basics (c) Eraj Basnayake
Inheritance Relationship Inheritance is used to specialize a class Since the child class is a specialization of the parent class, it has • Has all of the behaviors of the old class (except private/final) • Some of the older behavior may be modified (overriding) • Could have behavior of its own, not found in the parent class. (specialization) Example Issues: public class Cube extends Box{ public Cube(){ this(0); } public Cube(int dim){ super(dim,dim,dim); } public void setDimensions(int dim){ super.setLength(dim); super.setWidth(dim); super.setHeight(dim); } public void setLength(int l){setDimensions(l);} public void setWidth(int w){setDimensions(w);} public void setHeight(int h){setDimensions(h);} } • Method overloading and overriding • Constructor chaining • Polymorphism Object, class, basics (c) Eraj Basnayake
Method overriding Cube public static int count=0; public Box(){ this(0,0,0); } public Box(int length, int width, int height){ setLength(length); setWidth(width); setHeight(height); } public Box(Box box){ this(box.getLength(),box.getWidth(),box.getHeight()); } public void setHeight(int h){ height = h;} public void setWidth(int w){ width = w;} public void setLength(int l){ length = l;} public int getHeight(){return height;} public int getWidth(){ return width;} public int getLength(){ return length;} //… other methods public Cube(){ this(0); } public Cube(int dim){ super(dim,dim,dim); } public void setDimensions(int dim){ super.setLength(dim);super.setWidth(dim);super.setHeight(dim); } public void setLength(int l){setDimensions(l);} public void setWidth(int w){setDimensions(w);} public void setHeight(int h){setDimensions(h);} Overriding != overloading Object, class, basics (c) Eraj Basnayake
Constructor Chaining Cube • Local chaining • chaining under inheritance public static int count=0; public Box(){ this(0,0,0); } public Box(int length, int width, int height){ setLength(length); setWidth(width); setHeight(height); } public Box(Box box){ this(box.getLength(),box.getWidth(),box.getHeight()); } public void setHeight(int h){ height = h;} public void setWidth(int w){ width = w;} public void setLength(int l){ length = l;} public int getHeight(){return height;} public int getWidth(){ return width;} public int getLength(){ return length;} //… other methods public Cube(){ this(0); } public Cube(int dim){ super(dim,dim,dim); } public void setDimensions(int dim){ super.setLength(dim);super.setWidth(dim);super.setHeight(dim); } public void setLength(int l){setDimensions(l);} public void setWidth(int w){setDimensions(w);} public void setHeight(int h){setDimensions(h);} Object, class, basics (c) Eraj Basnayake
Polymorphism = Many Shaped c0 c c0 b Cube inherited from Box … Of Cube … int height int width int length c0 Cube c = new Cube(1); Box b = c; Key Concept When using b you are thinking of the object as a Box. Therefor, you may use only method found in Box, including those that were overridden. c.getLength(); //Valid-inherited from Box b.setLength(5); //running Cube’s setLength b.setDimensions(6); //invalid C = (Cube) b; Downcasting: Downcasting The process of assigning a object referenced by a parent reference down to a Child reference. • Can only be done if the object is truly a child type • Otherwise will throw ClassCastException Object, class, basics (c) Eraj Basnayake
Polymorphism = Many Shaped Object of type Human Object human Carnivore carnivore biped Biped Monkey monkey Human object • Can run methods found in Object - Human • Can run methods first defined in Object - Monkey but not those methods exclusively Human. • Can run methods first defined in Object - Biped but not those method exclusively Ape or Human • Can run only methods first defined in Object - Carnivore • Can run only methods first defined in Object Human human = new Human(); Monkey monkey = human ; Biped biped = human ; Carnivore carnivore = human ; Object object = human ; Object, class, basics (c) Eraj Basnayake
Variable shadowing Variables of a super class are not overridden by the subclass - instead they are shadowed. This implies both classes have a variable with the same name and type. public Parent{ public int i; …method0(){ i = 10; … } … method1(){ i = 5; … } … public Child extends Parent{ public int i; …method1(){ i = 20; } … public class Client{ … method(…){ Child c = new Child(); c.method1(); c.method0(); c.i = 6; Parent p = c; p.i = 10; Variable Shadowing Rules • Developer perspective - from inside the method/class, you will (always) use the most local variable • Client perspective (outside the class) - the type of reference determines which variable is used. developer client Uses the Child’s i Uses the Parent’s i Object, class, basics (c) Eraj Basnayake
The mother of all Examples public class Light{ public String billType = “Small bill” //… protected double getBill(int noOfHours){ double smallAmount = 10.0; smallAmount = smallAmount * noOfHours; System.out.println(billType+”:”+smallAmount); return smallAmount; } public void banner(){ System.out.println(“Let there be light!”); } } Variable Shadowing public class TubeLight extends Light{ public String billType = “Large bill”; //… public double getBill(int noOfHours){ double largeAmount = 100.0; largeAmount = largeAmount * noOfHours; System.out.println(billType+”:”+largeAmount); return largeAmount; } public double getBill(){ System.out.println(“no bill”); return 0.0; } } Method Overriding Object, class, basics (c) Eraj Basnayake
The mother of all Examples What's the output? Give the single most appropriate term/explanation. class NeonLight extends TubeLight{ //… public void demonstrate(){ } } public class Client{ public static void main(String args[]){ } } banner(); getBill(20); getBill(); System.out.println(billType); NeonLight neonRef = new NeonLight(); neonRef.demonstrate(); TubeLight tl = neonRef; tl.banner(); tl.getBill(20); tl.getBill(); System.out.println(tl.billType); Light l = neonRef; l.banner(); l.getBill(20); l.getBill(); System.out.println(l.billType); Object, class, basics (c) Eraj Basnayake
One more thing! Final methods and classes If a method is Final - it cannot be overridden If a class is Final - it cannot be extended Object, class, basics (c) Eraj Basnayake
Arrays Array references are similar to object references. Primitive arrays: int[] array1; array1 = new int[10]; int[][] array2 = new int[20][30]; Object arrays: Box[] array; array = new Box[20]; for(int i=0; i<array.length; i++) array = new Box(); Create the array of references Create the individual objects. Array Algorithms that you should know: insert delete traversal Object, class, basics (c) Eraj Basnayake
the end Object, class, basics (c) Eraj Basnayake