The Ctdp Object Oriented Basics Guide

  • Uploaded by: RAHA TUDU
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View The Ctdp Object Oriented Basics Guide as PDF for free.

More details

  • Words: 3,048
  • Pages: 11
The CTDP Object Oriented Basics Guide

Object Oriented Basics What is an Object? If you are familiar with programming, you know that programming on a very basic level requires: •

Code - The part of the program that allows functionality. Sets of instructions that tell the program how to operate or what to do.



Data - Where program and external information is stored. There are several data types such as integers, strings, float values and more. Data is called attributes in object oriented programming.

When writing code, the code is put into what is called procedures or functions. The classic difference between a procedure and a function is that a function returns a value when a procedure does not, however the terms function and procedure are sometimes used interchangeably. In the real world, an object includes such things as: •

Table



Chair



Car

You get the idea. In the computer world, objects are simulated using code and data. The data stores the current and permanent states of the object, and the code makes the object perform its functions (also known as behavior). For example in the case of a car permanent object data would include: •

Make - The manufacturer of the car.



Model



Color



Engine horsepower



Transmission information



Passenger capacity

There are many more. The current object state describes characteristics of the object that would change with time such as: •

Position or location



Speed



Fuel reserve



Mileage

To make an object perform functions the following types of functions may be required: •

Start



Turn off



Accelerate



Decelerate



Turn number of degrees left



Turn Number of degrees right



Check fuel reserve



Fill fuel tank

As you can see an object requires the above items and more to make it what it is. Therefore objects in the programming world consist of: •

Functions



Data (or attributes)

Classes are used to implement objects. Classes are not objects themselves, but are used to create objects. An excellent comparison of classes and objects is to compare a class to a cookie cutter and the object is a cookie. Classes are templates for objects.

Object Classes Hierarchy Classes are used as templates for objects to define them. Classes are used in a hierarchical nature. This means that a class may have subclasses. For examples a car might have the following subclasses: •

Body



Engine



Tire (4 instances plus a spare)



Transmission



Seat(s)



Door(s)



Trunk

Each of these objects may also have a subclass. For example the engine would include carburetor, pistons, cylinders, spark plugs and more. The object containing the subclasses is referred to as the parent class, also called the superclass. The object to subobject hierarchy is arranged from the less specific parent to the more specific subclass, also sometimes called the child class. As you move down into the subclasses, information is more specific. This means that the superclass or parent class is the generalization of the subclass and the subclass is the specialization of the parent class.

Class Relationships When classes use or contain subclasses, the following relationships outline the differences between the respective classes:



A-kind-of - The A-kind-of relationship is a classic relationship such as a sports car is a kind of car, or a Ford mustang is a kind of a car. This refers from the specific (type of car) to general (car) description. This is a general description of the subclass to parent class relationship.



Part-of - This relationship is of the type described above where a car is composed of specific parts. This refers from the subclass to the parent or superclass. An engine is part of a car.



Has-a - This is the inverse of the part-of relationship and refers from the parent or superclass to the subclass. A car has an engine. This concept is called aggregation or composition and is designated by a filled diamond in the Unified Modeling Language (UML).

Class Declaration and Sections When a class is declared, there are three possible sections inside the class: •

Public - Data and methods in this section are accessible outside the class.



Protected - Data and methods in this section are accessible to this class and subclasses derived from this class or one of its subclasses.



Private - Data and methods in this section are not accessible outside the class. Only functions in this class have access to private functions and data.

