Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.

Slides:



Advertisements
Similar presentations
Chapter 1 Inheritance University Of Ha’il.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics 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.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
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.
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Inheritance using Java
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
CSC 142 Computer Science II Zhen Jiang West Chester University
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Inheritance The Basics in Java. Definition  A class that is derived from another class is called a subclass (also a derived class, extended class, or.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Topics Inheritance introduction
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Inheritance.
OOP: Encapsulation &Abstraction
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
Week 8 Lecture -3 Inheritance and Polymorphism
Java Programming Language
Simple Classes in C# CSCI 293 September 12, 2005.
Inheritance, Polymorphism, and Interfaces. Oh My
Inherited Classes in Java
Advanced Programming Behnam Hatami Fall 2017.
Simple Classes in Java CSCI 392 Classes – Part 1.
An Example of Inheritance
Lecture 8 Inheritance CS140 Dick Steflik.
C++ Programming CLASS This pointer Static Class Friend Class
More on Creating Classes part 3
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly

Recap from last week… Scope of Fields and Methods can be public, private, protected, … Constructors syntax is just like C++ optional since fields can be initialized "static" fields and methods belong to the entire class, not individual instances

What is Inheritance? Building a new class by reusing everything in an existing class. Subclasses can add to methods and fields of their base class, and can replace inherited methods. Animal Fish Mammal

Why use Inheritance? Reuse existing code Better Maintenance: Correcting/Improving code in the base class fixes all the subclasses.

Declaring an Inherited Class public class stack extends list { The new stack class is a subclass of the list class

What gets Inherited? All fields marked as "protected" or "public". All fields marked as "protected" or "public". "private" fields are only visible to the class that declared them "protected" fields are only visible to the class that declared them, and any subclasses All public and protected methods. All public and protected methods.

public class list { protected int[] values; private int size;... public class stack extends list {... public void some_method () { values[i] = myinteger; // legal size++; // illegal

Overload vs Override Alert Overloading creating multiple methods with the same name example: multiple constructors may have the same name if they have different parameters Overriding replacing inherited methods example: see next page

public class SuperClass { public void method1 () {... } public void method1 (int param1) {... } public void method2 () {... } } public class SubClass extends SuperClass { public void method1 () {... } public void method2 (int param2) {... } }

public class SuperClass { public void method1 () {... } public void method1 (int param1) {... } public void method2 () {... } } public class SubClass extends SuperClass { public void method1 () {... } public void method2 (int param2) {... } } Override Overload

public class SuperClass { public void method1 () {... } public void method1 (int param1) {... } public void method2 () {... } } public class SubClass extends SuperClass { public void method1 () {... } public void method2 (int param2) {... } }... SubClass bob = new SubClass(); bob.method1(); bob.method2(); bob.method1(99);

Using the Parent's Constructor/Method public class ParentClass { public ParentClass (int param) { do your initializations } public class ChildClass extends ParentClass { public ChildClass (int param) { super (param); super.methodName(argumentList); }

Java hierarchy The Java API uses inheritance extensively in its own classes. The Object class in the java.lang package is the superclass for all classes. Common methods of the Object class getClass()Returns a Class object that represents the type of this object. Murach’s Java SE 6, C7