Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.

Slides:



Advertisements
Similar presentations
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Advertisements

Inheritance Writing and using Classes effectively.
AP Computer Science DYRT Quiz Key
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
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.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.
LECTURE 07 Programming using C# Inheritance
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
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.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Object.
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.
// Java0802.java // CardDeck Case Study #02 // Variables, called attributes or data fields, are added to the class. public class Java0802 { public static.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
What is inheritance? It is the ability to create a new class from an existing class.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
Exposure Java 2011 APCS Edition
Interfaces and Inner Classes
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Inheritance and Polymorphism
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
BY:- TOPS Technologies
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Inheritance Object Oriented.
Exposure Java 2013 APCS Edition
Lecture 12 Inheritance.
Objects as a programming concept
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Exposure Java 2013 APCS Edition Chapter 9 Slides Focus on OOP:
Exposure Java 2015 AP®CS Edition
Section 8.7 The Consequences of Scope.
Pre-AP® Computer Science Quiz Key
Pre-AP® Computer Science Quiz
Section 9.1 Introduction.
PreAP Computer Science Quiz Key
© A+ Computer Science - GridWorld The GridWorld case study provides a graphical environment where visual objects inhabit and interact.
PreAP Computer Science Quiz
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Inheritance Lakshmish Ramaswamy.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass and the new class, which inherits the superclass features, is called the subclass. superclass: Car subclasses: Truck, Limo & Racecar

“Is-A” and “Has-A” The creation of new classes with the help of existing classes makes an important distinction between two approaches. An "is-a" relationship declares a new class as a special “new- and-improved” case of an existing class. In Geometry, a parallelogram "is-a" quadrilateral with special properties. A “has-a” relationship declares a new class composed of an existing class or classes. A line "has" points, a square "has" lines, and a cube "has" squares. A truck "is-a" car, but it "has-an" engine. In computer science an "is-a" relationship involves class interaction that is called inheritance and a "has-a" relationship involves class interaction that is called composition.

