These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Classes and SubClasses Super Parent Sub Child IS - A.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Java Class and Inheritance.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS503: Fourth Lecture, Fall 2008 Advanced OOP and Lab. Michael Barnathan.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Ruby (on Rails) Slides modified by ements-2ed.shtml) 1.
“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.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Topic 4 Inheritance.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces and Polymorphism CS 162 (Summer 2009).
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
When is a class not a class? When it is either abstract or an Interface.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Written by: Dr. JJ Shepherd
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
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.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Polymorphism 11-Nov-18.
Recitation 6 Inheritance.
Overloading and Constructors
Object Oriented Programming (OOP) LAB # 8
Advanced Programming Behnam Hatami Fall 2017.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Java Inheritance.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Programming in C# CHAPTER 5 & 6
Presentation transcript:

These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.

What are some collections that occur in life?

A class is a collection of variables and methods. A blueprint that can be used to create objects. Something that encapsulates those things that defines the aspects and behavior of things in real life. Example: Building, Animal, Vehicle, Electronic Device, Person

A way to make use of code already created in other classes by adding variables and methods to sub classes. Those variables and methods add complexity and better define the objects are created. We have sub classes in real life: Building -> House Animal -> Dog Vehicle -> Train Electronic Device -> MP3 Player Person -> Student

First you start off with a Parent, Base, or as it is sometimes called a Super Class. Then you extend that with a Child, Derived, or as it is sometimes called a Sub Class. This is done by using the keyword extends after the class name Lets take a look at the code on the next two slides.

public class Parent{ public int x; private int y; protected int z; public Parent(int x, int y, int z){ this.x = x; this.y = y; this.z = z; } public void addToY(int n){ y += n; } public String toString(){ return "x = " + x + ", y = " + y + ", z = " + z; }

public class Child extends Parent{ private int a; public Child(int a, int x, int y, int z){ super(x, y, z); this.a = a; } public void addToAll(int n){ a += n; x += n; addToY(n); z += n; } public String toString(){ return super.toString() + ", a = " + a; }

Notice that you indicate the Child class is a subclass of the Parent class by the key word extends The constructor of the Parent class is called from the Child class using the key word super. It has to be the first line of code. x and z are directly accessible from the Child class. y is not because it is declared private so the method addToY is called instead. x is public so it is accessible even outside of the class. z is protected so it is not accessible outside of the class but is accessible by all sub classes.

Child classes can redefine how methods of the Parent classes function by redefining them. They have to have the same name and the same input variables. Ex: The toString method is overridden in the Child class. public String toString(){ return super.toString() + ", a = " + a; } Notice the methods of the Parent class can still be referenced by using the key word super. Variables can be overridden in the same way and super can again be used to reference variables of the parent class.

Parent and Child Classes can be declared and created in the following way: Parent dad = new Parent(1, 1, 1); Child son = new Child(5, 5, 5, 5); Parent father = new Child(10, 10, 10, 10); The one that is NOT allowed is the following: Child sibling = new Parent(15, 15, 15); The logic behind this is that Animals can be Animals, Dogs can be Dogs, Animals can be Dogs, but when you say something is a Dog then you can’t assign a generic Animal to it.

The constructor of the sub class will many times need to call the constructor of the super class. This can be done using only the keyword supper. This also has to be the first line of code in the constructor. Ex: public Child(int a, int x, int y, int z){ super(x, y, z); //notice super is called first this.a = a; }

JAVA by default will have every class inherit from the Object class (whether you like it or not) The object class has several methods, two of which are usually overridden: String toString() – returns a String description of the object, if this is not overridden then it will return the class name and memory address boolean equals(Object other) – returns whether or not the other object is equal, if this is not overridden then it will return whether the two objects are refering to the same thing in memory

One of the more powerful things with having multiple classes inherit from a Parent class and have everything inherit from the Object class is that you can create Collections of varied classes. Ex: ArrayList crowd = new ArrayList (); crowd.add(new Student()); crowd.add(new Teacher()); crowd.add(new Parent());

Sometimes we get an object from a collection or somewhere else and we have to determine what subclass it is. We use instanceof to do this. We can then cast a Supper class as a Sub class Ex Person thisGuy = crowd.get(0); if(thisGuy instanceof Student){ ((Student)thisGuy).doHomework(); } Note that this Guy is not told to do his homework unless they are a Student and in order to do that we must first cast him as a student.