OOPS (Object Oriented Programming)
Classes - A class is
software construct that defines the data, grouping up of similar objects.
Method - Methods
are the functions that operate on the data and define objects behavior.
Variables – Variables
are used to store the data for the class, they are specific to an object.
Class
declaration: Specifies the name of the class and necessary
variables, It can also specify inheritance.
Static: Static
methods are methods that are called on the class itself, means an instance of
class is not required to call these methods.
Void:
This
datatype show that the return value is null.
Main:
This is the method from where the execution
of code get start.
A class in order to execute on its own, should have a
main method.
New() – This
method initiates a new object, you can also assign values to the object variable using this
method.
Construct() - The method
should return an instance of the class.
Class ClassName
{
public void main(Args _args)
{
}
}
Public – Access Modifier
Void – Return Type
Main – Method name
Args – Parameter
Global and Local Variables:
A scope defines the area in which an item can be accessed. Variables defined in a class are available to the methods within that class. Variables in methods can be accessed only within the current Method
Class ClassName
{
Int a; ----- Global variable (Can be used in any of the method defined in this class
public void main(Args _args)
{
Int b; ------ Local variable (Can be used only in this method)
}
}
Creating object of class:
ClassName objectName = new ClassName();
Calling method through object
ClassName objectName = new ClassName();
objectName.MethodName();
Calling Static method:
ClassName::MethodName()
RunnableClass1::Main();
Constructor – In X++ constructor is a static method which is used to call instance of the class.
Class ClassName
{
public static void Construct()
{
return new ClassName();
}
}
Call a constructor
ClassName className = ClassName::Construct();
Parameterized Construct
public static void Construct(Args _args)
{
ConstructClass contructClass = ConstructClass::construct("icici");
}
Inheritance
Inheritance is an important concept in X++. Inheritance is a concept in which you define parent class and child class relationship.
The child classes inherit methods of the parent class, but at the same time, they can also modify the behaviour of the methods if required. The child class can also define methods of its own if required.
In X++ inheritance is done using keyword “extends”. “Super” keyword is used to call parent class method in child class. To Stop a class from being inherited use keyword ”Final”.
X++ supports single inheritance, but it does not support multiple inheritance. X++ supports overriding, but it does not support overloading.
Class ParentClass
{
public void helloWorld()
{
info(“Hello World”);
}
}
Class ChildClass extends ParentClass
{
public void helloWorld()
{
super();
}
}
Abstract Class & Method
Abstract classes are marked by the keyword abstract in the class definition, they are typically used to define a base class in the hierarchy. We can't create an instance of them
Mostly, an abstract class is used to define what is known as a base class. A base class is a class which has the most basic definition of a particular requirement.
An abstract class can have both abstract methods and Non Abstract methods.
Abstract Method
When declare a method as abstract this method should be declare/initiate in child class. Abstract methods only be declared in abstract classes. No code or declarations are allowed in abstract methods.
abstract class AbstractClass
{
abstract void myAbstractMethod()
{
}
}
Class ChildClass extends AbstractClass
{
void myAbstractMethod()
{
}
}
Interface
An interface is a method that contains only method signature, it doesn’t contains any code in it. Interface can be accessed by using keyword Implements, we can implement multiple interface at a time using comma separator.
An interface can extend another interface by using extends keyword. An interface cannot extend more than one interface.
The purpose of interfaces is to define and enforce similarities between unrelated classes without having to artificially derive one class from the other.
All methods in Interface are public in nature.
Interface InterfaceClass
{
void myInterfaceMethod()
{
}
}
Class ChildClass implements InterfaceClass
{
void myInterfaceMethod()
{
// Write your code
}
}
No comments:
Post a Comment