Agenda Inheritance Polymorphism Type compatibility Homework.

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
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.
Reusable Classes.  Motivation: Write less code!
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
UML Class Diagram: class Rectangle
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
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.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Programming in Java
Programming in Java: lecture 7
Modern Programming Tools And Techniques-I
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Lecture 15: More Inheritance
Object-Oriented Programming
Inheritance in Java.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Interfaces.
One class is an extension of another.
CSC 143 Inheritance.
Inheritance Basics Programming with Inheritance
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
MSIS 670 Object-Oriented Software Engineering
Inheritance, Polymorphism, and Interfaces. Oh My
Virtual Functions Department of CSE, BUET Chapter 10.
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
Polymorphism CT1513.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Lecture 15: Inheritance II
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Inheritance and Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Agenda Inheritance Polymorphism Type compatibility Homework

Inheritance What is inheritance? What is the relationship between superclass and subclass? Which one is begger?(contains more data and more methods) superclass or subclass? Why do we want inheritance?

Inheritance hierarchy Can a subclass be a superclass for another subclass? How do you define a “is-a” relationship if there is a superclass Person and a subclass Male? The is-a relationship is transitive. Name an example. What is method overriding?

Implementing subclasses Which keyword is used for the inheritance relationship? Person Student Employee GradStudent UnderGrad

Inheriting instance mthods and variables Can a subclass “UnderGrad” access the superclass “Student” state variable myName directly?  Can a subclass invoke the public accessor and mutator methods of the superclass? What if the state variable myName is public can a subclass access it?  Do the classes on the same level, in this case, GradStudent and UnderGrad, inherit anything from each other?

Method overriding and super keyword What is method overriding? What is partial overriding? Why do we do partial method overriding?  Look at page 126 with the following codes public void computeGrade() { super.computeGrade(); if(getTestAverage() >= 90) setGrade(“Pass with distinction”); } where computerGrade(), getTestAverage() and setGrade() are all methods in the superclass. Can keyword “super” be eliminated from the statement super.computeGrade() like setGrade()?

Constructors and super Can constructors be inherited? If a subclass has no constructor, what happens? If a subclass has no constructor and the superlass does not have a default constructor, what happens? How to invoke the superclass constructor in subclass? If the subclass like GradStudent has all the state variables as the superclass and plus its own state variable gradeID, how to write the default constructor?

What is wrong with the following codes and why? public GradeStudent() { gradeID = 0; super(); } Can a subclass override static methods of the superclass? Can a subclass directly access the private members of its superclass?  

Declaring subclass objects Is the following legal? Student s = new Student(); Student g = new GradStudent(); Student u = new UnderGrad(); GradStudent g = new Student(); UnderGrad u = new Student(); GradStudent g = new UnderGrad();

what is polymorphism? what would be a good example in last slide? What is dynamic binding(late binding)? What is static binding(early binding)? In polymorphism, which binding is used at run time? With overloaded methods, which binding is used at compile time? On page 131, what is the box on the right saying about polymorphism?

Type compatibility Student s = new GradStudent(); Look at the statements and answer the questions Student s = new GradStudent(); GradStudent g = new GradStudent(); int x = s.getID(); int y = g.getID(); which statement will cause a compile time error and why? What can be done to fix the error? What is a downcast? What is ClassCastException?

What is wrong with the following codes? Student u = new UnderGrad(); System.out.println((String) u); int x = ((GradStudent) u).getID();

Abstract classes Can an abstract class be instantiated? Which keyword is used to declare an abstract class? Suppose we have an abstract class Vehicle and Car, Motorcyle and Truck are its subclasses. Which of the following is wrong? Why? a) Car aCar = new Car(); b) Truck aCar = new Truck(); c) Vehicle aVehicle = new Vehicle();

Look at the question 9 diagram Look at the question 9 diagram. If Person is an abstract class, any of its abstracted methods must be implemented in any subclasses Student or Employee.. If Student fails to implement all abstracted methods, what needs to be done? Is it possible for an abstract class to have NO abstract methods? An abstract class may not have constructors. Correct?

Interfaces What is an Interface? All the methods in an Interface are all public and abstract by default, correct? Assume Person is an Interface. Is it required the Student has to implement all the methods in Person, if Student is a nonabstract class? If Student fails to implement any of the methods in Person, what needs to be done to avoid compile-error?

How do you declare an interface? Which keyword do you use to implement the interface? Write the declaration of Employee which inherits class Person and implements the interface Comparable Can a class have more than on superclass? Can a class implement more than one interface? How do you declare if a class Bird is a subclass of FlyingAnimal and implements interface of Flying and Walking.

Comparable interface What data type needs to implement Comparable to do the comparison? What is the only method in the class Comparable? If two objects being compared are not type compatible, what happens?

What is the signature of a abstract method? Give an example. If any class contains any abstract method, does this class have to be declared an abstract class?

Homework Study the Barron's chapter 2 multiple-choice questions on class and objects(p103-117). There is a quiz III next Monday. Be sure to fully understand the materials. Look through the "Answers Explained" section for anything you don't understand. Turn in any homework you failed to submit.