Presentation is loading. Please wait.

Presentation is loading. Please wait.

Key Concepts from 1301: What you should already know

Similar presentations


Presentation on theme: "Key Concepts from 1301: What you should already know"— Presentation transcript:

1 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

2 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

3 Object, class, basics (c) Eraj Basnayake
What is a reference? Object? What is an object? ref1 null ref2 null What is a reference? Box ref1; Box ref2; ref1 = new Box(); ref2 = new Box(); a0 a1 Object, class, basics (c) Eraj Basnayake

4 Object, class, basics (c) Eraj Basnayake
A more realistic picture ref1 a0 ref2 a1 int height int width int length a0 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 int height int width int length a1 Object, class, basics (c) Eraj Basnayake

5 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

6 Object, class, basics (c) Eraj Basnayake
Method overriding 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);} Cube Overriding != overloading Object, class, basics (c) Eraj Basnayake

7 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

8 Object, class, basics (c) Eraj Basnayake
Polymorphism = Many Shaped c c0 int height int width int length c0 b c0 Cube inherited from Box Of Cube 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

9 Object, class, basics (c) Eraj Basnayake
Polymorphism = Many Shaped Object human Carnivore carnivore biped Biped Object of type Human Monkey monkey Human object Human human = new Human(); Monkey monkey = human ; Biped biped = human ; Carnivore carnivore = human ; Object object = human ; 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 Object, class, basics (c) Eraj Basnayake

10 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{ …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

11 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

12 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

13 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

14 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

15 Object, class, basics (c) Eraj Basnayake
the end Object, class, basics (c) Eraj Basnayake


Download ppt "Key Concepts from 1301: What you should already know"

Similar presentations


Ads by Google