The friend construct allows private data and methods to be accessible to classes that are declared "friends" with the class the private data and methods are in. An example of class declarations with friend declarations in C++ is: class Aclass { friend class Bclass; //Make Bclass a friend class friend int Zclass::ready(); //Make the ready() method in Zclass a friend private: int x, y; void funct1(int n) { x=n; } protected: int a, b; public: int x1, y1; functa(int m) { if (m ) funct1(m); //although x cannot be changed externally, this function called from external sources can change it. } }

Inheritance Inheritance is applied in the a-kind-of class relationship. As in all parent child relationships, children (subclasses) inherit the characteristics of the parent class, also called the superclass.

What this means is when a subclass is created from a parent class, it will inherit the functions and data that is defined in the parent class. What this means is that the same attributes and methods are available to the subclass. For instance the car class is: class Car { attributes: float positionx float positiony float speed float direction float maxaccel = 50 methods: accelerate(float) turn(float) }

An example of a subclass being declared follows: class parent_classA { int var1, b; setVariable(int n) { var1=n; } }; class sub_ClassA : parent_classA { setVariable(int n) { var1=b*n; } };

The class sub_classA is the subclass of the superclass or parent class parent_classA. In this case sub_classA inherited the integer variables var1 and b along with the method setVariable(int) from the parent class parent_classA. It overrode the setVariable(int) method with a setVariable(int) method of its own. A derived class cannot access the private data of its parent unless specifically authorized by the parent. When the child class is created using a parent class, additional values and functions may be added to the subclass to make it more specific. For instance if the parent class is a car, the child class may be sports car. The sports car could have additional data describing it in more detail. In this case, although the parent car class has an engine, the sports car may have a more powerful engine definition. Therefore in the sports car class, the engine is defined again with different information. In the example above maxaccel=120 may be defined rather than the value of 50 for a standard car. This way the original car engine characteristics are overridden in favor of the more powerful engine.

Multiple Inheritance A subclass may inherit from two or more parent classes. This does not refer to two parent classes where one is a parent to the other. This refers to two parent classes that are independent of each other. This capability does not apply to Java. Java uses interfaces to add some functionality in this area.

Abstract Classes Abstract classes are used to define a basic class which may contain specific defined attributes and methods along with specific abstract methods which are not fully defined. These methods not fully defined must be fully defined in the child class. For example a goemetric figure may have a drawing method along with X and Y position information. The draw method would be defined in the abstract class for the geometric figure (the geometric class). The draw method would be fully defined in all subclasses which are the type of geometric figure such as circle or square. Therefore the subclass circle or square would define their own draw method which would override the draw method defined in the geometric class.

Generic Types Generic types are the complement to abstract classes although they are not classes. They are merely a template used to create classes. They define the methods to be used in the class, but leave the data or attributes to be defined in the specific class. List types are used for this purpose. The methods such as appending or removing items from the list are provided, but the data is not since it will depend on the type of list.

Messages Messages are sent when an object calls another object method. There are three parts to the message: •

Name of the object the message is sent to.



Name of the method being called.



Any required parameters which are variables or values being passed to the method.

An example of calling a method would be to first create an instance of the Car class (as in the above example) with the following line: car Car; Then call a method of the object car: car accelerate(20); This sends a message to the car object and calls the method.

Virtual Functions and Polymorphism Methods are uniquely identified by: •

Name of method



Parameter list

Polymorphism (many form) refers to an objects ability to become many different object forms. Polymorphism allows multiple methods (functions) to have the same name. The functions are uniquely identified by the parameter list used. For instance if there are two methods with the name "method1" and one of them requires two integer variables and the other requires one integer variable in the parameter list, if "method1(int 10)" is called, the function with the single integer on the parameter list is called. The difference in parameter list may be that parameters passed to the function are of a different type rather than quantity. One method may require a string variable while another requires a float variable.

An example should help. In this example, the class "Shape" is defined in another file and has a getArea() method that is overridden by the Triangle and Rectangle classes. The getArea() methods in both Triangle and Rectangle share the same interface (The same number and type of parameters are passed which is none) although the methods are different. When the printArea(Shape) method is called from main, the correct method for the correct object is called although the method is actually different from one object to another. This is one way polymorphism is applied. import Shape; public Class Triangle extends Shape { int height, width; public int getArea() { return (int) ((height * width)/2); } } public Class Rectangle extends Shape { int height, width; public int getArea() { return height * width; } } public static void main(String[] args) { rect = new Rectangle; tri = new Triangle;

}

printArea(rect); printArea(tri);

void printArea(Shape figure) { int x, y; System.out.println(figure.getArea()); }

Constructors and Destructors Constructors have the same name as the class they are defined in. They provide for automatic initialization of objects and parameters when the object is declared. Destructors are indicated with the tilde, ~, in front of them. They provide for automatic deallocation of resources used by the object. Constructor example: class Aclass { friend class Bclass;

//Make Bclass a friend class

friend int Zclass::ready(); Zclass a friend private: int x, y; public: int a, b;

//Make the ready() method in

Aclass() { x=0; y=0; a=0; b=0; } void funct1(int n) { x=n; } functa(int m) { if (m ) funct1(m); //although x cannot be changed externally, this function called from external sources can change it. } }

Object Oriented Scope In object oriented programming, methods and variables have various scope. Scope means that the method or variable may or may not be directly accessable to other objects or classes. Classes that do not have instances may be accessable to the system.

Class Scope Class variables and class methods are associated with a class. An instance of the class (object) is not required to use these variables or metnods. Class methodc cannot access instance variables or metnods, only class variables and methods.

Instance Scope Instance variables and instance methods are associated with a specific object. They can access class variables and methods.

Private Scope Private variables and private methods are only accessible to the object they are contained in.

Protected Scope Protected variables and protected methods are accessible by the class they are in and inheriting classes (sub classes).

Public Scope Public variables and public methods are accessible outside the object they are contained in. They are accessible to any other object.

Encapsulation The process of providing a public interface to interact with the object while hiding other information inside the object.

Java Variables and Scope There are three types of variable scope in Java. •

Class variables - Applies to all instances of a class of objects.



Instance variables - Define an object’s attributes.



Local variables - Are defined and used inside methods or other statement blocks such as a catch block. After the block or method is exited, the variables no longer exist. They must be declared and given values before use.

Object Data Scope •

Local



Global

Java Basic Data Types Also called primitive types. •

Integers - All types are signed. ○ byte - 8 bits ○ short - 16 bits ○ Int - 32 bits ○ long - 64 bits



Floating point numbers ○ float ○ double - For more precise real number calculations.



Characters - char



Boolean - boolean - True or false.

Unified Modeling Language Unified modeling language (UML) references and diagrams are common when dealing with JAVA programming. UML is used to describe the relationship between objects. For instance, in the section about classes class relationships were described such that three possible object relationships are: •

A-kind-of - The A-kind-of relationship is a classic relationship such as a sports car is a kind of car, or a Ford mustang is a kind of a car. This refers from the specific (type of car) to general (car) description. This is a general description of the subclass to parent

class relationship. This relationship is shown with an arrow pointing from the sub class to the parent class. •

Part-of - This relationship is of the type described above where a car is composed of specific parts. This refers from the subclass to the parent or superclass. An engine is part of a car. This relation in the reverse of the "Has-a" relationship, below. It is indicated using a solid diamond as described below.



Has-a - This is the inverse of the part-of relationship and refers from the parent or superclass to the subclass. A car has an engine. This concept is called aggregation or composition and is designated by a filled diamond at the containing class in the Unified Modeling Language (UML).

Object Modeling Objects are shown as boxes with up to three sections. The sections are: •

Type of object (class name)



Data members important enough to list.



Member functions. This is also called the interface since it is a list of functions used to "interface" the object to the outside world.

Object Relationships In the drawing below, the class "airplane" "has-an" engine and wings which is an aggregation or composition relationship. The "jetfighter" and the "ultralight" class are "a-kind-of" the class "airplane". The aggregation relationship is shown with a solid diamond, and the "a kind of"

relation is shown with an arrow. Of course, the jetfighter and the ultralight class would use the keyword "extends" with the phrase "jetfighter extends airplane" when the class is declared to be a subclass of the airplane class. The engine and the wings class would be a part of the airplane class and be implemented inside the airplane class.

Object Oriented Terms •

Attributes - Data describing an object. the attributes or characteristics of the object.



Behavier - Defined by the methods the object contains.



Classes - The implementation of an abstract data type defining attributes and methods. With C++ this is used with pointers but with JAVA late binding is done automatically when required.



Constructor



Destructor



Early binding - The method used to implement an action is implemented at compile time. Used with pointers and may not apply to JAVA.



Encapsulation - The process of providing a public interface to interact with the object while hiding other information inside the object.



Friend - allows private data and methods to be accessible to classes that are declared "friends" with the class the private data and methods are in.



Function - A subroutine written to perform a specific task and return a value.



Implementation - The implementation of objects is hidden in the object class. This means that the data and functions that define the object are defined by the object's class.



Instance - An instance of a class. An object.



Instantiation - Creating an instance of a class which is an object.



Late binding - The method used to implement an action is implemented at run time. The virtual keyword is used to designate functions defined at run time. With C++ this is used with pointers but with JAVA late binding is done automatically when required.



Message - A request to an object to invoke one of its methods. Messages contain the name of the method and the arguments required by the method.h



Object - An instance of a class.



Operator Overloading



Polymorphism



Procedure - A subroutine written to perform a specific task but not return a value.



Private - Data and methods in this section are not accessible outside the class. Only functions in this class have access to private functions and data.



Protected - Data and methods in this section are accessible to this class and subclasses derived from this class or one of its subclasses.



Public - Data and methods in this section are accessible outside the class.



Virtual functions - Use of the keyword virtual in front of the function designates that it is implemented at run time. Used with pointers and may not apply to JAVA.

Related Documents


More Documents from ""

Note On Java
June 2020 2
Iview Quest
June 2020 2
New Field Book.pdf
November 2019 0
01_title.pdf
November 2019 2