Inherited Classes in Java

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.
OOP: Inheritance By: Lamiaa Said.
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.
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.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
LECTURE 7: INHERITANCE CSC 212 – Data Structures.
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.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
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.
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.
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.
Object Oriented Programming Lecture 5: BallWorld.
CSC 142 Computer Science II Zhen Jiang West Chester University
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
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.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
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.
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.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance.
Advanced Programming in Java
Class Inheritance Part I
OOP: Encapsulation &Abstraction
Objects as a programming concept
Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
Week 8 Lecture -3 Inheritance and Polymorphism
CSC 143 Inheritance.
Inheritance Chapter 5.
Class Inheritance (Cont.)
Simple Classes in C# CSCI 293 September 12, 2005.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
OO Design with Inheritance
Simple Classes in Java CSCI 392 Classes – Part 1.
Object Oriented Programming
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
Lecture 10 Concepts of Programming Languages
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inherited Classes in Java CSCI 392 Classes - Part 2

Recap from last week… Scope of Fields and Methods can be Constructors 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 their base class's list of methods and fields, and can also replace inherited methods. Animal Fish Mammal

Why use Inheritance? So that you don't have to rewrite a bunch of 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". "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 methods marked protected or public.

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

Jargon Alert Overloading Overriding 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

Override Overload public class SuperClass { public void method1 () { ... } public void method1 (int param1) public void method2 () } public class SubClass extends SuperClass 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 method2 (int param2) ... SubClass bob = new SubClass(); bob.method1(); bob.method2(); bob.method1(99);

Using the Parent's Constructor public class ParentClass { public ParentClass (int param) { do a bunch of work } ... } ================================== public class ChildClass extends ParentClass public ChildClass (int param) super (param)