Object Oriented Programming OOP in Java - ProgrammerTech
Language:

Search

Object Oriented Programming OOP in Java

  • Share this:
Object Oriented Programming OOP in Java

Object Oriented Programming

OOP is an abbreviation of the term (object oriented programming). In the upcoming lessons, we will address oop, and if you follow the lessons that we will present in the future, you will be able to deal with oop in any language that uses the concepts of oop in terms of structure The format is like Java and C#.

We will talk about all the surrounding topics in object-oriented programming (oop) and try to understand everything small and large so that the learner comes out with very good information so that he can deal with object-oriented programming (oop) in any language that adopts the same structure and form of Java before starting to define oop or Defining classes and objects Let's take a quick look at what a program looks like in oop.

We talked in the first lessons that the Java language adopts the oop form in terms of structure and shape. The object consists of methods. This object contains the methods. We can write more than one function and work for it include and under the object remains his job that he is responsible for these functions and instead of calling all A function by itself we invoke the object in its functions all.

Syntax object-oriented programming in Java

package oop;
class a
{
}
public class Oop{
public static void main(String[] args){
}}

The meaning of the word package is that all the lines below this word belong to the package, that is, they belong to the word after the word package. We have two classes in the previous form, the a-class and the Oop class, and these two classes follow the oop packet. And we benefit from the package by packaging the program we write, which means the ability to reuse all the code we used in another project. For example, we wrote a program that used 6 classes, and we needed to use these classes instead of writing them again. We can reuse it by including the package name in another program and then calling the existing classes, thus we will shorten the effort, time and code.

The meaning of this word class is considered to be the top of the pyramid in the world of oop oriented programming, and it is the most important thing about it. Previously, before the advent of oop oriented programming, codes were written in an unorganized and random manner. And they needed to organize the data, divide the data, and arrange the data so that it is easier for the programmer to work with the data. When they are divided in a neat and orderly manner, so they create what are called oop-oriented programming and classes.

The second benefit of the class is that the class protects the variables and assigns degrees. Access to make our data more secure and access degrees we will talk about in future lessons. The object in object-oriented programming is a copy of the class and we benefit from it that we can't. Accessing data within the class directly except by using an object that is derived from the same class.

Example 1

package oop;
class a
{
int n=9;
}
public class Oop{
public static void main(String[] args){
 a ob=new a();
System.out.println(ob.n);
}}

At first we called the package oop, then we created the class a and it contains a variable and it was of type int and its name is n and its value is 9. We entered the main program to ask it to print the value of the variable n and we created an object of the same class so that we can access the class a. And we wrote the name of the class a, then we wrote the name of the object, then we wrote new a, meaning this is a copy of the class. We include it inside the ob object, then we print the class, and when the program runs, it will print 9, which is the value of the variable a.

 

Class concept in java

The class in Java is a large container or box in which we can put all the code such as (variables - functions - objects) and anything else in the code.

Syntax a class in Java

class Name{
Class Contents
    }

Example 2

class Alsadig {
    String child1;
    String child2;
    String child3;
    String child4;
    String child5;
    void printInfoAlsadig() {
        System.out.println("Child1: " +child1);
        System.out.println("Child2: " +child2);
        System.out.println("Child3: " +child3);
        System.out.println("Child4: " +child4);
        System.out.println("Child4: " +child5);
    }
}

Below we created a class called Alsadig and it contains five variables which are children of Alsadig, and we added a function that displays the values ​​of these variables, which is printInfoAlsadig when it is called.

 

The concept of an object in Java

The object in java, and this object is an exact copy of a specific class, this means that we cannot create an object unless there is a class in the program. To define an object from a class, we must put the name of the class and after that we put a name for this object.

Syntax object in Java

Alsadig saja = new Alsadig();

Example 3

Alsadig saja;
saja = new Alsadig();

The first line means that the saja object will be an Alsadig class object. The second line means we took an object from the Alsadig class and stored it in the saja, and the saja became an object derived from the Alsadig class.

 

Constructor in Java

Constructor is a Java language that makes it easy for us to create objects from a specific class and is an important thing to think about after creating the class. It is a function that has a special type, this function is called during the creation of the object in order to generate the initial values ​​of the properties in it.

class Saja{
}

Compiler or the Java interpreter will generate an empty constructor by default if it finds that the class you created does not contain a constructor as follows.

class Saja {
    public saja(){
    }
}

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com