The following are the oops concepts that every language will follow.
Objects is the primary key to understand the object-oriented concept. For example dog, table, tv, etc. Every object will have state and behavior, where god will have state like color, breed and behavior like barking. The following is the syntax for the object in pascal.
[c]type object-identifier = object
private
field1 : field-type;
field2 : field-type;
...
public
procedure proc1;
function f1(): function-type;
end;
var objectvar : object-identifier;[/c]
A class is a plan or format or set of guidelines to fabricate a particular kind of an object. Each object is worked from a class. Many classes are utilized to fabricate a whole application. One class can have many object. The following is the syntax.
[c]type class-identifier = class
private
field1 : field-type;
field2 : field-type;
...
public
constructor create();
procedure proc1;
function f1(): function-type;
end;
var classvar : class-identifier;[/c]
Inheritance is the concept to hire the properties from base class, inheritance is one of the excellent feature of OOPS, but Inheritance in Java does not support multiple inheritance concept why because it leads to the confusion. Inheritance follows the IS-A relationship. The following is an example.
[c]TObjectHelper = class helper for TObject
procedure SomeMethod;
end;
TMyObject = class(TObject)
end;
TMyObjectHelper = class helper(TObjectHelper) for TMyObject
procedure SomeOtherMethod;
end; [/c]
In the above example, the
TMyObjectHelper
class is inheriting the properties from the
TObjectHelper
. Here TObjectHelper is the base class and TMyObjectHelper is the child class.
Polymorphism is the OOPS concept that means one can perform a single action by various ways, actually poly means many and morph means forms, these both words are Greek words. There are two types in polymorphism, one is compile time polymorphism and the second one is runtime polymorphism. By using overload concept developer can perform polymorphism.
Encapsulation is a process of protecting the members of the class. Here member functions and all the data will be together to form an object.
If one class has multiple methods by similar name but, various parameters then it is called as method overloading, the advantage of Overloading is that readability of a program.
The constructor alludes to an extraordinary kind of capacity which will be called consequently at whatever point there is a formation of an object from a class. The following is the example.
[c]program constructoranddistructor;
type
Rectangle = object
private
length, width: integer;
public
constructor init(l, w: integer);
destructor done;
procedure setlength(l: integer);
function getlength(): integer;
procedure setwidth(w: integer);
function getwidth(): integer;
procedure draw;
end;
var
r1: Rectangle;
pr1: ^Rectangle;
constructor Rectangle.init(l, w: integer);
begin
length := l;
width := w;
end;
destructor Rectangle.done;
begin
writeln(' Desctructor Called');
end;
procedure Rectangle.setlength(l: integer);
begin
length := l;
end;
procedure Rectangle.setwidth(w: integer);
begin
width :=w;
end;
function Rectangle.getlength(): integer;
begin
getlength := length;
end;
function Rectangle.getwidth(): integer;
begin
getwidth := width;
end;
procedure Rectangle.draw;
var
i, j: integer;
begin
for i:= 1 to length do
begin
for j:= 1 to width do
write(' * ');
writeln;
end;
end;
begin
r1.init(3, 7);
writeln('Draw a rectangle:', r1.getlength(), ' by ' , r1.getwidth());
r1.draw;
new(pr1, init(5, 4));
writeln('Draw a rectangle:', pr1^.getlength(), ' by ',pr1^.getwidth());
pr1^.draw;
pr1^.init(7, 9);
writeln('Draw a rectangle:', pr1^.getlength(), ' by ' ,pr1^.getwidth());
pr1^.draw;
dispose(pr1);
r1.done;
end.[/c]
The above case will give a destructor and a constructor for the class which will instate length, width for the rectangle while the creation of object and it will demolish when leaves scope.
Output:Now compile the code result will be as follows.
[c]Draw a rectangle:3 by 7
* * * * * * *
* * * * * * *
* * * * * * *
Draw a rectangle:5 by 4
* * * *
* * * *
* * * *
* * * *
* * * *
Draw a rectangle:7 by 9
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
Desctructor Called[/c]