What is Object-oriented programming?
Objected-oriented programming(OOP) is a complicated topic that whole textbooks can be written about. OOP has simplified coding to a significant degree and is now essentially a staple of modern computer programming. In short, object-oriented programming allows objects, which in Java are represented using classes, to be easily used by other classes and other packages. Packages are generally collections of classes, interfaces, enums, and records that perform particular functions using code. A package can be used by other programs and extended to meet the design needs of the application. Usually, this is done by importing the package as a module to be used by the application.
For example, say we have a class
public class Person {
private int age;
private String name;
private String description;
// The constructor
public Person(int age, String name, String description) {
this.age = age;
this.name = name;
this.description = description;
}
// The getter functions
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String description() {
return description;
}
}
Notice in the above class there are private class variables but public getters. This is generally the convention in creating classes to be used by other applications.
How can classes be used by other classes and packages?
Another Java application can then import this class for its own use generally by typing something like using packageName.className at the top of the application.
Another application might extend this class to improve the functionality of the class or to meet the design needs of their application.
An example might be:
public class policeOfficer extends packageName.Person {
int age;
String name;
String description;
public policeOfficer(int age, String name, String description) {
super(age, name, description); // Super here calls the superclass that is being extended. In this case the Person class above.
}
public void eatDonuts() {
// Of course there would be a function like this.
System.out.println(“Officer ” + name + ” is currently eating donuts.”);
}
public void arrestPerson() {
// Of course there would be a function like this.
System.out.println(“Officer ” + name + ” is arresting someone.”);
}
// The above function didn’t exist in the original Person class above.
}
Object-oriented programming is a very complex area of computer science. For more information about Object-oriented programming or other computer science or computer programming topics, I recommend this website page here. I also recommend the book Head First Design Patterns: Building Extensible & Maintainable Object-Oriented Software by Eric Freeman and Elisabeth Robson with Kathy Sierra and Bert Bates.
To view more blog posts from our company visit our blog section here.