Terms and Rules II Professor Evan Korth New York University (All rights reserved)

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Inheritance Reserved word protected Reserved word super
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 interfaces Professor Evan Korth. review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
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.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
Computer Science I Inheritance Professor Evan Korth New York University.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
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.
Inheritance in the Java programming language J. W. Rider.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
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.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance and Polymorphism
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance and Polymorphism
Interfaces Professor Evan Korth.
abstract classes and casting objects
Road Map Inheritance Class hierarchy Overriding methods Constructors
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Java – Inheritance.
Java Inheritance.
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Terms and Rules II Professor Evan Korth New York University (All rights reserved)

inheritance In object orientated programming, inheritance refers to the process by which one class automatically gets the members of another class without having to re-declare the members in the new class. Thus inheritance, fosters code reuse on a large scale. It also requires us to think about which members are shared between the classes we design. The keyword extends is used in Java for inheritance.

Inheritance (cont) A class that extends another class is said to be a subclass. It is also known as a child class or a derived class. The class it extends is called a superclass. Sometimes the superclass is called a base class or a parent class. So a subclass inherits the non-private members of it’s superclass. Typically, the subclass will also add members giving it more functionality and making it more specific than it’s superclass. A superclass can also modify its method members.

Keyword super The keyword super can be used in a subclass to refer to members of the subclass’ superclass. It is used in two ways: –To invoke a constructor of the superclass (more about this on the next slide). For example (note: the keyword new is not used): super (); super (params); –To call a superclass’ method (it is only necessary to do so when you override (more on overriding soon too) the method). For example: super.superclassMethodName(); super.superclassMethodName(params);

Inheritance and constructors Constructors do not get inherited to a subclass. Instead, the subclass’ constructor automatically calls it’s superclass’ default constructor before it executes any of the code in it’s own constructor. If you do not want to use the default constructor, you must explicitly invoke one of the superclass’ other constructors with the keyword super. –If you do this, the call to the superclass’ constructor must be the first line of code in the subclass’ constructor. If the superclass has no default constructor, the first line of any subclass constructor MUST explicitly invoke another superclass constructor.

Overriding methods Often a subclass does not want to have the same exact method implementations as it’s superclass. In this case, the subclass can override the methods that it wants to change. To override a method, you must re-declare the method in the subclass. The new method must have the exact same signature as the superclass’ method you are overriding. Only non-private methods can be overridden because private methods are not visible in the subclass. Note: You cannot override static methods. Note: You cannot override fields. In the above two cases, when you attempt to override them, you are really hiding them.

Object class In Java, all classes are derived from other classes except the object class which is the top of Java’s class hierarchy. Therefore, if a new class does not explicitly extend another class, it implicitly extends the object class. Several of the methods provided in the object class are provided with the intention that they will be overridden.

Object class: equals method public boolean equals (Object object) object’s equals method returns true if the objects are the same object (ie the two variables refer to the same position in memory) Since you can already check for that condition with the == operator, you are meant to override the equals method with one that will check to see if the objects have the same fields.

Object class: toString method public String toString () object’s toString method returns the name of the class of the object plus sign and a number representing the object. You should override toString to return a string that more closely represents the object. Whenever you print an object, the result of method toString is what will be printed.

Overload vs override Overloading a method refers to having two methods which share the same name but have different signatures. Overriding a method refers to having a new implementation of a method with the same signature in a subclass.

Visibility modifiers for data and methods (review) Public means the data / method is visible to everything. No modifier (default) means the data / method is visible in the package in which it is declared. Private means the data / method is visible only within the class in which it is defined. –Trying to access private data from another class will result in a compile time error.

Visibility modifiers for data and methods (cont) Protected means the data / method is visible: –In any class in the same package –In any of the class’ subclasses (even if the subclasses are in different packages) –Note: When a protected member is overridden, it’s visibility can be changed to public to allow greater access but not less access (less access is called weaker access privileges in Java terminology).

