Download presentation
Presentation is loading. Please wait.
Published byEdwina Skinner Modified over 9 years ago
1
CS1101 Group1 Discussion 6 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101
2
Mastermind Hardcoding the cases i.e. if (….){ …. Usually programs you deal with doesn’t require you to do so much harcoding, if you really need to do, then maybe you are in the wrong track
3
Mastermind Double counting e.g. guess : 313 correct: 344 Assume you check using guess, 3 – correct position, correct number. 1 – wrong 3 – found number, wrong position
4
OO Concepts Please also refer to my past sem materials (link found on my website) CS1101X 2007 Sem1CS1101X 2007 Sem1 (discussion 1 & 10)
5
OO concepts - idea Modelling real world stuff into Objects e.g. Now, Classroom –Computers –People (list of Human) Students –John –Peter Teacher –Projector Made up of parts etc…
6
OO concepts - idea Take a closer look at the Human Object class Human{ String name; int age; } class Student extends Human{ //will get all the fields and methods from Human Modules[] modulesTaken; } class Teacher extends Human{ Modules[] modulesTaught; }
7
OO ideas Constructors Class methods/variables Instance methods/variables Polymorphism –Superclass –Subclass
8
Scope of discussion Constructors Accessors(getters) / mutators(setters) Access rights toString Superclass, Subclasses super instanceOf casting
9
Constructors These are used for creating Objects and do some initialization if necessary Default constructor Non-default Constructor Constructor overloading
10
Constructors – default constructor This is constructor with no parameter Take note that the name follow the same as the class name e.g. public class Car{ Color c; public Car(){ //no parameters c = new Color(“black”); //default color is black } … }
11
Constructors – non default constructor This is constructor with parameters It allows more flexibility in creating the objects e.g. public class Car{ Color c; public Car(){ c = new Color(“black”); //default color is black } public Car(Color c){ //since I used c in the parameter list, to access the c on //the extreme top, I need to do this.? this.c = c; }.. }
12
Constructors – Constructor overloading This is constructor with parameters It allows more flexibility in creating the objects e.g. public class Car{ Color c; int extra_para1, extra_para2, extra_para3; public Car(){ c = new Color(“black”); //set the color to black extra_para1 = 1; extra_para2 = 1; extra_para3 = 1; //! } public Car(Color c){ this.c = c; //this also set the color to something extra_para1 = 1; extra_para2 = 1; extra_para3 = 1; //! }.. }
13
Constructors – Constructor overloading This is constructor with parameters It allows more flexibility in creating the objects e.g. public class Car{ Color c; public Car(){ this(new Color(“black”)); } public Car(Color c){ this.c = c; extra_para1 = 1; extra_para2 = 1; extra_para3 = 1; }.. } //lesser code -> lesser bugs! //easier maintenance when there’s a change
14
Class/Instance variables public class Car{ Color c; final static int MAXLENGTH = 100; … } Instance variables are variables like c –A different one for each instance Class variables are variables like MAXLENGTH –A common variable common in all instances
15
Class/Instance methods Class methods are methods associated with the class Instance methods are methods associated with instances When to use which? –Think whether a method can still operate without instance values
16
Accessors(getters) public class Car{ private Color c; //private … public Color getColor(){ return c; } Usually instance variables are declare with private, because you want to restrict the access rights Public class Password{ private String password; //don’t want to supply a getPassword }
17
Mutators(setters) public class Car{ private Color c; //private … public void setColor(Color c){ this.c = c; } Usually instance variables are declare with private, because you want to restrict the access rights also Public class Human{ private String passportNumber; //don’t want to supply a setPassportNumber }
18
Access rights Member Restriction thisSubclassPackageGeneral public protected default private
19
toString Human a = new Human(“John”); System.out.println(a); What if you want to print out John when printing this? You could declare a special method (actually it overrides the default Object method) class Human{ private String name; public String toString(){ return name; } Notice that the method takes in no parameters, and the method has to be spelled as toString (not ToString etc) and it returns String
20
Creating sub classes class SuperClass{} class Subclass extends SuperClass{} extends
21
super Calling superclass’s method Or need to refer to superclass’s variable super.method(); super.variable; super(); //calling constructor –Take note that this has to be the 1 st statement of your constructor
22
super Calling superclass Constructor super(); or super(???, ???); –Take note that this has to be the 1 st statement of your constructor Why? Because you are creating objects when you call the constructor And subclass inherits attributes from the super class, so how can you create the subclass first before creating the superclass? –super(); is implicitly found in the default constructor even though you didn’t put it there
23
super Calling superclass’s method Or need to refer to superclass’s variable super.method(); super.variable;
24
instanceof class Animal{} class Cat extends Animal {} class Dog extends Animal {} Cat is an Animal Dog is an Animal But an Animal is not a Cat and not a Dog
25
instanceof class Animal{} class Cat extends Animal {} class Dog extends Animal {} Dog d = new Dog(); Cat c = new Cat(); Animal a = new Animal(); System.out.println(d instanceof Animal); //true System.out.println(c instanceof Animal); //true System.out.println(a instanceof Cat); //false
26
Casting class parent{ } class child extends parent{ } Child c = new Child(); Parent p = new Parent(); Parent c1 = (Parent)c;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.