Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to Java 2 Programming
Object Oriented Programming with Java
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.
Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
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.
Inheritance Chapter 9.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
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.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Polymorphism & Interfaces
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.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright © Curt Hill Inheritance and Polymorphism A Powerful Technique.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
COMP Inheritance Basics Yi Hong June 09, 2015.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Inheritance and Polymorphism
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
BY:- TOPS Technologies
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Polymorphism
Interfaces.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
Inherited Classes in Java
Chapter 10: Method Overriding and method Overloading
Inheritance and Polymorphism
Method Overriding and method Overloading
C++ Programming CLASS This pointer Static Class Friend Class
Final and Abstract Classes
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Presentation transcript:

Class Inheritance

The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers, this saves us a mountain of time. We do not need to build things from scratch as we develop new programs. We can “add on” to each individual layer, one building on top of each other.

Science classification system: Animal Vertebrates Mammals Primates Homo Sapiens

Animals (has certain characteristics) Vertebrates takes on all characteristics of Animals. That means the all Vertebrates are animals. Not all animals are necessarily Vertebrates. Vertebrates is a child or subclass of Animals. Animals is a parent or superclass of Vertebrates. Primates takes on all characteristics of Mammals. That means the all Primates are Mammals. Not all Mammals are necessarily Primates. Primates is a child or subclass of Mammals. Mammals is a parent or superclass of Primates. Both Mammals and Primates are subclasses of Vertebrates. Animals is the superclass of all these classes.

Video Games Consider the game: Counter-Strike: Gun Assault Rifles Automatic AK-47

Guns (has certain characteristics) Assault rifles takes on all characteristics of Guns. That means that all assault rifles are guns. Not all guns are necessarily assault rifles. Assault rifles is a child or subclass of Guns. Guns is a parent or superclass of assault rifles. Automatics takes on all characteristics of assault rifles. That means that all automatics are assault rifles. Not all assault rifles are necessarily automatics. Automatics is a child or subclass of assault rifles. Assault rifles is a parent or superclass of automatics. Both automatics and assault rifles are subclasses of guns. Guns is the superclass of both automatics and assault rifles.

class person { // body of code } class student extends person { // body of code } /* class student takes on all the properties of class person. Person is the superclass of student and student is the subclass of person. */

class person { String gender; int height; int weight; public person(char g, int h, int w) { gender = g; height = h; weight = w; } The constructor

class student extends person { String gender; int height;  All inherited from person int weight; // however, constructor is not. // constructor is never inherited automatically String name; int identification; public student(char g, int h, int w, String n; int ID) { super(g,h,w);// you can manually call superclass constructor name = n; identification = ID; } }

A subclass inherits all the methods and variables of its superclass except for the constructor. A subclass can add new private/public variables A subclass can add new private/public methods A subclass must define its own constructors A subclass can explicitly call its superclass constructor using the reserved word super() within its own constructor method.

class person { String gender; int height; int weight; public person(char g, int h, int w) { gender = g; height = h; weight = w; } class student extends person { String gender; int height;  All inherited int weight; String name; int identification; public student(char g, int h, int w, String n; int ID) { super(g,h,w); name = n; identification = ID; }

class person { String name; } What about private data? Can a subclass of person change the private data of person?

class person { String name; } class student extends person { public void setname(); //suppose this sets name } void main() { person newperson = new person(); newperson.setname(); }

A SUBCLASS CANNOT CHANGE THE PRIVATE VALUES OF ITS PARENT CLASS. IT MAY HOWEVER CALL ITS PARENTS’ MUTATOR/ACCESSOR METHODS.

Let’s deal with the constructor issue class X { int x,y; public X(int a, int b) { x = a; y = b; } class Y extends X { int s; public Y(int c) { s = c; }

void main { int num1 = 10; Y myclass = new Y(num1); } What’s the problem? The problem is that when you declare an instance of class Y, you have also declared an instance of class X, since Y inherits X. X is expecting two variables to use its constructors, which you will need to deal with.

Correct Implementation class X { int x,y; public X(int a, int b) { x = a; y = b; } class Y extends X { int s; public Y(int a, int b, int c) { super(a,b); s = c; }

Example 1: Is this ok? class X { int x,y; } class Y extends X { int s; public Y(int a, int b, int c) { x = a; y = b; s = c; }

Example 2: Is this ok? class X { int x,y; } class Y extends X { int s; public Y(int a, int b, int c) { super(a, b); s = c; }

Example 3: Is this ok? class X { int x,y; public X() { x = 0; y = 0; } class Y extends X { int s; public Y(int a, int b, int c) { s = c; x = a; y = b; }

Polymorphism and Method Overloading

class X { int x,y; public X() { x = 0; y = 0; } class Y extends X { public Y() { x = 5; y = 5; }

class X { int x,y; public X() { x = 0; y = 0; } public void method(int a, int b) { // some action } class Y extends X { double x; double y; public Y() { x = 11.5; y = 12.3; } public void method(double a) { // some action }

A subclass can override its parents’ methods and variables. However, you need to be careful when doing so. However, we need to understand that a subclass cannot change the values of its parents’ private data, it simply overrides it for its own instance. This is called method overriding. You can have multiple methods that carry the same name but take different parameters. This is called method overloading. In object oriented languages, when object can take different forms and shapes, we refer to that as polymorphism. It means that object can pass the “more than IS-A” test.

Exercise 1 Build a class called Person. Give it four private variables: String name int age double height double weight Develop a constructor that will take four values as its parameters and initialize these values. Develop a method called Country. Country will ask what country the person lives in and return a string from the desired input. Develop a method called City that will also intake a string and return that string.

Exercise 2 Develop a class called Student that extends Person. Student is to have four variables. int ID double GPA String city String country Build a constructor with no parameters that asks what the GPA is and asks what the ID is. Run the Country method from you superclass and set it equal to country. Run the City method from your superclass and set it equal to city.

Exercise 3 Develop a new methods called Country and City under class Student.. Country now takes two parameters, String name, String CapitalCity, combines both of them together separated by a comma, and returns the values as one string. City now takes two parameters, String name and int population. It no longer returns a value but sets the value of name to city and returns the value of population as an integer.