Object Oriented Programming

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Lecture 5: Interfaces.
Polymorphism Method overriding Method overloading Dynamic binding 1.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
UML Class Diagram: class Rectangle
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
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
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
Programming Pillars Introduction to Object- Oriented Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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 Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Abstract classes and interfaces
Web Design & Development Lecture 9
Final and Abstract Classes
Polymorphism, Abstract Classes & Interfaces
Interface.
Road Map Inheritance Class hierarchy Overriding methods Constructors
UML Class Diagram: class Rectangle
Abstract classes and interfaces
Interfaces.
Polymorphism, Abstract Classes & Interfaces
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Final and Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Object Oriented Programming Inheritance

Single Inheritance The Java model of programming makes extensive use of Inheritance Normal inheritance plays two roles in programming. When class B inherits from class A, it “reuses” all the non-private methods and members of class A. B also becomes a subtype of A.

Inheritance Hierarchies The standard way of drawing out inheritance is through a tree-like hierarchy. In UML the arrows point from the subclass to the superclass. This is because the superclass doesn’t generally know of all of its subclasses but the subclasses know of the superclass.

Inheritance for Code Reuse The first side effect of inheritance is gaining “copies of” non-private members. This means that if A had a public method get() then B will also have a public method get().

Virtual methods One of the powers of Java is that you don’t always have to use the methods defined by the superclass. You can override them in the subclass. Methods that can be overridden are called virtual methods. By default all methods in Java are virtual, which means they can all be overriden.

Inheritance for Subtyping Inheritance also provides subtyping. This is because the subclass has all the public methods and members of the superclass. Formally, when we say that B is a subtype of A, what we are saying is that any place in the code where an A is expected, a B can be used, or a B can always take the place of an A.

Single Inheritance of Classes Java only allows single inheritance of classes i.e. a class can only inherit from one superclass This greatly simplifies code by reducing ambiguity. C++ has multiple inheritance which causes one to frequently need to specify which superclass of a given class a method should be called through.

Inheritance examples Superclass Subclasses Student GraduateStudent, UndergraduateStudent Shape Circle, Triangle, Rectangle Loan CarLoan, HomeLoan, MortgageLoan Employee Faculty, Staff BankAccount CurrentAccount, SavingsAccount

Sample UML single inheritance hierarchy

Multiple inheritance Java uses interfaces instead of pure multiple inheritance Classes in Java can only inherit from one single class (single inheritance) but can inherit from multiple interfaces This prevents the multiple inheritance problem where for example two superclasses of class D inherit from A resulting in conflicts in class D, as shown in figure below.

Fig: multiple inheritance problem D

Defining interfaces We use the interface keyword to define an interface e.g. public interface InterfaceName { variable declaration; methods declaration; }

An interface is a kind of a class The difference is that interfaces only define abstract methods and final fields i.e. data fields are all constants and methods don’t have a definition It is the responsibility of the class that implements an interface to define the code for the methods

Example public interface Item { static final int code = 1001; float compute(float x, float y); void show(); }

Extending interfaces Just like classes, interfaces can be extended The only difference is that the extending class (subinterface) must be an interface and not an ordinary class The new subinterface will inherit all members of the superinterface We use the keyword extends to subinterface an interface e.g.

Example

public interface ItemConstants { int code = 1001; String name = "Fan"; } public interface Item extends ItemConstants void display();

NB: Although keywords final and static are absent, fields in the two interfaces above are treated as constants Subinterfaces cannot implement inherited methods because they are still interfaces

Implementing interfaces We inherit properties of interfaces into classes as follows class classname implements interfacename { body of classname }

Using interfaces can make our shapes program a lot easier e.g. public interface Area { final static float pi = 3.14F; float compute(float x, float y); } class Rectangle implements Area { public float compute(float x, float y){ return(x*y);

public class Circle implements Area { public float compute(float x, float y) return(pi*x*x); }

public class InterfaceTest { public static void main(String args[]) { Rectangle rect = new Rectangle(); Circle circle = new Circle(); Area area; area = rect; System.out.println("Area of Rectangle = "+area.compute(10,20)); area = circle; System.out.println("Area of Circle = "+area.compute(10,0)); }

Accessing interface variables You can directly access interface variables from a class e.g. interface A { int m = 10; int n = 50; } class B implements A { int x = m; void methodB(int size) { … if(size<n)

Case Study: Student System

class Student { int rollNumber; void getNumber(int n) rollNumber = n; } void putNumber() System.out.println("Roll No: "+rollNumber);

class Test extends Student { float part1, part2; void getMarks(float m1, float m2) { part1 = m1; part2 = m2; } void putMarks() { System.out.println("Marks obtained "); System.out.println("Part 1 = "+part1); System.out.println("Part 2 = "+part2);

interface Sports { float sportWt = 6.0F; void putWt(); }

class Results extends Test implements Sports { float total; public void putWt() { System.out.println("Sports Weight = "+sportWt); } void display() { total = part1 + part2 + sportWt; putNumber(); putMarks(); putWt(); System.out.println("Total score = "+total);

class Hybrid { public static void main(String args[]) Results student1 = new Results(); student1.getNumber(1234); student1.getMarks(27.5F, 33.0F); student1.display(); }

Output Roll No: 1234 Marks obtained Part 1 = 27.5 Part 2 = 33.0 Sports Weight = 6.0 Total score = 66.5