Object-Oriented Programming in Java - A Beginner's Guide
Object-Oriented Programming in Java - A Beginner's Guide
By Patrick Cyubahiro
What is Object-Oriented
Programming?
Object-oriented programming (OOP) is a fundamental
programming paradigm based on the concept of _“objects”_. These
objects can contain data in the form of elds (often known as
attributes or properties) and code in the form of procedures (often
known as methods).
1. What is Java?
2. What is a class?
3. What is an object?
9. Interfaces in Java.
What is Java?
Java is a general-purpose, class-based, object-oriented
programming language, which works on different operating
systems such as Windows, Mac, and Linux.
Desktop applications
Web applications
Embedded systems
In Java, every application starts with a class name, and this class
must match the le name. When saving a le, save it using the class
name and add “.java” to the end of the le name.
Don't worry if you don't understand the above code at the moment.
We are going to discuss, step by step, each line of code just below.
For now, I want you to start by noting that every line of code that
runs in Java must be in a class.
You may also note that Java is case-sensitive. This means that Java
has the ability to distinguish between upper and lower case letters.
For example, the variable _“myClass” and the variable “myclass”_ are
two totally different things.
class ClassName {
// fields
// methods
}
Note that in Java, we use elds to store data, while we use methods
to perform operations.
int y = 2;
Note that a class should always start with an uppercase rst letter,
and the Java le should match the class name.
Example 1:
Object: car.
Example 2:
Object: house.
int y = 10;
System.out.println(myObj.y);
Default
Public
Private
Protected
Here is an example of how you can use the default access modi er:
class SampleClass
{
void output()
{
System.out.println("Hello World! This is an Introduction to OOP -
}
}
class Main
{
public static void main(String args[])
{
SampleClass obj = new SampleClass();
obj.output();
}
}
// Car.java file
// public class
public class Car {
// public variable
public int tireCount;
// public method
public void display() {
System.out.println("I am a Car.");
System.out.println("I have " + tireCount + " tires.");
}
}
// Main.java
public class Main {
public static void main( String[] args ) {
// accessing the public class
Car car = new Car();
Output:
I am a Car.
I have 4 tires.
You may also note that the private entities are not visible even to
the subclasses of the class.
class SampleClass
{
When we run the above program, we will get the following error:
So, the best way to access these private variables is to use the
getter and setter methods.
Let's have a look at how we can use the getters and setters method
to access the private variable.
class SampleClass
{
return this.task;
}
this.task= task;
}
}
System.out.println(task.getTask());
}
}
To learn more about the this keyword, you can read this article
here.
Let's have a look at how we can use the protected access modi er:
// Multiplication.java
package learners;
return a*b;
// Test.java
package javalearners;
import learners.*;
System.out.println(obj.multiplyTwoNumbers(2, 4));
} //output: 8
int a;
public Main() {
a = 3 * 3;
System.out.println(myObj.a);
4. After that, we have set the initial value for variable a that
we have declared. The variable a will have a value of 9. Our
program will just take 3 times 3, which is equal to 9. You are
free to assign any value to the variable a . (In programming,
the symbol “*” means multiplication).
Every Java program starts its execution in the main() method. So,
we have used the public static void main(String[] args) , and
that is the point from where the program starts its execution. In
other words, the main() method is the entry point of every Java
program.
Now I'll explain what every keyword in the main() method does.
Main.
Main is the name of the Java main method. It is the identi er that
the Java Virtual Machine looks for as the starting point of the java
program.
Let me give you an example of how you can use methods in Java.
class Main {
// create a method
public int divideNumbers(int x, int y) {
int division = x / y;
// return value
return division;
}
int firstNumber = 4;
int secondNumber = 2;
Output:
Dividing 4 by 2 is: 2
Now that you know some Java basics, let's look at object-oriented
programming principles in a bit more depth.
2. Inheritance
3. Abstraction
4. Polymorphism
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
Inheritance in Java
Inheritance allows classes to inherit attributes and methods of
other classes. This means that parent classes extend attributes and
behaviors to child classes. Inheritance supports reusability.
class Animal {
class Main {
public static void main(String[] args) {
}
}
Output:
My name is Jerry
I can eat
Abstraction in Java
Abstraction is a concept in object-oriented programming that lets
you show only essential attributes and hides unnecessary
information in your code. The main purpose of abstraction is to
hide unnecessary details from your users.
When you send an e-mail, you just need to enter the email address
of the receiver, the email subject, type the content, and click send.
Example:
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzzz");
}
}
class Main {
public static void main(String[] args) {
Cow myCow = new Cow(); // Create a Cow object
myCow.animalSound();
myCow.sleep();
}
}
Polymorphism in Java
Polymorphism refers to the ability of an object to take on many
forms. Polymorphism normally occurs when we have many classes
that are related to each other by inheritance.
Example:
We are going to create objects Cow and Cat, and call the
animalSound() method on each of them.
class Animal {
public void animalSound() {
System.out.println("An animal can make a sound.");
}
}
class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myCow = new Cow();
Animal myCat = new Cat();
myAnimal.animalSound();
myCow.animalSound();
myCat.animalSound();
}
}
Interfaces in Java
An interface is a collection of abstract methods. In other words,
an interface is a completely "abstract class" used to group
related methods with empty bodies.
An interface speci es what a class can do but not how it can do it.
Example:
// create an interface
interface Language {
void getName(String name);
}
class Main {
public static void main(String[] args) {
ProgrammingLanguage language = new ProgrammingLanguage();
language.getName("Java");
}
}
Output:
Conclusion
We have looked at some of the main object-oriented programming
concepts in this article. Having a good understanding of these
concepts is essential if you want to use them well and write good
code.
If you read this far, thank the author to show them you care. Say Thanks
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000
people get jobs as developers. Get started
Donations to freeCodeCamp go toward our education initiatives, and help pay for Linux React CI/CD
Mobile App
Publication powered by Hashnode About Alumni Network Open Source Shop Support Sponsors Academic Honesty Code of Conduct Privacy Policy Terms of Service Copyright Policy