AD 300 ---- Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
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.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 CS1001 Lecture Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
15-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
LECTURE 07 Programming using C# Inheritance
Abstraction, Inheritance, and Polymorphism in Java.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
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,
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Chapter 10 Inheritance and Polymorphism
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
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.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Topics Inheritance introduction
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Polymorphism Lecture - 9.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Object-Oriented Programming Concepts
Final and Abstract Classes
Interfaces.
Inheritance and Polymorphism
Week 8 Lecture -3 Inheritance and Polymorphism
Polymorphism 11-Nov-18.
Polymorphism 11-Nov-18.
Polymorphism 15-Nov-18.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Polymorphism 28-Nov-18.
Overview of C++ Polymorphism
Polymorphism 15-Apr-19.
Polymorphism 21-Apr-19.
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism

Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. A class can implement an interface, implementing all the specified methods. Inheritance implements the “is a” relationship between objects. 2

Inheritance (cont’d) 3 subclass or derived class superclass or base class extends

Inheritance (cont’d) In Java, a subclass can extend only one superclass. In Java, a subinterface can extend one superinterface 4

Inheritance (cont’d) Inheritance plays a dual role: A subclass reuses the code from the superclass. A subclass (or a class that implements an interface) inherits the data type of the superclass (or the interface) as its own secondary type. 5

Inheritance (cont’d) Inheritance leads to a hierarchy of classes and/or interfaces in an application: 6 Person WomenMan

Inheritance (cont’d) An object of a class at the bottom of a hierarchy inherits all the methods of all the classes above. It also inherits the data types of all the classes and interfaces above. Inheritance is also used to extend hierarchies of library classes, reusing the library code and inheriting library data types. 7

Encapsulation Encapsulation means that all data members (fields) of a class are declared private. Some methods may be private, too. The class interacts with other classes (called the clients of this class) only through the class’s constructors and public methods. Constructors and public methods of a class serve as the interface to class’s clients. 8

Encapsulation (cont’d) Ensures that structural changes remain local: Usually, the structure of a class (as defined by its fields) changes more often than the class’s constructors and methods. Encapsulation ensures that when fields change, no changes are needed in other classes (a principle known as “locality”). 9

10 Polymorphism Polymorphism means many (poly) shapes (morph) In Java, polymorphism refers to the fact that you can have multiple methods with the same name in the same class There are two kinds of polymorphism: Overloading Two or more methods with different signatures Overriding Replacing an inherited method with another having the same signature

11 Overload In Java, two methods have to differ in their names or in the number or types of their parameters foo(int i) and foo(int i, int j) are different foo(int i) and foo(int k) are the same foo(int i, double d) and foo(double d, int i) are different

12 Overloading class Test { public static void main(String args[]) { myPrint(5); myPrint(5.0); } static void myPrint(int i) { System.out.println("int i = " + i); } static void myPrint(double d) { // same name, different parameters System.out.println("double d = " + d); } } int i = 5 double d = 5.0

13 Why overload a method? So you can use the same names for methods that do essentially the same thing Example: println(int), println(double), println(boolean), println(String), etc. So you can supply defaults for the parameters: int increment(int amount) { count = count + amount; return count; } int increment() { return increment(1); } Notice that one method can call another of the same name So you can supply additional information: void printResults() { System.out.println("total = " + total + ", average = " + average); } void printResult(String message) { System.out.println(message + ": "); printResults(); }

14 Overriding This is called overriding a method Method print in Dog overrides method print in Animal A subclass variable can shadow a superclass variable, but a subclass method can override a superclass method class Animal { public static void main(String args[]) { Animal animal = new Animal(); Dog dog = new Dog(); animal.print(); dog.print(); } void print() { System.out.println("Superclass Animal"); } } public class Dog extends Animal { void print() { System.out.println("Subclass Dog"); } } Superclass Animal Subclass Dog

15 Summary You should overload a method when you want to do essentially the same thing, but with different parameters You should override an inherited method if you want to do something slightly different than in the superclass