Superclasses and Subclasses in Java Computer Science 3 Gerb Objective: Understand superclass methods and the “this” keyword in Java.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OOP: Inheritance By: Lamiaa Said.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance using Java
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance in the Java programming language J. W. Rider.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Constructors & Garbage Collection Ch. 9 – Head First Java.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Typecasting References Computer Science 3 Gerb Reference: Objective: Understand how to use the Object class in Java in the context of ArrayLists.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
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.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part I
Polymorphism.
Inheritance and Polymorphism
Java Unit 11: Inheritance I
Inheritance CT1513.
Object Oriented Programming
CSC 143 Inheritance.
Overloading and Constructors
Inheritance CT1513.
Inheritance and Polymorphism
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

Superclasses and Subclasses in Java Computer Science 3 Gerb Objective: Understand superclass methods and the “this” keyword in Java

Recall that When class2 extends class1, we say… –Class2 is a subclass of class1 –Class1 is a superclass of class2 When class2 defines a method defined in class1, we say that method is specialized. –If myMethod is specialized in class2, then anytime it is invoked on an instance of class2, class2’s version will be called, not class1’s –This could be a problem.

A dilemma Suppose we have a method myMethod in class1, which is extended by class2 –We want to specialize myMethod in class2 –But we have shut ourselves off from the myMethod in class1. –If we want to use class1’s myMethod to help us implement class2’s myMethod. We’re out of luck! Woe is us…

Super to the rescue Java provides a keyword called super Super allows us to access a method in a superclass that we specialized. E.g. super.myMethod() means “call the myMethod defined in the superclass before I specialized it.”

Example mySuper defines a method myMeth that calls the setUp.setMeUpWell function. (Note that this is a java shorthand for the object on which the method is called.) class mySuper { public void myMeth(){ setUp.setMeUpWell(this);}} mySub specializes myMeth, to call the method called setUp.setMeUpBetter, but now can’t call mySuper’s myMeth method. class mySub extends mySuper { myMeth(){ setUp.setMeUpBetter();}}

Example Cont’d class mySuper { public void myMethod(){ setUp.setMeUpWell(this);}} Can use super to call mySuper’s myMeth method: class mySub extends mySuper { public void myMethod(){ super.myMethod(); setUp.setMeUpBetter();}}

A note on the this keyword The this keyword can always be used in an instance method to refer to the instance upon which the method was called. E.g.: –Give the following definition public void aMethod() { anotherClass.anotherMethod(this);} –The following method invocation anInstance.aMethod() –Would result in anotherClass.anotherMethod being called with anInstance as a parameter.

Another way to use super Super is frequently used to call a constructor for a superclass When used in this way –Must be the first line in the subclass’s constructor –Is used by itself without a period followed by a method name

For Example class class1 { class1(int i){ … //Statements to construct class1 } class class2 extends class1 { class2(){ super(5);//Constructs according to //class1’s constructor … //Additional statements to } //Construct class2. }

Why super is needed When an instance of a subclass is created, the default constructor of the superclass is always called. Compiler error if superclass has no default constructor. Super allows you to decide which superclass constructor to call, and with which parameters.

Summary Use the super keyword to call methods in a superclass that have been overridden. Use the this keyword to refer to the object on which a method was invoked. When super is used to invoke a constructor –Must be the first statement in the subclass constructor. –Must not use a period or a method name.