Inheritance CT1513.

Slides:



Advertisements
Similar presentations
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Advertisements

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.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
OOP: Inheritance By: Lamiaa Said.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance CT Introduction 2  Inheritance  Software reusability  Create new class from existing class  Absorb existing class’s data and behaviors.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Inheritance using Java
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Intro to OOP with Java, C. Thomas Wu
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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…..
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
Inheritance CT1513.
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
UML Class Diagram: class Rectangle
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
CSC 143 Inheritance.
Inheritance Chapter 5.
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism CT1513.
Java – Inheritance.
Introduction to Java Programming
Polymorphism CT1513.
Polymorphism Polymorphism - Greek for “many forms”
Inheritance CT1513.
Java Inheritance.
An Example of Inheritance
C++ Programming CLASS This pointer Static Class Friend Class
Computer Science II for Majors
Presentation transcript:

Inheritance CT1513

Modifiers in inheritance Private(-) data members and methods for a super class cant be accessible to the subclass Protected (#) data members and methods for a super class accessible to the instances of the subclasses . Public (+) data members and methods are accessible to everyone.

Using super methods In subclass you can override (or redefine) a method That is already existed in super class. If there is no override you can call a public method of a superclass by its name and appropriate parameter list. If the sub override the super method you should call it by using the reserved key word super.methodsName()

Inheritance and Constructors public class SubClass extends SuperClass { public SubClass(………….) super (………); } **the call of the super constructor must be the first statement. Note : if you do not include statement super(). The default constructor of super will be called if any or there will be an error

Not valid No call | super();

valid

valid

valid

public class rectangle { private double length; private double width; //defoult constructor public rectangle() {length=width=0; } public rectangle( double w , double l){ setdimension(w,l); } public void setdimension( double w, double l){width=w; length=l; } public double getw(){return width;} public double getl(){return length;} public double area(){ return width*length;} public void print() {System.out.println("length =" + length+"width = " + width );} }

Create class called box inherits from rectangle having the following: Private height ,, ge_height() Default constructor and copy constructor to set Setdimension(); Area();//2*L*W + L * H +W* H Print();

Polymorphism CT1513

Introduction to polymorphism

Static Binding Vs. Dynamic Binding Binding means associating a method definition with its invocation It can be also called early binding

Note ! A A refrence variable from a class A A ref; Can point to an object of class B as well as an object of class C ref = new A(); ref = new B(); ref = new C(); B C

100 200 300 1000

In same rectangle – box example