More on Creating Classes part 3

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

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 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.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
04-Intro-Object-Oriented-In-Java1 Barb Ericson Georgia Institute of Technology Sept 2009 Introduction to Object-Oriented Programming in Java.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Chapter 8 More Object Concepts
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Methods.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part1 Barb Ericson Georgia Institute of.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Creating and Modifying Text part 2
Road Map Inheritance Class hierarchy Overriding methods Constructors
Barb Ericson Georgia Institute of Technology Dec 2009
Introduction to Object-Oriented Programming in Java
CS 302 Week 11 Jim Williams, PhD.
Chapter 9 Inheritance and Polymorphism
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Extending Classes.
Java Programming Language
Inherited Classes in Java
Teaching Java using Turtles part 3
Java Programming, Second Edition
Barb Ericson Georgia Institute of Technology Oct 2005
Chapter 10: Method Overriding and method Overloading
Barb Ericson Georgia Institute of Technology June 2005
More on Creating Classes
Method Overriding and method Overloading
Barb Ericson Georgia Institute of Technology Oct 2005
Presentation transcript:

More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Oct 2005 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Computing concepts Inheriting from a class The implicit call to super() Calling parent constructors explicitly Overriding a parent method How methods invocations are resolved Georgia Institute of Technology

Creating an Inherited Class Create a class ConfusedTurtle that inherits from the Turtle class But when a ConfusedTurtle object is asked to turn left, it should turn right And when a ConfusedTurtle object is asked to turn right, it should turn left Georgia Institute of Technology

Inheriting from a Class To inherit from another class Add extends ClassName to the class declaration public class ConfusedTurtle extends Turtle { } Save in ConfusedTurtle.java Try to compile it Georgia Institute of Technology

Georgia Institute of Technology Compile Error? If you try to compile ConfusedTurtle you will get a compiler error Error: cannot resolve symbol symbol: constructor Turtle() location: class Turtle Why do you get this error? Georgia Institute of Technology

Inherited Constructors When one class inherits from another all constructors in the child class will have an implicit call to the no-argument parent constructor as the first line of code in the child constructor Unless an explicit call to a parent constructor is there as the first line of code Using super(paramList); Georgia Institute of Technology

Why is an Implicit Call to Super Added? Fields are inherited from a parent class But fields should be declared private Not public, protected, or package visibility Lose control over field at the class level then But then subclasses can’t access fields directly How do you initialize inherited fields? By calling the parent constructor that initializes them Using super(paramList); Georgia Institute of Technology

Explanation of the Compile Error There are no constructors in ConfusedTurtle So a no-argument one is added for you With a call to super(); But, the Turtle class doesn’t have a no-argument constructor All constructors take a world to put the turtle in So we need to add a constructor to ConfusedTurtle That takes a world to add the turtle to And call super(theWorld); Georgia Institute of Technology

Add a Constructor that takes a World public class ConfusedTurtle extends Turtle { /** * Constructor that takes a world and * calls the parent constructor * @param theWorld the world to put the * confused turtle in */ public ConfusedTurtle(World theWorld) super (theWorld); } Georgia Institute of Technology

Georgia Institute of Technology Try this Out Compile ConfusedTurtle It should compile Try it out It should ask just like a Turtle object How do we get it to turn left when asked to turn right? And right when asked to turn left? Use super.turnLeft() and super.turnRight() super is a keyword that means the parent class Georgia Institute of Technology

Georgia Institute of Technology Resolving Methods SimpleTurtle: Class turnLeft() turnRight() forward() toString() When a method is invoked (called) on an object The class that created the object is checked To see if it has the method defined in it If so it is executed Else the parent of the class that created the object is checked for the method And so on until the method is found Super means start checking with the parent of the class that created the object Turtle: Class drawSquare() toString() ConfusedTurtle: Class turnLeft() turnRight() Georgia Institute of Technology

Confused Turtle turnLeft and turnRight /** * Method to turn left (but confused turtles * turn right) */ public void turnLeft() { super.turnRight(); } * Method to turn right (but confused turtles * turn left) public void turnRight() super.turnLeft(); Georgia Institute of Technology

Try out ConfusedTurtle > World earth = new World(); > Turtle tommy = new Turtle(earth); > tommy.forward(); > tommy.turnLeft(); > ConfusedTurtle bruce = new ConfusedTurtle(earth); > bruce.backward(); > bruce.turnLeft(); > bruce.forward(); > tommy.turnRight(); > bruce.turnRight(); Georgia Institute of Technology

Georgia Institute of Technology Override Methods Children classes inherit parent methods The confused turtle knows how to go forward and backward Because it inherits these from Turtle Children can override parent methods Have a method with the same name and parameter list as a parent method This method will be called instead of the parent method Like turnLeft and turnRight Georgia Institute of Technology

Georgia Institute of Technology What is Happening? Each time an object is asked to execute a method It first checks the class that created the object to see if the method is defined in that class If it is it will execute that method If it isn’t, it will next check the parent class of the class that created it And execute that method if one if found If no method with that name and parameter list is found it will check that classes parent And keep going till it finds the method Georgia Institute of Technology

Georgia Institute of Technology Method Overloading SimpleTurtle: Class forward() backward() turnLeft() turnRight() Obj: Turtle ------------- class Turtle: Class drawSquare() tommy When you ask tommy to go forward the Java Virtual Machine will first check if the class Turtle has a method forward() and since it doesn’t it will check the class SimpleTurtle which does have this method. When you ask bruce to go forward() the Java Virtual Machine will first check if the class ConfusedTurtle has the method forward() and since it doesn’t it will check the class Turtle and since that also doesn’t have that method it will check the class SimpleTurtle and find it. When we ask bruce to turnLeft() it will execute the turnLeft() in ConfusedTurtle which executes super.turnLeft() which will check in Turtle (the parent class) for turnLeft() and then in SimpleTurtle. Obj: ConfusedTurtle --------------------------- class ConfusedTurtle: Class turnLeft() turnRight() bruce Georgia Institute of Technology

Georgia Institute of Technology Exercise Create an OppositeTurtle class That turns left when asked to turn right And right when asked to turn left And goes backward when asked to go forward And forward when asked to go backward Georgia Institute of Technology

Georgia Institute of Technology Summary Use the extends keyword to specify the parent class When you declare a class public class ConfusedTurtle extends Turtle A class inherits methods and fields from a parent class But doesn’t have direct access to private fields and methods A implicit call to the no-arg parent constructor will be added If there isn’t a call to super(paramList) as the first line of code in a child constructor A class can override a parent method To be called instead of the parent method Using the same method name and parameter list as a parent method A method can invoked a parent’s method Using super.methodName(paramList); Georgia Institute of Technology