Object Oriented Programming

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
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.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
CS-2135 Object Oriented Programming
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
L 3.1 Forms of Inheritance Inheritance is used in a variety of way and for a variety of different purposes. Inheritance for Specialization Inheritance.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Object-Oriented Programming: Inheritance
Final and Abstract Classes
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Review Session.
Road Map Introduction to object oriented programming. Classes
Week 4 Object-Oriented Programming (1): Inheritance
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
ATS Application Programming: Java Programming
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Nested class.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
CSC 143 Inheritance.
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
INHERITANCE.
Advanced Programming Behnam Hatami Fall 2017.
Learning Objectives Classes Constructors Principles of OOP
Object Oriented Programming: Inheritance
Computer Programming with JAVA
Java – Inheritance.
Today’s topics UML Diagramming review of terms
Chapter 8 Objects and Classes
Inheritance Cse 3rd year.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CISC/CMPE320 - Prof. McLeod
CMSC 202 Generics.
Fundaments of Game Design
Index Understanding static Final Inheritance Super
Inheritance and Polymorphism
NAME 436.
Object-Oriented Programming: Inheritance
Object Oriented Programming
Final and Abstract Classes
Haidong Xue Summer 2011, at GSU
C++ data types.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Object Oriented Programming

Topics to be covered today Inheritance

Inheritance One of the pillars of object-orientation. A new class is derived from an existing class: existing class is called super-class derived class is called sub-class A sub-class is a specialized version of its super- class: has all non-private members of its super-class may provide its own implementation of super-class methods Objects of a sub-class are a special kind of objects of a super-class.

Inheritance Syntax Syntax: class sub-class extends super-class { … } Each class has at most one super-class; no multi- inheritance in Java. No class is a sub-class of itself.

Example: Super-Class & Sub-Class

Example: Testing Class

Inheritance and Private Members A class may declare some of its members private. A sub-class has no access to the private members of its super-class:

Inheritance and Private Members Class B has no access to the A’s private variable j. This program will not compile:

Example: Box class The basic Box class with width, height and depth:

Example: BoxWeight class BoxWeight class extends Box with the new weight variable: Box is a super-class, BoxWeight is a sub-class.

Example: BoxWeightDemo

Another Sub-Class Once a super-class exists that defines the attributes common to a set of objects, it can be used to create any number of more specific sub- classes. The following sub-class of Box adds the color attribute instead of weight:

Referencing Sub-Class Objects A variable of a super-class type may refer to any of its sub-class objects: However, the inverse is illegal:

Example: Sub-Class Objects

Super-Class Variable Access plainbox variable now refers to the WeightBox object. Can we then access this object’s weight variable through plainbox? No. The type of a variable, not the object this variable refers to, determines which members we can access! This is illegal:

Super as Constructor Calling a constructor of a super-class from the constructor of a sub-class: Must occur as the very first instructor in the sub- class constructor:

Example: Super Constructor BoxWeight need not initialize the variable for the Box super-class, only the added weight variable:

Example: Super Constructor

Referencing Sub-Class Objects Sending a sub-class object: to the constructor expecting a super-class object:

Uses of Super Two uses of super: to invoke the super-class constructor to access super-class members super.variable; super.method(…);

Super and Hiding Why is super needed to access super-class members? When a sub-class declares the variables or methods with the same names and types as its super-class: The re-declared variables/methods hide those of the super-class.

Example: Super and Hiding

Example: Super and Hiding Although the i variable in B hides the i variable in A, super allows access to the hidden variable of the super-class:

Multi-Level Class Hierarchy The basic Box class:

Multi-Level Class Hierarchy Adding the weight variable to the Box class:

Multi-Level Class Hierarchy Adding the cost variable to the BoxWeight class:

Multi-Level Class Hierarchy

Multi-Level Class Hierarchy

Constructor Call-Order first call super-class constructors then call sub-class constructors In the sub-class constructor, if super(…) is not used explicitly, Java calls the default, parameter- less super-class constructor.

Example: Constructor Call-Order A is the super-class: B and C are sub-classes of A:

Example: Constructor Call-Order CallingCons creates a single object of the class C:

Questions?