Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
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 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Abstract Classes and Interfaces
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
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.
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance in the Java programming language J. W. Rider.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
1 Chapter 5 - Object-Oriented Programming 1 What is inheritance? Object-oriented systems allow classes to be defined in terms of other classes. Classes.
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.
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.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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 Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
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.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Interfaces.
Inheritance and Polymorphism
Object Oriented Analysis and Design
OOP’S Concepts in C#.Net
Interfaces.
METHOD OVERRIDING in JAVA
Java Programming, Second Edition
Inheritance and Polymorphism
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Encapsulation, Inheritance & Polymorphism

OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible from an outside class. ­The attributes and behaviours of that class cannot be changed from outside of the class Inheritance ­The ability to create new classes that are based on existing ones Polymorphism ­The ability to create methods that perform a general function, which automatically adapts itself to work with objects of different classes

Encapsulation Encapsulation means hiding the details of an object from the other parts of a program. The object can be used only through its access methods, which are carefully written to keep the object consistent and secure. Example: The inner working of a TV are encapsulated by the screen and plastic frame. The remote control or user interface allows you to access the TV. In Java, the user interface is controlled by the access modifiers

Access Control Modifiers Private Access Modifier ­A member in the class can only by the methods of that class. Public Access Modifier ­Allows a member in the class to be accessed by code outside of that class

Encapsulation at Work Class CheckingAccount { private String accountNumber; private String accountHolder; private int balance; private int useCount = 0; public CheckingAccount( String accNumber, String holder, int start ) {.... } private void incrementUse() {.... } public int getBalance() {.... } public void processDeposit( int amount ) {.... } public void processCheck( int amount ) {.... } } Only accessed in CheckingAccount Class Accessed Outside of the Class

Inheritance Inheritance enables you to define a new class based upon an existing class. The new class is similar to the existing class, but has additional member variables and methods. This makes programming easier because you can build upon an existing class instead of starting out from scratch.

Parents and Children The class that defines the new class is known as ­The parent class ­The superclass ­The base class A class that is based on the parent class is known as ­The child class ­The subclass ­The derived class. The child classes inherits the characteristics of the parent class In Java there is one parent for multiple childs.

Inheritance between classes not objects Remember classes are like blueprints You can have many objects for the same class What this means ­Parent and Child classes can have several objects each Example: ­Parent class ``Automobile`` has one object Joe`s Car ­Child Class ``Ford`` has two objects Mary`s Ford, Bob`s Ford Note: A class may be the parent for a child class and may be a child of another class. class childClass extends parentClass {...}

Using Inheritance class Video { String title; // name of the item int length; // number of minutes boolean avail; // availability} class Movie extends Video { String director; // name of the director String rating; // G, PG, R, or X public Movie( String ttl, int lngth, String dir, String rtng ) { // constructor super( ttl, lngth ); // uses the super class's constructor director = dir; rating = rtng; // initialize what's new to Movie } } member titleinherited from Video lengthinherited from Video availinherited from Video show()inherited from Video directordefined in Movie ratingdefined in Movie Note: use reserved super() class to ensure inheritance of parent class – always place first in constructor –anywhere in method

Polymorphism & the abstract class Recall that polymorphism is ability to create methods that perform a general function, which can then adapt to work with other object in different classes The abstract class is a special class that is never instantiated. Its purpose is to be a parent to several related classes. The children classes inherit from the abstract parent class. ­abstract class ClassName

Abstract Methods Abstract Methods can be included in the abstract class The purpose of the abstract method is to ensure that all types objects can use the methods of the parent class. Example ­Class cards – has properties of greeting cards ­Various types of Cards – Birthday, Mother’s Day ­The greeting for a birthday card will be different from the greeting of the Mother’s day card ­Using the Abstract method ensures the correct greeting is on the correct object An abstract method has no body. (It has no statements.) ­ It declares an access modifier, return type, and method signature followed by a semicolon.

Example of the Abstract abstract class Card { String recipient; // name of who gets the card public abstract void greeting(); // abstract greeting() method }

Polymorphism without the Abstract Polymorphism means "having many forms.“ For example: a single variable might be used with several objects of related classes at different times in a program. When the variable is used to invoke a method, exactly which method is run depends on the object that the variable currently refers to.

Example public class CardTester { public static void main ( String[] args ) { Card card = new Holiday( "Amy" ); card.greeting(); //Invoke a Holiday greeting() card = new Valentine( "Bob", 3 ); card.greeting(); //Invoke a Valentine greeting() card = new Birthday( "Cindy", 17 ); card.greeting(); //Invoke a Birthday greeting() }