Visibility modifiers for data and methods (cont) private – only seen in class (UML -) default (no visibility modifier) – private access plus classes in the package (UML none) protected – default access plus subclasses (UML #) public – protected access plus everything else (UML +) Weakest access privieges Strongest access privieges

final modifier The many faces of the final modifier –When applied to a data field, final means the field is a constant. –When applied to a class, final means the class cannot be extended. –When applied to a method, the method cannot be overridden. –When applied to a variable that is local to a method, final means the variable is a constant.

Keyword abstract An abstract class is a class that cannot be instantiated. An abstract method is a method signature without an implementation. Any class that contains an abstract method must itself be abstract. However, an abstract class can contain both abstract and concrete methods. Any non-abstract sub-class of an abstract class must provide an implementation for every abstract method of it’s superclass. (UML italics)

NOTE An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract. In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass.

NOTE A subclass can be abstract even if its superclass is concrete. For example, in the book, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract.

NOTE You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of GeometricObject type which is an abstract class, is correct (see book). GeometricObject[] geo = new GeometricObject[10];

polymorphism An object of a subclass can be used by any code designed to work with an object of its superclass. This feature is known as polymorphism (from a Greek word meaning “many forms”).

Dynamic binding The Java Virtual Machine can figure out which (possibly overloaded) method to use at run time. Dynamic binding allows us to use variables of superclasses which refer to objects of its various subclasses. When we use such a superclass variable in a method call, the JVM will choose the correct implementation of the method to use. –It starts with the “sub-most” class and works its way up until a method with the correct signature is found.

Choosing the method The compiler is responsible for choosing which method signature will be used for a given method call. It must make sure the method call’s parameter list matches a parameter list of one of the method declarations. The JVM is responsible for binding the method call to the proper implementation of the given method signature.

Casting objects Classes variables can refer to objects of related classes as long the object has an “is a” relationship to the class. Since subclasses have an ‘is-a’ relationship with their superclass, we can always point a superclass variable to a subclass object. In such case a casting operator is not necessary. You implicitly cast the object to it’s superclass just by using the gets operator in an assignment statement. For example: Superclass sup = new Subclass(); or Superclass sup = subclassVar;

Casting objects (cont) On the other hand, an object referenced by a superclass variable may or may not be a subclass object. To attempt an assignment such as: Subclass sub = superclassVar; You must explicitly cast the superclass object to the subclass type. For example: Subclass sub = (Subclass) supclVar; If the variable supclVar in the example above is not an instance of Subclass, a runtime error will occur.

Casting objects (cont) In order to help prevent the type of runtime error described in the last slide, Java has the keyword instanceof, which is used to test a variable to see if it is an instance of a class. For example: (supclVar instanceof Subclass) will evaluate to true if and only if supclVar is an instance of class Subclass. So we can test a variable to see if it is an instance of a class before we make an explicit cast. For example: if (supclVar instanceof Subclass) Subclass sub = (Subclass) supclVar; Will never cause a runtime error because the assignment will only happen in the event that supclVar is an instance of Subclass.

Interfaces In Java, only single inheritance is permitted. However, Java provides a construct called an interface which can be implemented by a class. Interfaces are similar to abstract classes (we will compare the two soon). A class can implement any number of interfaces. In effect using interfaces gives us the benefit of multiple inheritance without many of it’s problems. Interfaces are complied into bytecode just like classes. Interfaces cannot be instantiated. Can use interface as a data type for variables. Can also use an interface as the result of a cast operation. Interfaces can contain only abstract methods and constants.

Interfaces (cont) An interface is created with the following syntax: modifier interface interfaceID { //constants/method signatures }

public class Point extends Object implements Shape, Comparable {

Interfaces (cont) An interface can extend other interfaces with the following syntax: modifier interface interfaceID extends comma-delimited-list-of- interfaces { //constants/method signatures } Obviously, any class which implements a “sub- interface” will have to implement each of it’s “super-interfaces”

InterfaceAbstract class FieldsOnly constantsConstants and variable data MethodsNo implementation allowed (no abstract modifier necessary) Abstract or concrete Interface vs. abstract class

Interface vs. abstract class (cont) InterfaceAbstract class InheritanceA subclass can implement many interfaces A subclass can inherit only one class Can extend numerous interfaces Can implement numerous interfaces Cannot extend a class Can extend one class

Interface vs. abstract class (cont) InterfaceAbstract class Rootnoneobject namesAdjective or Nouns Nouns