top of page
hv

OOP In Java

Object-Oriented Programming concepts in Java are the same as those in other programming languages. These include:

  • Abstraction: Abstraction means using simple ways to show complexity. In Java, abstraction means simple things like objects, classes, and variables that represent more complex code and data. This is essential as it prevents the repetition of the same function many times.

  • Encapsulation: This keeps fields within a class private, then provides access to them via public methods. It poses a barrier that keeps the data and code safe within the class itself. This way, objects like code components or variables can be reused without giving users open access to the data system-wide.

  • Inheritance: This is a special feature of Object-Oriented Programming in Java. It allows programmers to create new classes that share some attributes of preexisting classes.

  • Polymorphism: This Java OOP concept lets programmers use the same word to mean different things. One form of polymorphism in Java is method overloading. That’s when different meanings are implied by the code itself. The other form is method overriding. That’s when the different meanings are implied by the values of the supplied variables.


Principles of OOP in Java


The principles of OOP in Java programming are those that ease programming for coders. They reduce complexity and are easy to use, all while not sacrificing security. They include:

  • DRY (“Don’t Repeat Yourself”): This is the most essential principle in Java. In a program, there must never be two or more identical blocks of code in different places. Instead, there should be one method that can be reused for different applications. If the code is expected to change in the future, it can be encapsulated by making all the variables and methods private.

  • Single Responsibility: This is another important principle in Java which states that a class should always have only one functionality. That way, it can be called or extended on its own when new uses are required, without causing coupling between different functionalities.

  • Open-Closed Design: This is to make all methods and classes Closed for modification but Open for an extension. That way, tried and tested code can remain static but can be modified to perform new tasks as and when required.


Classes in Java


A class is a layout for objects in OOP and classes in Java follow a specific format:

  • The name of a class must match the file name and it is referenced each time a new instance of the class is created.

  • All class variables are initialised below the class name declaration and can be ‘public’ or ‘private’ depending on what the scope of that variable should be.

  • The constructor method is generally the first method inside the class and is required to initialise each object of the class. The name of the constructor, like the name of the class, must also match the name of the file in which it is created. Constructors are also used to set default values for class variables.

  • Each class method can then be programmed to perform activities that the user would like to perform but there are 2 main types of methods classified by what their purpose is:

    • Accessor methods are used to obtain data from the class and are commonly known as ‘getters’. They are generally used to give the user values of variables that are initialised as ‘private’.

    • Mutator methods are used to modify the value of variables within the class and are known as ‘setters’.


Why do Programmers use OOP in Java?

  • It is faster and easier to use and execute

  • It provides a clearer structure

  • It keeps the code DRY "Don't Repeat Yourself"

  • It makes the code easier to maintain, modify and debug

  • It makes it possible to create reusable applications with less code and shorter development time

Article was written for CyberClubNPSi

0 views

Recent Posts

See All

コメント


bottom of page