Inheritance using Java

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Abteilung für Telekooperation Softwareentwicklung 2 UE SE2UE_ Vererbung (Inheritance)
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
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.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
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.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Intro to OOP with Java, C. Thomas Wu
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Coming up: Inheritance
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object-Oriented Programming: Inheritance and Polymorphism.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Sections Inheritance and Abstract Classes
OOP: Encapsulation &Abstraction
Lecture 12 Inheritance.
Inheritance and Polymorphism
UML Class Diagram: class Rectangle
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Inherited Classes in Java
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Java – Inheritance.
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Presentation transcript:

Inheritance using Java Terms source: http://www.learn-java-tutorial.com/Java-Inheritance.cfm Java Programming Guidelines 4/21/2017

Java Programming Guidelines Why inheritance? to model hierarchies found in the real world to allow customization for some features, and have some features that are common to all Also helpful with maintenance old code depends on base class specializations are made derived classes and distributed Example: if code is written for Fruit then it will work for Apple now, and any other fruit instance that is added in the future Java Programming Guidelines 4/21/2017

Java Programming Guidelines inheritance example class BaseClass { private int baseX; public int f(); protected int baseG() { …} public BaseClass(int baseXin) { …} // constructor } class DerivedClass extends BaseClass { private int derY; public int f() { // overridden implementation } public void derivedGG() { derY = baseG(); } // Note: baseX variable is NOT accessible in this class Java Programming Guidelines 4/21/2017

Java Programming Guidelines Access for Protected A protected field or method is visible to a derived class of the base class. A protected field also means that all classes in the same package can also access it. Package private: If no modifier is used, only classes in the same package can use it Java Programming Guidelines 4/21/2017

inheritance example: using super class DerivedClass extends BaseClass { private int derY; DerivedClass(int xIn, derYIn ) { super (xIn); derY = derYin; } … In a class constructor you can reuse the superclass constructor and overridden superclass methods by using the reserved word super. this reference to superclass constructor must be the first line of code in the subclass constructor. Java Programming Guidelines 4/21/2017

Abstract method in a Superclass class DerivedClass { private int derY; DerivedClass(int yIn, derYIn ) { super (yIn); derY = derYin; } void commonWork() { ….} abstract void doComplexWork(); … If a method is abstract, the entire class is abstract Java Programming Guidelines 4/21/2017

Java Programming Guidelines Abstract Superclass abstract class DerivedClass { private int derY; DerivedClass(int yIn, derYIn ) { super (yIn); derY = derYin; } void commonWork() { ….} abstract void doComplexWork(); … If a class is declared abstract, the entire class is abstract Java Programming Guidelines 4/21/2017

Abstract class vs Abstract method may or may not have abstract methods can have concrete methods are incomplete by themselves and need to be completed by a subclass cannot be instantiated Abstract method Has no definition in the class Has to be implemented in a derived class Java Programming Guidelines 4/21/2017

Java Programming Guidelines Superclass is generic a derived class instance can be used in any place a base class instance is expected as a variable, a return value, or argument to a method derived classes can change the behavior of existing methods (which is called overriding the methods) The superclass is supposed to be more general than its subclass(es). as it contains elements and properties common to all of the subclasses. Java Programming Guidelines 4/21/2017