/* * AP(r) Computer Science GridWorld Case Study: * Copyright(c) Cay S. Horstmann ( * * by Leon Schram * Used for Java0901 project */ import info.gridworld.actor.ActorWorld; import info.gridworld.actor.Actor; import info.gridworld.grid.Location; public class TestJava0901 { public static void main(String[] args) { ActorWorld world = new ActorWorld(); Actor actor1 = new Actor(); Actor actor2 = new Actor(); Actor actor3 = new Actor(); Actor actor4 = new Actor(); world.add(new Location(0,0),actor1); world.add(new Location(0,9),actor2); world.add(new Location(9,0),actor3); world.add(new Location(9,9),actor4); world.show(); } public class Spider { }

/* * AP(r) Computer Science GridWorld Case Study: * Copyright(c) Cay S. Horstmann ( * * by Leon Schram * Used for Java0902 project */ import info.gridworld.actor.ActorWorld; import info.gridworld.actor.Actor; import info.gridworld.grid.Location; public class TestJava0902 { public static void main(String[] args) { ActorWorld world = new ActorWorld(); Actor actor1 = new Actor(); Actor actor2 = new Actor(); Actor actor3 = new Actor(); Actor actor4 = new Actor(); world.add(new Location(0,0),actor1); world.add(new Location(0,9),actor2); world.add(new Location(9,0),actor3); world.add(new Location(9,9),actor4); world.add(new Location(4,4),new Spider()); world.add(new Location(5,5),new Spider()); world.show(); } import info.gridworld.actor.Actor; public class Spider extends Actor { }

/* * AP(r) Computer Science GridWorld Case Study: * Copyright(c) Cay S. Horstmann ( * * by Leon Schram * Used in Java0903 project */ import info.gridworld.actor.Actor; import java.awt.Color; public class Spider extends Actor { public Spider() { setColor(Color.red); } } // driving class remains the same // and will no longer be shown

Altering Object Appearance Rename SpiderX.gif to Spider.gif Now a graphics file has the same name as the class.

/* * AP(r) Computer Science GridWorld Case Study: * Copyright(c) Cay S. Horstmann ( * * by Leon Schram * Used in Java0903 project */ import info.gridworld.actor.Actor; import java.awt.Color; public class Spider extends Actor { public Spider() { setColor(Color.red); } } Re-execute the Java0903 project.

/* * AP(r) Computer Science GridWorld Case Study: * Copyright(c) Cay S. Horstmann ( * * by Leon Schram * Used in Java0904 project */ public class LazySpider extends Spider { public void act() { } This re-defines the act method. This means is will not inherit the original act method from its grandparent, the Actor class.

Subclass Methods Never alter a well-designed, and tested, existing class. Write a new subclass class to use the methods of the existing class and create new methods in your new class. Write methods in the subclass that are re-definitions of the existing superclass methods or write totally new-definitions.

// Java0905.java // This program demonstrates fundamental inheritance with. // There are no constructors yet, which results in Java handling the // construction and assigning default values to the attributes. public class Java0905 { public static void main(String args[]) { System.out.println("\nJAVA0905\n"); Student tom = new Student(); System.out.println("Person's age is " + tom.getAge()); System.out.println("Student's grade is " + tom.getGrade()); System.out.println(); } class Person { private int age; public int getAge() { return age; } } class Student extends Person { private int grade; public int getGrade() { return grade; } }

// Java0905.java // This program demonstrates fundamental inheritance with. // There are no constructors yet, which results in Java handling the // construction and assigning default values to the attributes. public class Java0905 { public static void main(String args[]) { System.out.println("\nJAVA0905\n"); Student tom = new Student(); System.out.println("Person's age is " + tom.getAge()); System.out.println("Student's grade is " + tom.getGrade()); System.out.println(); } class Person { private int age; public int getAge() { return age; } } class Student // extends Person { private int grade; public int getGrade() { return grade; } } Output with extends Person removed.

// Java0906.java // This program reverses the access between the classes. // A person object now tries to access a subclass method. public class Java0906 { public static void main(String args[]) { System.out.println("\nJAVA0906\n"); Person tom = new Person(); System.out.println("Person's age is " + tom.getAge()); System.out.println("Student's grade is " + tom.getGrade()); System.out.println(); } class Person { private int age; public int getAge() { return age; } } class Student extends Person { private int grade; public int getGrade() { return grade; } }

Access with Inheritance When two classes have an inheritance interaction then access is only possible from the subclass to the superclass members. It is not possible to access subclass members from the superclass.

// Java0907.java public class Java0907 { public static void main(String args[]) { System.out.println("\nJAVA0907\n"); Student tom = new Student(); tom.showData(); System.out.println(); } class Person { private int age; public Person() { age = 17; } } class Student extends Person { private int grade; public Student() { grade = 12; } public void showData() { System.out.println("Student's Grade is " + grade); System.out.println("Student's Age is " + age); }

// Java0908.java public class Java0908 { public static void main(String args[]) { System.out.println("\nJAVA0908\n"); Student tom = new Student(); tom.showData(); System.out.println(); } class Person { protected int age; public Person() { age = 17; } } class Student extends Person { protected int grade; public Student() { grade = 12; } public void showData() { System.out.println("Student's Grade is " + grade); System.out.println("Student's Age is " + age); }

public, private & protected Attributes & methods declared public can be accessed by methods declared both outside and inside the class. Attributes & methods declared private can only be accessed by methods declared inside the class. Attributes & methods declared protected can be accessed by methods declared inside the class or subclass.

// Java0909.java // This program adds output in the constructors to the and classes. // Note how the constructor is called, even though there does // not appear to be a object instantiated. public class Java0909 { public static void main(String args[]) { System.out.println("\nJAVA0909\n"); Student tom = new Student(); System.out.println("tom's age is " + tom.getAge()); System.out.println("tom's grade is " + tom.getGrade()); System.out.println(); } class Person { private int age; public Person() { System.out.println("Person Constructor"); age = 17; } public int getAge() { return age; } } class Student extends Person { private int grade; public Student() { System.out.println("Student Constructor"); grade = 12; } public int getGrade() { return grade; } }

// Java0910.java // This program adds a call to in the constructor. // The program output is identical to the previous program. // Java automatically makes the call to. class Student extends Person { private int grade; public Student() { super(); System.out.println("Student Constructor"); grade = 12; } public int getGrade() { return grade; } // The other 2 classes and the output are identical // to program Java0909 and are not shown here.

Inheritance and Constructor Calls When an object of a subclass is instantiated, the constructor of the superclass is called first, followed by a call to the constructor of the subclass. An invisible - to the programmer - call is made by Java to the super method, which generates a call to the superclass constructor. This statement can be written in the subclass constructor with the same results, but it is not required.

// Java0911.java // This program demonstrates how a subclass // constructor passes parameter information // to a superclass constructor. public class Java0911 { public static void main(String args[]) { System.out.println("\nJAVA0911\n"); Student tom = new Student(12,17); tom.showData(); System.out.println(); } class Person { protected int age; public Person(int a) { System.out.println( "Person Parameter Constructor"); age = a; } public int getAge() { return age; } } class Student extends Person { protected int grade; public Student(int g, int a) { super(a); // this must be the first call grade = g; System.out.println( "Student Parameter Constructor"); } public int getGrade() { return grade; } public void showData() { System.out.println("Student's Grade is " + getGrade()); System.out.println("Student's Age is " + getAge()); }

// Java0912.java // This program demonstrates inheritance at three levels. public class Java0912 { public static void main(String args[]) { System.out.println("\nJAVA0909\n"); Cat tiger = new Cat("Tiger",500,5); System.out.println(); System.out.println("Animal type: " + tiger.getType()); System.out.println("Animal weight: " + tiger.getWeight()); System.out.println("Animal age: " + tiger.getAge()); System.out.println(); } class Animal { protected int age; public Animal(int a) { System.out.println("Animal Constructor Called"); age = a; } public int getAge() { return age; } } class Mammal extends Animal { protected int weight; public Mammal(int w, int a) { super(a); weight = w; System.out.println( "Mammal Constructor Called"); } public int getWeight() { return weight; } } class Cat extends Mammal { protected String type; public Cat(String t, int w, int a) { super(w,a); type = t; System.out.println( "Cat Constructor Called"); } public String getType() { return type; } }

Multi-Level Inheritance & Multiple Inheritance The previous program showed an example of Multi-Level Inheritance. Multiple Inheritance is something different. It occurs when one subclass inherits from two or more superclasses. This feature is available in C++. It is NOT available in Java.

Multi-Level Inheritance Multiple Inheritance Animal Mammal Dog Terrier Reptile Dinosaur Extinct

// Java0913.java // In this program both the class and the class each have a method. public class Java0913 { public static void main(String args[]) { System.out.println("\nJAVA0913\n"); Person ann = new Person(); Student tom = new Student(); System.out.println("Person getData: " + ann.getData()); System.out.println("Student getData: " + tom.getData()); System.out.println(); } class Person { protected int age; public Person() { age = 21; } public int getData() { return age; } } class Student extends Person { protected int grade; public Student() { grade = 12; } public int getData() { return grade; } }

// Java0914.java // This program demonstrates that it is possible // to distinguish between two methods with the // same identifier using. public class Java0914 { public static void main(String args[]) { System.out.println("\nJAVA0914\n"); Student tom = new Student(12,17); tom.showData(); System.out.println(); } class Person { protected int age; public Person(int a) { System.out.println( "Person Parameter Constructor"); age = a; } public int getData() { return age; } } class Student extends Person { protected int grade; public Student(int g, int a) { super(a); grade = g; System.out.println( "Student Parameter Constructor"); } public int getData() { return grade; } public void showData() { System.out.println("Student's Grade is " + getData()); System.out.println("Student's Age is " + super.getData()); }

Using super The keyword super used as the first statement in a constructor passes information to the super class constructor, like super(a); The same keyword super used in front of a method indicates that a method of the superclass needs to be called, like super.getData(); Information can be passed up to multiple inheritance levels, but it can only be passed one level at one time.

SuperClass Declarations You are used to seeing the same identifier at the start and end of an object definition, like Bug barry = new Bug(); It is possible to declare an object with a superclass identifier and construct it with a subclass identifier. Actor barry = new Bug;

// Java0915.java // This program demonstrates that it is // possible to use the super class identifier // to declare each subclass object. public class Java0915 { public static void main(String args[]) { Animal tiger = new Cat("Tiger"); Animal eagle = new Bird("Eagle"); Animal shark = new Fish("Shark"); } class Animal { public Animal() { System.out.println( "Animal constructor called"); } class Cat extends Animal { protected String catType; public Cat(String ct) { System.out.println("Cat constructor called"); catType = ct; } class Bird extends Animal { protected String birdType; public Bird(String bt) { System.out.println("Bird constructor called"); birdType = bt; } class Fish extends Animal { protected String fishType; public Fish(String ft) { System.out.println("Fish constructor called"); fishType = ft; }

// Java0916.java // This program adds methods for each // one of the three sub classes. // The program does not compile, because // is not an method. public class Java0916 { public static void main(String args[]) { Animal tiger = new Cat("Tiger"); System.out.println(tiger.getType()); Animal eagle = new Bird("Eagle"); System.out.println(eagle.getType()); Animal shark = new Fish("Shark"); System.out.println(shark.getType()); } class Animal { public Animal() { System.out.println( "Animal constructor called"); } class Cat extends Animal { protected String catType; public Cat(String ct) { catType = ct; } public String getType() { return catType; } } class Bird extends Animal { protected String birdType; public Bird(String bt) { birdType = bt; } public String getType() { return birdType; } } class Fish extends Animal { protected String fishType; public Fish(String ft) { fishType = ft; } public String getType() { return fishType; } }

// Java0917.java // This program does compile, because each // object is a, or, which // does have a method. public class Java0917 { public static void main(String args[]) { System.out.println("\nJAVA0917\n"); Cat tiger = new Cat("Tiger"); System.out.println(tiger.getType()); Bird eagle = new Bird("Eagle"); System.out.println(eagle.getType()); Fish shark = new Fish("Shark"); System.out.println(shark.getType()); } class Cat { protected String catType; public Cat(String ct) { System.out.println("Cat constructor called"); catType = ct; } public String getType() { return catType; } } class Bird { protected String birdType; public Bird(String bt) { System.out.println("Bird constructor called"); birdType = bt; } public String getType() { return birdType; } } class Fish { protected String fishType; public Fish(String ft) { System.out.println("Fish constructor called"); fishType = ft; } public String getType() { return fishType; } }

// Java0918.java // This program solves the problem of the // program Java0916.java. The // superclass has a "fake" method // that is re-defined for each subclass. public class Java0918 { public static void main(String args[]) { System.out.println("\nJAVA0918\n"); Animal tiger = new Cat("Tiger"); System.out.println(tiger.getType()); Animal eagle = new Bird("Eagle"); System.out.println(eagle.getType()); Animal shark = new Fish("Shark"); System.out.println(shark.getType()); } class Animal { public Animal() { System.out.println("Animal constructor called"); } public String getType() { return ""; } } class Cat { protected String catType; public Cat(String ct) { catType = ct; } public String getType() { return catType; } } class Bird { protected String birdType; public Bird(String bt) { birdType = bt; } public String getType() { return birdType; } } class Fish { protected String fishType; public Fish(String ft) { fishType = ft; } public String getType() { return fishType; } }