One place for hosting & domains

      Programming

      Object-Oriented Programming Concepts, Explained


      Object-oriented programming gives you a set of programming principles to make your code more compartmentalized and reusable. Object-oriented programming accomplishes this by structuring programs around objects. This tutorial covers the core principles of of object-oriented programming and provides examples of these concepts written in Java.

      What is Object Oriented Programming (OOP)?

      Object-oriented programming — often abbreviated “OOP” — is a set of programming principles centered on objects. Such a set of principles is called a programming paradigm. Objects in OOP can hold attributes and be assigned behaviors, and they allow developers to structure programs around reusable, self-contained components.

      Because of its object-oriented focus, OOP shines when used for applications that need objects to be at their logical center. This is the case with user interfaces, one of the most common places to see OOP used, as well as business applications.

      OOP Concepts in Java You Need to Know

      Object-oriented programming tends to make use of four structures. These form the bedrock of all of the pieces a developer has to work with when building object-oriented programs.

      • Classes: These act as blueprints for objects. They define underlying properties and behaviors which can be inherited by other classes and by objects. Your OOP program’s collection of classes creates a structure off of which the rest of the program gets built.

        Often, in Java, code is constructed with one class per file. The class syntax resembles the following example:

        1
        2
        3
        4
        
        public class ClassName {
            // Code related to the class.
        }
            
      • Objects: These are derived from classes and populate the abstract of their classes’ properties with concrete values. They are the things built from the blueprints provided by classes. Objects also tend to be where the behaviors defined on classes get executed, bringing your application to life.

        Java lets you instantiate an object from a class using the new keyword. Here, a new object gets created from the class created above. This example works when the class has a constructor defined. You can see an example of a constructor definition in the
        Examples of Object Oriented Programming
        section further on.

        1
        2
        
        ClassName objectName = new ClassName();
            
      • Attributes: These are fields (or properties) defined on classes and which represent the state of a particular object. A class might, for instance, define an attributeOne as a String type. An object derived from that class can then use that attribute, assigning it attributeOne = "a string", for example.

        This next example shows what it could look like to add an attribute to the ClassName class created above:

        1
        2
        3
        4
        
        public class ClassName {
            public attributeOne = "a string";
        }
            
      • Methods: These are functions defined on classes, and they provide objects with behaviors. Methods typically act on the values held by an object’s attributes, allowing each object to act in a self-contained way.

        In the following example, you can see what it looks like to add a basic method to a class, using the ClassName example started above:

        1
        2
        3
        4
        5
        6
        7
        8
        
        public class ClassName {
            public attributeOne = "a string";
        
            public void methodOne() {
                System.out.println("The method has been called!");
            }
        }
            

      4 Basic OOP Principles

      In addition to the four basic parts, object-oriented programming has four fundamental concepts. These are what primarily make OOP stand out, and developers rely on these when making the most effective and reusable OOP code.

      These next four sections cover the four principles of OOP, giving you an overview of the roles they play. Then, keep reading to find a section with examples, in Java, each of which demonstrates these principles in action.

      Encapsulation

      This principle ensures that objects are self-contained and limits what information about their state they expose. In other words, other objects cannot directly access the state of an object. Each object manages its own state. To modify an object’s state, other objects need to use that object’s dedicated methods.

      So, for instance, say you have an object called firstObject. That object has two attributes, attributeOne and attributeTwo. Encapsulation prevents another object, say secondObject, from modifying the values of the attributes on firstObject.

      Now, firstObject has control of its own state. It may, for instance, define a method called setAttributeOne that outside objects can access. This way, secondObject can make changes to attributeOne on firstObject. But if firstObject does not define a similar method for attributeTwo, secondObject has no means of modifying it.

      Encapsulation can make OOP applications easier to upgrade and easier for collaboration. An engineer working on one object would thus be less likely to cause breaking changes to an object someone else is working on.

      Encapsulation also makes it easier to keep track of objects’ states. These states can become complicated, and more so the more outside access they allow. By ensuring that each object controls its own state, you make the code easier for yourself and other developers to follow and maintain.

      Data Abstraction

      This principle states that classes include only the details relevant to their context. Doing so creates abstract classes, which more specific classes and objects can extend.

      Take the example of a Pet class. You can make this class to define, in the most general way, the characteristics of pets. So, the class may have name, diet, and health attributes. Now you can extend that class with more specific kinds of Pet. For instance, you may define a Dog class that extends Pet and adds a bark method. At the same time, you can also define a Cat class similarly extending on Pet.

      One of the goals of abstraction is to define common characteristics. Using the example above, Dog has the unique behavior of the bark method, but otherwise it shares things like having a name in common with other pets. Abstraction makes it so that you do not need to redefine these attributes for each specific kind of pet.

      Abstraction also allows you to evaluate various classes by common abstract classes. So long as you know that both Cat and Dog extend Pet, you can evaluate them based on the common attributes held in Pet.

      1
      2
      3
      
      if (obj eitherCatOrDog instanceof Pet) {
          System.out.println("This is my pet, " + eitherCatOrDog.name + ".");
      }

      Inheritance

      This principle declares that objects get some or all of the properties of their parents. Inheritance is the foundation of reusability in OOP. With it, you can create a class and its properties can be reused in multiple objects.

      For example, you can start with a ClassName class from which you create two objects, objectOne and objectTwo. Each of these objects inherits from the parent class, ClassName, and receives all of its attributes and methods. The objects can then each individually work with those attributes and methods. But the important feature is that the ClassName class acts as a common and reusable base.

      In Java, such parent classes are called super classes. Commonly, classes inheriting from super classes are called sub classes. This means that you can make additional classes that inherit from super classes, so that you can have a chain of inheritance.

      Take a look at the Pet example above again. You have a Cat class and a Dog class that inherit from Pet, thus gaining its attributes. From there, you can create specific objects that inherit from the new classes:

      The new object inherits not only properties of the Dog class — like the bark method — but also those on the Pet class, like the name attribute.

      Polymorphism

      This principle states that each sub class can be used in the same way as its parent class or parent classes. At the same time, each sub class may keep its own, distinct form of attributes and methods initially defined in a super class.

      Polymorphism is one of the more complicated features of OOP, but it plays a useful role. To help you understand it, below is an example that reworks the Pet example elaborated in the sections above.

      Say, for instance, when creating the Pet class, you include a method called makeSound:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      
      class Pet {
          public String name = "None";
          public String diet = "Herbivore";
          public boolean healthy = true;
      
          public void makeSound() {
              System.out.println("This is my pet sound.");
          }
      }

      Obviously, the effect of makeSound should be different for Cat and Dog, even though both, being pets, do make sounds:

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      
      class Cat extends Pet {
          public String diet = "Carnivore";
      
          public void makeSound() {
              System.out.println("Meow.");
          }
      }
      
      class Dog extends Pet {
          public String diet = "Omnivore";
      
          public void makeSound() {
              System.out.println("Bark.");
          }
      }

      Following polymorphism, you can, indeed, use any property from the Pet class on any object deriving from the Cat and Dog classes. The effect may be different — you get a different sound from the makeSound method — but all of the parts are still there.

      Examples of Object Oriented Programming

      This section includes snippets of code that give examples of OOP concepts in Java. These are aimed to simultaneously show off some of the components of OOP as well as the four core principles discussed above. The examples also familiarize you with the elements of Java that relate to OOP.

      Starting simple, this first example shows a single Java class, not counting the default Main class used to start up the program. This class covers all of the parts — class, object, attribute, and method — of OOP mentioned above.

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      
      // Create a class.
      class BookShelf {
          // Declare the class attributes.
          public int numberOfBooks;
      
          // Implement a constructor. This is used to create objects from the class,
          // which you can see done in the `Main` class below.
          public BookShelf(int initialNumberOfBooks) {
              numberOfBooks = initialNumberOfBooks;
          }
      
          // Provide a method to add more books to the shelf.
          public void addBooks(int numberToAdd) {
              numberOfBooks += numberToAdd;
          }
      
          // Provide a method to display the count of books on the shelf.
          public void showBookCount() {
              System.out.println("The shelf has " + numberOfBooks + " books.");
          }
      }
      
      class Main {
          public static void main(String args[]) {
              // Use the `BookShelf` class's constructor to create a BookShelf object;
              // it also lets us specify how many books the object starts with.
              BookShelf thisBookShelf = new BookShelf(5);
              thisBookShelf.addBooks(2);
              thisBookShelf.showBookCount();
          }
      }
      The shelf has 7 books.

      Now, this next example is a little more ambitious. It has three classes — again, not counting the Main class. The first, GamingConsole, acts as a super on which other classes can extend. That is exactly what the second class, PlayStation, does — extends on the GamingConsole class. The last class, PlayStation4, does the same, but with the PlayStation class as its direct parent.

      This chain of extensions lets the example demonstrate several of the concepts of OOP at once. Each extension shows the concept of abstraction in action. The PlayStation class is able to make use of both attributes and methods from its parent, demonstrating inheritance. And the PlayStation4 class illustrates polymorphism through its identification with the GamingConsole super class during construction in the Main class.

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      
      // Create a super class, from which the other classes ultimately extend.
      class GamingConsole {
          // Declare attributes.
          public String consoleType;
          public String currentGame;
      
          // Provide a cosntructor to set initial values.
          public GamingConsole(String initialConsoleType, String initialGame) {
              consoleType = initialConsoleType;
              currentGame = initialGame;
          }
      
          // Provide two methods that should be common to all gaming consoles.
          public void insertGame(String gameName) {
              currentGame = gameName;
          }
      
          public void playGame() {
              System.out.println("Starting up the " + consoleType + " console.");
              if (currentGame == "") {
                  System.out.println("No game in the console.");
              } else {
                  System.out.println("Playing " + currentGame + ".");
              }
          }
      }
      
      // Create a sub class for a specific category of gaming console.
      class PlayStation extends GamingConsole {
          // Declare attributes.
          public boolean controllerConnected;
      
          // Provide a constructor.
          public PlayStation(String initialGame, boolean initialControllerConnected) {
              super("PlayStation", initialGame);
              controllerConnected = initialControllerConnected;
          }
      
          // Provide a method unique to this category of gaming consoles. (This
          // feature is not actually unique to PlayStation consoles, but just
          // pretend for the purposes of illustration.)
          public void connectController(boolean isControllerConnected) {
              controllerConnected = isControllerConnected;
          }
      
          // Override the default `playGame` method with a specific implementation
          // for `PlayStation` objects.
          public void playGame() {
              if (controllerConnected == false) {
                  System.out.println("Connect a controller before playing.");
              } else {
                  super.playGame();
              }
          }
      }
      
      // Create another sub class for an even more specific category, this being
      // a type of PlayStation gaming console.
      class PlayStation4 extends PlayStation {
          // Provide a constructor.
          public PlayStation4(String initialGame, boolean initialControllerConnected) {
              super(initialGame, initialControllerConnected);
              consoleType = "PlayStation 4";
      
              initiateWelcome();
          }
      
          // Provide a specific method for PlayStation 4 consoles.
          public void initiateWelcome() {
              System.out.println("Welcome to " + consoleType + ".");
          }
      }
      
      
      
      class Main {
          public static void main(String args[]) {
              // Instantiate a `PlayStation4` object. Notice the polymorphism
              // implied by the fact that we can use `GamingConsole` to identify
              // the new object's type.
              GamingConsole thisConsole = new PlayStation4("", true);
      
              // Use the two methods inherited from the `GamingConsole` super class.
              thisConsole.insertGame("Minecraft");
              thisConsole.playGame();
          }
      }
      Welcome to PlayStation 4.
      Starting up the PlayStation 4 console.
      Playing Minecraft.

      Conclusion

      In this guide you learned the fundamental principles of object-oriented programming. The concepts covered were encapsulation, abstraction, inheritance, and polymorphism. Applying these concepts helps to ensure that you are making the most of what the paradigm can do.

      Throughout this tutorial, the focus has been on OOP related to Java. But keep in mind that these concepts apply anywhere that supports object-oriented programming.
      JavaScript
      ,
      Python
      , and
      Ruby
      are popular examples.

      More Information

      You may wish to consult the following resources for additional information
      on this topic. While these are provided in the hope that they will be
      useful, please note that we cannot vouch for the accuracy or timeliness of
      externally hosted materials.



      Source link

      How To Install Python 3 and Set Up a Local Programming Environment on Ubuntu 20.04


      Introduction

      Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. First published in 1991 with a name inspired by the British comedy group Monty Python, the development team wanted to make Python a language that was fun to use. Quick to set up, and written in a relatively straightforward style with immediate feedback on errors, Python is a great choice for beginners and experienced developers alike. Python 3 is the most current version of the language and is considered to be the future of Python.

      This tutorial will guide you through installing Python 3 on your local Linux machine and setting up a programming environment via the command line. This tutorial will explicitly cover the installation procedures for Ubuntu 20.04, but the general principles apply to any other distribution of Debian Linux.

      Prerequisites

      You will need a computer or virtual machine with Ubuntu 20.04 installed, as well as have administrative access to that machine and an internet connection. You can download this operating system via the Ubuntu 20.04 releases page.

      Step 1 — Setting Up Python 3

      We’ll be completing our installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.

      The command line, also known as a shell or terminal, can help you modify and automate many of the tasks you do on a computer every day, and is an essential tool for software developers. There are many terminal commands to learn that can enable you to do more powerful things. The article “An Introduction to the Linux Terminal” can get you better oriented with the terminal.

      On Ubuntu 20.04, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left hand corner of your screen and typing “terminal” into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.

      Ubuntu Terminal

      Ubuntu 20.04 ships with both Python 3 and Python 2 pre-installed. To make sure that our versions are up-to-date, let’s update and upgrade the system with the apt command to work with Ubuntu’s Advanced Packaging Tool:

      • sudo apt update
      • sudo apt -y upgrade

      The -y flag will confirm that we are agreeing that all items to be installed, but depending on your version of Linux, you may need to confirm additional prompts as your system updates and upgrades.

      Once the process is complete, we can check the version of Python 3 that is installed in the system by typing:

      You will receive output in the terminal window that will let you know the version number. The version number may vary, but it will be similar to this:

      Output

      Python 3.8.10

      To manage software packages for Python, let’s install pip, a tool that will install and manage programming packages we may want to use in our development projects. You can learn more about modules or packages that you can install with pip by reading “How To Import Modules in Python 3.”

      • sudo apt install -y python3-pip

      Python packages can be installed by typing:

      • pip3 install package_name

      Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip3 install numpy.

      There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:

      • sudo apt install build-essential libssl-dev libffi-dev python-dev

      Press y if prompted to do so.

      Once Python is set up, and pip and other tools are installed, we can set up a virtual environment for our development projects.

      Step 2 — Setting Up a Virtual Environment

      Virtual environments enable you to have an isolated space on your computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.

      Setting up a programming environment provides us with greater control over our Python projects and over how different versions of packages are handled. This is especially important when working with third-party packages.

      You can set up as many Python programming environments as you want. Each environment is basically a directory or folder in your computer that has a few scripts in it to make it act as an environment.

      While there are a few ways to achieve a programming environment in Python, we’ll be using the venv module here, which is part of the standard Python 3 library. Let’s install venv by typing:

      • sudo apt install -y python3-venv

      With this installed, we are ready to create environments. Let’s either choose which directory we would like to put our Python programming environments in, or create a new directory with mkdir, as in:

      • mkdir environments
      • cd environments

      Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:

      Essentially, this sets up a new directory that contains a few items which we can view with the ls command:

      Output

      bin include lib lib64 pyvenv.cfg share

      Together, these files work to make sure that your projects are isolated from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs. Python Wheels, a built-package format for Python that can speed up your software production by reducing the number of times you need to compile, will be in the Ubuntu 18.04 share directory.

      To use this environment, you need to activate it, which you can do by typing the following command that calls the activate script:

      • source my_env/bin/activate

      Your prompt will now be prefixed with the name of your environment, in this case it is called my_env. Your prefix may appear somewhat differently, but the name of your environment in parentheses should be the first thing you see on your line:

      This prefix lets us know that the environment my_env is currently active, meaning that when we create programs here they will use only this particular environment’s settings and packages.

      Note: Within the virtual environment, you can use the command python instead of python3, and pip instead of pip3 if you would prefer. If you use Python 3 on your machine outside of an environment, you will need to use the python3 and pip3 commands exclusively.

      After following these steps, your virtual environment is ready to use.

      Step 3 — Creating a “Hello, World” Program

      Now that we have our virtual environment set up, let’s create a traditional “Hello, World!” program. This will let us test our environment and provides us with the opportunity to become more familiar with Python if we aren’t already.

      To do this, we’ll open up a command-line text editor such as nano and create a new file:

      When the text file opens up in the terminal window we’ll type out our program:

      print("Hello, World!")
      

      Exit nano by typing the CTRL and X keys, and when prompted to save the file press y.

      Once you exit out of nano and return to your shell, we’ll run the program:

      The hello.py program that you created should cause your terminal to produce the following output:

      Output

      Hello, World!

      To leave the environment, type the command deactivate and you will return to your original directory.

      Conclusion

      Congratulations! At this point you have a Python 3 programming environment set up on your local Ubuntu machine and can begin a coding project!

      If you are using a different local machine, refer to the tutorial that is relevant to your operating system in our “How To Install and Set Up a Local Programming Environment for Python 3” series. Alternatively, if you’re using an Ubuntu server, you can follow the “How To Install Python and Set Up a Programming Environment on an Ubuntu 20.04 Server” tutorial.

      With your local machine ready for software development, you can continue to learn more about coding in Python by reading our free How To Code in Python 3 eBook, or consulting our Programming Project tutorials.



      Source link

      How To Install Python 3 and Set Up a Programming Environment on Ubuntu 20.04 [Quickstart]


      Introduction

      Python is a flexible and versatile programming language, with strengths in scripting, automation, data analysis, machine learning, and back-end development.

      This tutorial will walk you through installing Python and setting up a programming environment on an Ubuntu 20.04 server. For a more detailed version of this tutorial, with more thorough explanations of each step, please refer to How To Install Python 3 and Set Up a Programming Environment on an Ubuntu 20.04 Server.

      Step 1 — Update and Upgrade

      Logged into your Ubuntu 20.04 server as a sudo non-root user, first update and upgrade your system to ensure that your shipped version of Python 3 is up-to-date.

      • sudo apt update
      • sudo apt -y upgrade

      Confirm installation if prompted to do so.

      Step 2 — Check Version of Python

      Check which version of Python 3 is installed by typing:

      You’ll receive output similar to the following, depending on when you have updated your system.

      Output

      Python 3.8.2

      Step 3 — Install pip

      To manage software packages for Python, install pip, a tool that will help you manage libraries or modules to use in your projects.

      • sudo apt install -y python3-pip

      Python packages can be installed by typing:

      • pip3 install package_name

      Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip3 install numpy.

      There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:

      • sudo apt install build-essential libssl-dev libffi-dev python3-dev

      Step 5 — Install venv

      Virtual environments enable you to have an isolated space on your server for Python projects. We’ll use venv, part of the standard Python 3 library, which we can install by typing:

      • sudo apt install -y python3-venv

      Step 6 — Create a Virtual Environment

      You can create a new environment with the pyvenv command. Here, we’ll call our new environment my_env, but you should call yours something meaningful to your project.

      Step 7 — Activate Virtual Environment

      Activate the environment using the command below, where my_env is the name of your programming environment.

      • source my_env/bin/activate

      Your command prompt will now be prefixed with the name of your environment:

      Step 8 — Test Virtual Environment

      Open the Python interpreter:

      Note that within the Python 3 virtual environment, you can use the command python instead of python3, and pip instead of pip3.

      You’ll know you’re in the interpreter when you receive the following output:

      Python 3.8.2 (default, Mar 13 2020, 10:14:16) 
      [GCC 9.3.0] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>>
      

      Now, use the print() function to create the traditional Hello, World program:

      Output

      Hello, World!

      Step 9 — Deactivate Virtual Environment

      Quit the Python interpreter:

      Then exit the virtual environment:

      Further Reading

      From here, there is a lot you can learn about Python, here are some links related to this guide:



      Source link