A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
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.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Polymorphism & Interfaces
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
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 Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Object-Oriented Concepts
Abstract classes and interfaces
Advanced Programming in Java
Advanced Programming in Java
Objects as a programming concept
Inheritance and Polymorphism
Polymorphism 2nd Lecture
Object-Oriented Programming: Polymorphism
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9 Inheritance and Polymorphism
Abstract classes and interfaces
Object-Oriented Programming: Polymorphism
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Advanced Programming in Java
Java Programming, Second Edition
Java Inheritance.
Advanced Programming in Java
CMSC 202 Generics.
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Abstract Classes and Interfaces
Abstract classes and interfaces
Review of Previous Lesson
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202

Overview This attempts to show the relationships among several concepts supporting polymorphism. Abstract classes Abstract methods The class Object Interfaces Inheritance and implementation Polymorphic references Casting

Abstract Classes An abstract class must have the modifier abstract included in the class heading. An abstract class usually has at least one abstract method. You can’t create an object using an abstract class constructor, but you can use an abstract class as a base class to define a derived class.

Abstract Class as a Type An abstract class is a type. You can define variables whose type is an abstract class and you can have parameters whose type is an abstract type.

Abstract Methods Serves as a placeholder for a method that will be fully defined in a descendant class. Has a complete method heading with the addition of the modifier abstract. Has no method body but does end with a semicolon in place of a method body. Can’t be private.

Abstract Methods To summarize, the abstract keyword allows you to create one or more methods in a class that have no definitions, i.e., You provide the method signature w/o providing a corresponding implementation, which is created by inheritors.

Interface The interface keyword allows creation of a class that has no implementation at all. Consider it a “pure” abstract class. The form for a class can be established: method names, argument lists, and return types, but no method bodies. An interface can contain fields that are implicitly static and final.

Interface An interface says, “This is what all classes that implement this particular interface look like.” A class that implements an interface should provide implementation for all its methods.

The implements Keyword A class may implement as many interfaces as necessary. An interface may extend zero or more interfaces but may not extend any classes.

Inheritance in Java Single inheritance through class extension. Multiple “inheritance” through interface extension and interface implementation.

Polymorphism Enabled in Java-1 Allows a subclass object to be referenced by a supertype reference. Animal myDog = new Dog(); Allows use of polymorphic arrays and ArrayLists. for (Employee emp:everyWorker) { emp.chainOfCommand(); }

Polymorphism Enabled in Java-2 The Object class As the supertype for all objects, an Object reference can be made to all objects. Object sumpn = new Dawg(); See Example.

Polymorphism Enabled in Java-3 A supertype reference can not invoke a method that does not exist in some form in the superclass. Object sumpn = new Dawg(); sumpn.walk(); // Nope. Permissible method calls are based on the class of the reference type, not the object type. Object has no walk().

Speaking of Object . . . The Object class is the “kernel” object for every class. Think of a user-defined class as an outer shell that includes all its super- classes as “inner” shells with, of course, the Object as the innermost.

Polymorphism Enabled in Java-4 Because which method calls are permissible is based on the class of the reference type, it may be desirable to cast an object reference to its real type. Object X = new Dawg(); Dawg y= (Dawg) x; y.bark();

Polymorphism Enabled in Java-5 To make sure the cast will not be “cast out” by the compiler, the instanceof operator can be used. If(x instanceof Dawg){ Dawg y = (Dawg) x;

Polymorphism Enabled in Java-6 One way that polymorphism is enhanced by interfaces is that the objects can be from anywhere in the inheritance tree. That is, there is not a strict requirement for an inheritance hierarchy. We will hear more of this . . .

Summary The use of these capabilities to support polymorphism was discussed. Abstract methods Abstract classes The class Object Interfaces Inheritance and implementation Polymorphic references Casting