OOP: Encapsulation &Abstraction

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Final and Abstract Classes
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Inheritance and Access Control CS 162 (Summer 2009)
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Inheritance.
Class Inheritance Part I
Objects as a programming concept
Can perform actions and provide communication
CS240: Advanced Programming Concepts
Encapsulation.
Can perform actions and provide communication
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Abstract Classes AKEEL AHMED.
Advanced Java Topics Chapter 9
Interfaces.
Advanced Java Programming
Java – Inheritance.
Can perform actions and provide communication
Java Programming, Second Edition
Java Inheritance.
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
C++ Object Oriented 1.
CS 240 – Advanced Programming Concepts
CSG2H3 Object Oriented Programming
Presentation transcript:

OOP: Encapsulation &Abstraction

What is Encapsulation Described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Is the technique of making the fields in a class private and providing access to the fields via public methods. Is also referred to as data hiding.

Benefit of Encapsulation The fields of a class can be made read-only or write-only. A class can have total control over what is stored in its fields. The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.

Implement Encapsulation Keep instance variables private. Make public accessor methods. For the methods, use the JavaBeans naming convention of set<someProperty> and get<someProperty>.

Implement Encapsulation

Abstraction Is to hide actual implementation of an object from the external world that would use the object. Implementation hiding. Ex : stack , queue.

Final keyword Final variable can be assigned only once. Final variable that is not initialized must be initialized in the constructors. Final methods cannot be overridden. In methods private is equal to final. Final classes cannot be extended. Restricting inheritance!

Final keyword cont,

Static keyword Static means one per class. Static variable is called a “class variable”. All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to create a instance Static method can be used without having to create a object first.

Static method

Static method Note : Static Methods Cannot Access Non-Static Variables

The rules for overriding a method The argument list must exactly match that of the overridden method. (If they don't match, you can end up with an overloaded). The return type must be the same as, the return type declared in the original overridden method in the superclass. The access level can't be more restrictive than the overridden method's. The access level CAN be less restrictive than that of the overridden method.

The rules for overriding a method Instance methods can be overridden only if they are inherited by the subclass. A subclass within the same package as the instance's superclass can override any superclass method that is not marked private or final. A subclass in a different package can override only those non-final methods marked public or protected (since protected methods are inherited by the subclass). You cannot override a method marked final. You cannot override a method marked static. If a method can't be inherited, you cannot override it

Thank You