Exploring Object-Oriented Programming: Key Concepts and Benefits
E N D
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 Maintenance • 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.
General OO Concepts Scalability and Time to Market • 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.
OO Concepts Inheritance • 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.
OO Concepts Inheritance • 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.
OO Concepts • Inheritance • For example, if you create a motor vehicle class, • car and motorcycle would be appropriate subclasses. Motor Vehicle Motorcycle Car
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 Inheritance • 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.
OO Concepts The Cosmic super class: • All Classes built in Java have one common ancestor. • The Objectclass 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.
OO Concepts EncapsulationEncapsulation 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 • 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 Person Object Send Message Car Object getName() getAge() start() getHorsePower() name carMake age horsePower setAge() walk() brake()