Download presentation
Presentation is loading. Please wait.
Published byAnnice Harrison Modified over 9 years ago
1
Encapsulation, Inheritance & Polymorphism
2
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
3
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
4
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
5
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
6
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.
7
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.
8
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 {...}
9
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
10
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
11
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.
12
Example of the Abstract abstract class Card { String recipient; // name of who gets the card public abstract void greeting(); // abstract greeting() method }
13
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.
14
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() }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.