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.

Slides:



Advertisements
Similar presentations
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Advertisements

1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Written by: Dr. JJ Shepherd
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 211 Inheritance AAA.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
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.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
Chapter 11 Inheritance and Polymorphism
CS1110 Classes, stepwise refinement 23 Sep 2009
Java Inheritance.
CS100J Final Class on Classes 22 September 2005
Continuing Chapter 11 Inheritance and Polymorphism
Overloading and Constructors
CS1316: Representing Structure and Behavior
Java Inheritance.
CS1316: Representing Structure and Behavior
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
Inheritance.
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Simple Classes in Java CSCI 392 Classes – Part 1.
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
CS100J Classes, stepwise refinement 21 September 2005
Chapter 11 Inheritance and Encapsulation and Polymorphism
Java Inheritance.
Review for Midterm 3.
CS100J Final Class on Classes 10 February 2005
CS 240 – Advanced Programming Concepts
Presentation transcript:

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 inherited classes 3. Apparent and Real Types (and Casting) 4. Syntax vs Semantics 1

Motivating Problem /** An instance is a zoo creature. */ public class Creature { /** This Creature's name */ private String name; /** set of Creatures in the zoo*/ public static Creature[] zoo; /** Constructor: a new Creature with name n */ public Creature(String n) { setName(n); } /** = this Creature's name */ public String getName() { return name; } /** Set this Creature's name to n */ public void setName(String n) { name= n; } /** An instance is a bird */ public class Bird extends Creature { /** set of Birds in the zoo. */ public static Bird[] aviary; /** Constructor: a Bird with name n */ public Bird(String n) { super(n); } /** = "a Bird can (usually) fly" */ public boolean canFly() { return true; } /** An instance is a penguin */ public class Penguin extends Bird{ /** Constructor: a new Penguin with name n*/ public Penguin(String n) { super(n); } /** = "a Penguin can usually fly" */ public boolean canFly() { return false; }

Folders! Penguin z = new Penguin(“ ”); z a0 Penguin canFly() Penguin(String n) Object equals();toString(); Creature name Bird canFly() Bird(String n) a0

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 inherited classes 3. Apparent and Real Types (and Casting) 4. Syntax vs Semantics 4

Inheritance Facts ● A subclass inherits ALL components (fields and methods) from the super class. ● Even private fields are inherited; they appear in each object. ● What makes them private, then? ● A subclass can override methods. ● A subclass should not override fields. It is called "shadowing the variables”. We have never seen a good use of it. Don’t do it.

Inside-out rule To find a variable, search outward through enclosing constructs until the declaration of the variable is found. In the folder representation, go bottom-up!

Constructors in subclasses The following can only appear as the first statement in a constructor: this(...); // call another constructor in this class super(...); // call a constructor in the super class If there is no explicit constructor call in a constructor, Java inserts super();. What if the super class does not have a default constructor? Note: If there is no explicit constructor declared in a class, Java inserts a default constructor.

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 inherited classes 3. Apparent and Real Types (and Casting) 4. Syntax vs Semantics

9

Apparent & Real Types (and Casting) Real type of a variable The type of the object it’s referring to. Apparent type of a variable: The type of the variable…-_- Used to tell if a reference is legal (if not, program won't compile.). v.field / v.method(...) is legal only if field / method() is defined in or inherited by the apparent class of v. Casting: changing of the apparent type What are the restrictions?

Syntax VS Semantics The validity of a statement should be checked by: SYNTAX (grammar; rules for legal programs) SEMANTICS (meaning; how legal programs are executed). Ex) Assess the following statements: a == b a.equals(b) What makes one correct, but not the other?

Running Example! Dr Java