Hello, future software developers!
In the early days of computers, programmers were developing applications using machine language, like Assembly, to achieve their goals with computers. Before then, programmers were using binary. Can you imaging writing a program using only 1's and 0's? If the idea of doing that makes your head spin, you're in luck! Java is one of the top programming languages in the world, and it uses object-oriented programing (OOP) concepts to allow users to be more efficient when writing their code. OOP focuses on the use of objects and classes. So what are these things? Let me explain:
Classes are procedures that you design to make something work. They can be used over and over again to increase your productivity. Objects are the items that you see in the class. Variables, arguments, and functions are all objects. Here is an example using pseudo-code:
class SayHello(arguments): //SayHello is the class
function Hello(string myName): //Hello and myName are Objects
print "Hello " + myName;
In this example, you can call to the class and its function to perform a task. Something like this:
SayHello.Hello("Jaime")
returns
"Hello Jaime"
It's important to highlight that there are four principles to OOP:
Inheritance: The ability of an object to inherit data and behaviors from another.
Encapsulation: Any data in an object that exposes only the selected data, preventing unwanted modifications.
Abstraction: Fetching, add, or removing data from an object. This is done a lot in classes.
Polymorphism: Many methods can do the same task but do not mix types. More than one function can be created within an class to do more than just one thing. The object can then be used multiple times for different behaviors.
Let's take what we made in the pseudo code and run it in a Java application:
class SayHello {
//inherits whatever is passed into "myName"
public static void Hello(String myName) {
String hi = "Hello" //The object "hi" is encapsulated, unchangable.
System.out.println(hi + " " + myName);
}
}
class NameGame {
//Create an instance of a class
public static void main(String[] args) {
SayHello object = new SayHello();
SayHello.Hello("Jaime");
//passing "Jaime" into the class SayHello and its function.
}
}
In the above code, the console will output "Hello Jaime" as it would in the pseudo-code. Any name can be passed to the function, and you can add more than just your name. You can give this a shot yourself by downloading the free Java JDK at:
https://www.oracle.com/java/technologies/javase-downloads.html
It is highly recommended that you use an IDE (integrated development environment) to help you with syntax and provide a nice interface. Some highly favored IDEs include:
Eclipse
Visual Studio Code
IntelliJ
Happy coding!
No comments:
Post a Comment