Inheritance and Access Control CS 162 (Summer 2009)

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OOP: Inheritance By: Lamiaa Said.
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.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Computer Science I Inheritance Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Inheritance Abstract Classes Check out Inheritance from SVN.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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 Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
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.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
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 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 9 Inheritance and Polymorphism
Polymorphism and access control
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Java Programming, Second Edition
Presentation transcript:

Inheritance and Access Control CS 162 (Summer 2009)

Class Relationships Has-A (one class uses or is composed of others - composition) Implements-A (one class implements an interface shared among multiple others) Is-A (inheritance – a class to be implemented specializes a more general one

Inheritance Terminology Child class, subclass, derived class Parent class, superclass, [base class] Base class – usually, a class with no parent classes (the “root” of the hierarchy). Sometimes, this term just means parent class

Access Control public data fields and methods are accessible to any derived class (as they are accessible to any class)

Access Control private data fields and methods are not accessible to derived classes (although they are inherited) – In other words, a new method defined in a derived class cannot make use of any private data fields or methods in the parent class. – However, public methods inherited from the parent class still function correctly.

Access Control protected data fields and methods can be accessed by the object’s class and all of its subclasses (+ all other classes in the same package) – what’s the point? – in other words, protected data is not really protected at all default access control is ‘package’ – accessible to all classes in the same package

Derived Classes Data Fields Methods

Data Fields All fields from the parent class are automatically inherited, even though they might not be visible. Derived classes can add new data fields that don’t exist in the base class (using any access control scheme) Parent class data fields cannot be overridden or removed – all fields from parent class are automatically inherited

Methods All methods from the base class are automatically inherited, even though one might not be able to call them (reusability) Derived classes can override methods defined in the parent class (by supplying a different public implementation of a method that exists in the base class)

Methods Derived classes can overload methods defined in the parent class (by supplying a method that exists in the parent class but has a different signature) Derived classes can add methods that don’t exist in the base class

Overriding Methods – Overriding method must have the same signature as the method in the parent class being overridden – Objects instantiating the derived class will call the overriding method instead of the original one – Can use ‘super’ in the overriding method to call a method from the parent class (like this). eg, super.getBalance() calls getBalance() from the parent class

Overriding Methods Overriding can be prevented in derived classes with the ‘final’ keyword: – eg, public final void sendFlowersToMyLovelyWife();

Inheritance Rules Derived classes cannot remove any data fields or methods from parent classes public methods in a parent class cannot be overridden by private methods in a derived class (ie, you cannot make a public method private) No multiple inheritance in Java (see: C++/Complicated) – Exception: multiple interface implementations allowed

Inheritance Rules Parent classes cannot inherit from derived classes (no loops) Every class automatically inherits from Object – gains a toString() and an equals() method (and some others) Primitive types are not objects (wrappers required to do inheritance)

Super Used to directly call methods in the parent class Also used to call the parent class constructor: public class DeathStarV2 extends DeathStar { public DeathStarV2(int numStormTroopers) { super(numStormTroopers); // calls DeathStar constructor improvedBlinkyLights = new BlinkyLights(); … } }

Super super() must be the first line of your new constructor. If you don’t start your constructor with a call to super(something), then the default constructor is automatically called (the one with no arguments) If there is no such default constructor, you get an error

Converting subclasses to superclasses subclass -> superclass (widening – always okay) Square s = new Square(); Rectangle r = s; Object o = r;

Converting Super to Sub – superclass -> subclass (narrowing – can be dicey) Square s = new Square(); Object o = s; Rectangle r = (Rectangle) o; // cast operator Casting is a run-time check – can generate an exception if o is not actually a rectangle (as in this case)

instanceof if (o instanceof Rectangle) { Rectangle r = (Rectangle) o; } Every object is an instanceof its own class and each of its parent classes

Late Binding Java looks at the actual object type whenever methods are called/dispatched eg, Rectangle r = new Square(); float area = r.area(); // calls the area() method associated with the Square class