CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Inheritance Writing and using Classes effectively.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
CS1101 Group1 Discussion 7 Lek Hsiang Hui comp.nus.edu.sg
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Inheritance & Polymorphism Android Club Agenda Inheritance Polymorphism.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
BY:- TOPS Technologies
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Polymorphism 11-Nov-18.
CSC 143 Inheritance.
Overloading and Constructors
Object Oriented Programming (OOP) LAB # 8
Polymorphism and access control
Inherited Classes in Java
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CS2011 Introduction to Programming I Objects and Classes
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Polymorphism 21-Apr-19.
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg

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

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

OO Concepts Please also refer to my past sem materials (link found on my website) CS1101X 2007 Sem1CS1101X 2007 Sem1 (discussion 1 & 10)

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…

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; }

OO ideas Constructors Class methods/variables Instance methods/variables Polymorphism –Superclass –Subclass

Scope of discussion Constructors Accessors(getters) / mutators(setters) Access rights toString Superclass, Subclasses super instanceOf casting

Constructors These are used for creating Objects and do some initialization if necessary Default constructor Non-default Constructor Constructor overloading

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 } … }

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; }.. }

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; //! }.. }

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

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

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

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 }

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 }

Access rights Member Restriction thisSubclassPackageGeneral public protected  default   private 

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

Creating sub classes class SuperClass{} class Subclass extends SuperClass{} extends

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

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

super Calling superclass’s method Or need to refer to superclass’s variable super.method(); super.variable;

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

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

Casting class parent{ } class child extends parent{ } Child c = new Child(); Parent p = new Parent(); Parent c1 = (Parent)c;