OOPs: Object Oriented Programming
OOP is  Organizing a program around with its Data and set the well defined interface to the Data

What are the OOPs Concepts?
OOPs use the three basic components. Class , Objects and Methods. Additionally Inheritance, polymorphism, Abstraction, Event Handling and Encapsulation are the significant concepts of OOPs.

Types of Classes (AS3)

ActionScript 3.0 supports the following class attributes:

  • dynamic (allows properties to be added to instance at run time)
  • final (cannot be extended by another class)
  • internal (visible inside the current package)
  • public (visible everywhere)
Class Property Attributes

  • internal (visible inside the same package)
  • private (visible in the same class)
  • protected (visible in the same class and derived classes)
  • public (visible everywhere
  • static (specifies that a property belongs to the class, not to instances of the class)
Encapsulation

It is an ability to hide and protect the data. For example, getters and setters in a class. We don’t need to know about the properties we get from the getters, but we should know what should be sent to the setters. So these getters and setters are used to make your class properties private and also accessible by the other classes.

Inheritance

Inheritance is a form of a code reuse which allows programmers to develop new classes that are based on the existing classes. Advantage of inheritance is the way it allows you to reuse the code from the base class and leave the existing code unmodified.

Interfaces

Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. Implementing an interface allows a class to become more formal about the behavior it promises to provide. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. Lets see a simple code later in another post.

By using interfaces, you can maintain the consistency through-out the application. It will also make you (or anyone else editing/implementing your interfaces) to not leave any methods that is necessary to a class. It is more useful in cases where you can use more classes.

See at the end of this thread to download a sample interface classes in AS3.

Function Overloading

Function overloading is one attribute of Polymorphism in OOP. Writing two methods with the same name and different parameters is called Function Overloading. Function overloading is available in AS2 but AS3 do not support. See the code example.

function MyFun() {
   trace("Function");
}
function MyFun(msg:String) {
   trace(msg);
}

This sample will work with AS2 compiler but not with AS3 compiler. To overload a function in AS3, we can use the “*” as datatype for the parameter which will get any parameter type with the same function name and we can add our codes to behave according to the type of arguments.

function doSomething(obj:*) : int
{
  if (obj is int)
  // do int stuff
  else if (obj is String)
  // do string stuff
  else
  // what type did you give me?
}

Singleton Class:

Singleton class restrict the limit of instantiation of the objects. In other words, its a class which have only one object. For example, configuration data is stored in one singletone class, and objects for this class have initialized in many classes. In this case, data in configuration is important for all the classes, so everything is managed by a single object or central object. Any change in configuration can be easily notified for all linked classes.

interfaces_oop.zip
File Size: 8 kb
File Type: zip
Download File




Leave a Reply.