Sections Inheritance and Abstract Classes

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Basics Prof. Ankur Teredesai, Computer Science Department, RIT.
Advertisements

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.
Inheritance Inheritance Reserved word protected Reserved word super
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.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 11 Classes Continued
ITEC200 – Week03 Inheritance and Class Hierarchies.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object-oriented Programming Concepts
Chapter 10 Classes Continued
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.
© 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.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance using Java
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
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.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
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
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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.
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
1 Section 11.4 Java Interfaces – The Implementation Perspective Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Polymorphism in Methods
Web Design & Development Lecture 9
Sections Basic Concepts of Programming
Interface, Subclass, and Abstract Class Review
Interfaces.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Inheritance and Polymorphism
Java Anonymous inner class
Section 11.1 Class Variables and Methods
Section 3.2c Strings and Method Signatures
Object-Oriented Programming: Polymorphism
Object Oriented Programming
Lecture 14 - Abstract Classes
CSC 205 Java Programming II
Object-Oriented Programming: Polymorphism
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Inheritance and Polymorphism
Presentation transcript:

Sections 11.7 - 9 Inheritance and Abstract Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

Inheritance and Abstract Classes Inheritance reduces code duplication. Abstract class: cannot be instantiated. Concrete class: extends a class and are instantiated. Abstract methods: methods in an abstract class for which you cannot write any code. Final method: cannot be overridden by a subclass.

Some Observations About Interfaces, Inheritance, and Relationships Among Classes A Java interface has a name and consists of method headers. One or more classes can implement the same interface. If a variable is declared to be interface, it can be associated with an object from any class that implements the interface. If a class implements an interface, so do its subclasses.

A subclass inherits the characteristics of its superclass. Some Observations About Interfaces, Inheritance, and Relationships Among Classes (continued) A subclass inherits the characteristics of its superclass. A subclass can add new variables and methods or modify inherited methods. Characteristics common to several classes can be collected in common abstract superclass that is never instantiated. Abstract class can contain headers for abstract methods implemented in subclasses.

Finding the Right Method: Some Observations About Interfaces, Inheritance, and Relationships Among Classes (continued) Finding the Right Method: When a message is sent to an object, Java looks for a matching method. Starts in object’s class, continues up hierarchy. Implementation, Extension, Overriding, and Finality: Each subclass is forced to implement the abstract methods in its superclass.

Some Observations About Interfaces, Inheritance, and Relationships Among Classes (continued) Implementation, Extension, Overriding, and Finality (cont): There are two kinds of extension: The subclass method does not exist in the superclass. The subclass method invokes the same method in the superclass and extends the superclass’s behavior with its own operations. Overriding: the subclass method is a replacement of the superclass method.

Some Observations About Interfaces, Inheritance, and Relationships Among Classes (continued) Implementation, Extension, Overriding, and Finality (cont): A final method is complete and cannot be modified by the subclasses. Working Without Interfaces: Interfaces are useful but not necessary. Hierarchies of interfaces are used to organize behavior and hierarchies of classes to maximize code reuse.

Some Observations About Interfaces, Inheritance, and Relationships Among Classes (continued) Dependency: an object of once class can send a message to an object of another class. Aggregation or has-a: an object of one class can contain objects of another class as structural components. Inheritance or is-a: an object’s class can be a subclass of a more general class.

Some Observations About Interfaces, Inheritance, and Relationships Among Classes (continued) Three types of relationships among classes

Acceptable Classes for Parameters and Return Values The rules of Java as enforced by the compiler state that in any situation when an object of class BBB is expected, it is acceptable to substitute an object of a subclass but never of a superclass. A subclass of BBB inherits BBB’s methods. No guarantees about the methods in the superclass. References to objects can be passed to and returned from methods. 10 10