General OO Concepts Objectives For Today: Discuss the benefits of OO Programming Inheritance and Aggregation Abstract Classes Encapsulation Introduce Visual.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

More on Classes Inheritance and Polymorphism
By Waqas Over the many years the people have studied software-development approaches to figure out which approaches are quickest, cheapest, most.
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Abstract Class and Interface
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
More about classes and objects Classes in Visual Basic.NET.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object-oriented Programming Concepts
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
BACS 287 Basics of Object-Oriented Programming 1.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Object Oriented Software Development
Inheritance using Java
Object-Oriented Programming Concepts
Introduction to Object-oriented programming and software development Lecture 1.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
10-Nov-15 Java Object Oriented Programming What is it?
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
© 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.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
Topics Inheritance introduction
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object-Oriented Programming
Sections Inheritance and Abstract Classes
OOP: Encapsulation &Abstraction
JAVA By Waqas.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Week 4 Object-Oriented Programming (1): Inheritance
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Corresponds with Chapter 7
Lecture 22 Inheritance Richard Gesick.
Fundaments of Game Design
Inheritance and Polymorphism
Object Oriented Analysis and Design
Object-Oriented PHP (1)
Object Oriented Design & Analysis
CSG2H3 Object Oriented Programming
Presentation transcript:

General OO Concepts Objectives For Today: Discuss the benefits of OO Programming Inheritance and Aggregation Abstract Classes Encapsulation Introduce Visual Age for Java

General OO Concepts Java is an Object Oriented Language. What makes OO languages so appealing to developers? Object Oriented technology improves software development in the following areas: 1) Code Quality 2) Maintenance 3) Application Scalability 4) Time to Market

General OO Concepts Code quality and maintainability improves with OO programming because code is written inside of Classes. Code inside of a Class is written to undertake the tasks that the Class is responsible for. So, we know exactly what that code is suppose to be doing and how to find it quickly. In Java, once code is written, it can be re-used over and over again. This re-use reduces the amount of code you have to write greatly improving quality and maintainability. Code Quality and Maintenance

General OO Concepts Classes make up our programs, but are completely independent of one another. This means that if you want to add Classes to expand your program, you don’t have to tear apart and change what you already have. You can just add additional Classes to your program. All these characteristics make the application development process much faster. Scalability and Time to Market

OO Concepts We have already seen and created our own Classes. A well designed class can be used over and over again. In fact, it becomes a re-usable piece of code. The Java platform comes with a set of classes that you can use in your programs. You have already seen a few of these classes: String, Integer etc. Not only can we re-use classes, we can also build our own classes from existing classes. Inheritance

OO Concepts Creating classes from existing Classes is called Inheritance. When you create a Class based on another Class, the new class becomes the sub class and the original is referred to as the super class. The sub class inherits all the fields and methods of its parent. This allows the developer to use the existing methods and fields and also allows him/her to build more methods and fields to make a class more specialized. Inheritance

OO Concepts Inheritance For example, if you create a motor vehicle class, car and motorcycle would be appropriate subclasses. Motor Vehicle MotorcycleCar

OO Concepts Classes can have two kinds of relationships between them. The super class sub class relationship is referred to as an is-a relationship. ie. A car is-a motor vehicle (Inheritance) Classes can have fields that are in themselves objects. This is referred to as a has-a relationship. ie. A car has-a motor. (Also known as Aggregation or Containment) Demonstrate motor vehicle diagrams

OO Concepts Creating a new class based on another class is called extending a class. We implement inheritance by extending a class. All the fields and methods are inherited from the super class to the subclass. You often add additional fields and methods to specialize the subclass so it has the implementation you are looking for. We extend a Class by using the keyword extends. Inheritance

OO Concepts The Cosmic super class: All Classes built in Java have one common ancestor. The Object class found in the java.lang package is directly or indirectly related to all Classes in the Java language. In Java the Object class is at the top of the class hierarchy All classes descend from the Class called Object. The java.lang package is imported by default into all classes that you create.

OO Concepts The Cosmic super class: Lets do an exercise that demonstrates Inheritance.

OO Concepts Abstract Classes An abstract class is a class that can never be used to create multiple objects. Do this to design a class for use only as a super class. Abstract classes are designed to define the behavior and fields common to all subclasses in an inheritance hierarchy. We work with abstract classes through extending the class since it cannot be instantiated. The keyword abstract is used to define a Class as being abstract.

OO Concepts Abstract Classes Classes are declared as being abstract as follows: public abstract class MotorVehicle { } You can also declare methods to be abstract, but only within the confines of an abstract Class. Abstract methods have no implementation (code) and are forced to be implemented by sub classes of the parent. Why?

OO Concepts Abstract Classes We may have a method that we want subclasses to inherit and implement, but we are not sure how to implement it in the super class. To solve this problem define an abstract method in an abstract class that provides no implementation and make it abstract. Methods can be declared in abstract classes as follows: public abstract class MotorVehicle { public abstract void steer(); } Let’s re-visit our Motor Vehicles and explore Abstract Classes.

Encapsulation Encapsulation is a process of hiding details of an object. A kind of protective capsule that stops direct access from other objects. Forces access to an objects state through its public methods. OO Concepts

Encapsulation It is common to represent an object as a donut. The center of the donut contains the instance variables (fields). The instance variables are protected and only accessible through the methods that the object provides. The methods are represented on the outer rim of the donut surrounding the variables on the inside

OO Concepts Encapsulation For your objects to be truly encapsulated, you should declare your fields as private when creating your classes. Private fields can only be accessed by other objects via methods of the Class where the fields are declared. This ensures that other Objects cannot manipulate these fields directly. Other Objects should only be able to access these fields through public methods defined in the class.

OO Concepts Encapsulation Example public class Person { private String name; //Accessor method used to get the name of the person public String getName() { return name; } //Mutator method used to set the name of the person. public void setName(String aName) { name = aName; } Lets do an example demonstrating encapsulation

OO Concepts Person Object name age getAge() getName() setAge() walk() Car Object carMake horsePower getHorsePower() start() brake() Send Message Objects communicate to one another by calling each others methods. In the example below, the Person object is sending a message to the Car object by calling the start method