Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "General OO Concepts Objectives For Today: Discuss the benefits of OO Programming Inheritance and Aggregation Abstract Classes Encapsulation Introduce Visual."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

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

8 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

9 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

10 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.

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

12 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.

13 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?

14 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.

15 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

16 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

17 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.

18 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

19 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

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

Similar presentations

Ads by Google