Java Standard Edition: Principles
Java Principles
1 Java OOP
Object-oriented programming (OOP) is a programming paradigm that is based on the concepts of objects
and classes
, which can contain data and code that manipulates that data. OOP is based on several key principles, including the following:
Encapsulation: Encapsulation refers to the idea of combining data and the code that manipulates that data into a single unit, or object. This allows for better organization and modularity of code, and helps to hide the internal details of an object from other parts of the program.
Abstraction: Abstraction is the process of representing essential features without including the details. In OOP, abstraction is often achieved through the use of interfaces or abstract classes, which define a set of common methods that can be implemented by different classes in different ways. This allows for greater flexibility and reusability of code.
Inheritance: Inheritance is the ability of a class to inherit the attributes and behaviors of another class. In OOP, a class can inherit from a parent class, also known as a superclass, and can override or extend the methods of the parent class to add new functionality. This allows for code reuse and makes it easier to create hierarchies of classes.
Polymorphism: Polymorphism is the ability of an object to take on multiple forms. In OOP, polymorphism allows for the use of a single interface to refer to objects of different types. This allows for greater flexibility and code reuse, as the same method can be used on objects of different types and the appropriate implementation will be called based on the type of the object.
Overall, the principles of encapsulation, abstraction, inheritance, and polymorphism are fundamental to OOP and form the basis for many of its benefits, such as modularity, flexibility, code reuse, and ease of maintenance.
1.1 What is an object in Java OOP
Objects can interact with each other by sending and receiving messages, or method calls, to access and modify their data.
Objects are created from classes, which are templates that define the structure and behavior of objects.
📘 Object-oriented programming (OOP)
In object-oriented programming (OOP), an object is a self-contained unit that combines data and the code that manipulates that data.
An object is a data structure that contains:
- attributes, or properties, that describe the object, defines the state’s object
- and methods, or functions, that define the behavior of the object.
In Java, objects are created using the new
keyword, and their attributes and methods can be accessed using the dot (.)
operator. Objects are an important concept in OOP and are central to many of its benefits, such as modularity, code reuse, and flexibility.
Person carla = new Person("Carla", 21, "red hair", 45.000);
1.1.1 Attributes, Contructor and Methods
In object-oriented programming (OOP):
- The constructor is a special type of method that is used to create and initialize an object. In Java, a constructor is a method that has the same name as the class and is used to create an instance of the class. A constructor can take arguments, which are used to initialize the object’s attributes, or properties.
- The state of an object refers to the values of its attributes at a given moment in time.
- The behavior of an object refers to the actions or operations that it can perform. In Java, the behavior of an object is defined by its methods, which are functions that are associated with the object and can be called to perform actions on the object.
Overall, a constructor, state, and behavior are important concepts in OOP and are closely related.
A constructor is used to create and initialize an object, and its state and behavior are defined by its attributes and methods, respectively. In Java SE, these concepts are central to the creation and use of objects, and are fundamental to the flexibility and power of the Java language.