Getting Started with Oops | Object-Oriented Programming in Java
What is Objective-Oriented Programming?
Objective-Oriented programming(Oops) is the standard way to code. It is used to structure software program into simple, reusable piece of code. You may have heard the term oops. When solving a problem, oops involves the practice of representing key concepts through objects in your software. Take a look around, You see a computer or a tablet. You may also see other physical objects like a table, a door or a coffee mug. They are objects as well. The room itself is even an object.
So why should you use objects to represent things in your code?
It is a way of keeping your code organized, flexible and reusable. It keeps code organized by having related details and specific functions in distinct, easy to find places. This creates flexibility because you can easily change details in a modular way without affecting the rest of the code. You can also reuse code and keep your program simple.
Let’s explore Oops.
Consider seminar room. The first object we identify is the room itself. The room has details such as the room number and the seating capacity of the room. There are many physical objects such as the chair, the table, the projector and the white board. Each of these physical objects could be represented by objects in software. The projector has specs related to its performance such as resolution and brightness. A chair has its own details such as seat dimensions that would be relevant to a user of that object. Objects can also have individual responsibilities or behaviors. For example, the projector is responsible to power on, take a video input and display an image. A person is also an object. Let’s put a person object in this room. A person object knows their name and age and a laptop knows its specs.In Oops, even inanimate objects know their information. So a chair knows its dimensions and location. A door knows its frame height and the angle that the door is ajar. The white board knows its height and width as well as if it’s blank or contains writing. With object-oriented thinking, you often think of everything as objects even living things. Start viewing the world around you in terms of objects. It will help you out when you’re called upon to design software using Oops. Think objects.
Concepts in Objective-Oriented programming(Oops).
There are 4 core concepts in Oops:
Abstraction
Abstraction provides the outside world with only essential information, in a process of representing essential features without including implementation details. Abstraction is the idea of simplifying a concept in the problem domain to its essentials within some context. Abstraction allows you to better understand a concept by breaking it down into a simplified description that ignores unimportant details. For example, we might want to create an abstraction for a food. In a health context, its nutritional value and not its cost would be part of a simplified description of a food. Good abstraction emphasizes the essentials needed for the concept and removes details that are not essential. Also an abstraction for a concept should make sense for the concept’s purpose. This idea applies the Rule of Least Astonishment. That is, “ The abstraction captures the essential attributes and behavior for a concept with no surprises and no definitions that fall beyond its scope. You don’t want to surprise anyone trying to understand your abstraction with irrelevant characteristics”. A good real world example is book. We don’t know exact specifics, such as page count, color, or the size of the book.
The concept of abstraction is that we focus on essential qualities, rather than the specific characteristics of one particular example.
Abstraction in Java:
In Java, abstraction is achieved using abstract class and interfaces. An abstract class is achieved using abstract classes and interfaces.
Abstract class in JAVA:
In Animal.java the Animal class is defined as abstract. The method makeSound is also abstract as it has no implementation in the superclass. We have inherited from the the Animal class and define the makeSound() method for the subclass.
Every Animal makes a sound, but each has a different way to do it. So we defined an abstract class Animal, and leave the implementation of how they make sounds to the subclasses.
Important points to remember while defining abstract class.
- If a class is declared abstract it cannot be instantiated i.e you cannot create objects of that type.
- To use abstract class you have to inherit it from other class.
- Any class that contains an abstract method should be defined as abstract.
Interface in JAVA:
An interface is completely abstract class that contains only abstract methods.
In Animal.java the animal class is defined as interface which contains only abstract methods while no need of defining abstract in front of method. The Cat.java implements methods makeSound and eat written in Animal class.
Some specifications for interfaces:
- An interface is a reference type, similar to a class, that can contain only constants(static final variables), method signatures, default methods, static methods, and nested types.
- Cannot contain a constructor because the interfaces cannot be instantiated.
- Interfaces can extend only interfaces.
- A class can implement any number of interfaces.
- An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.
- Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
- Methods in an interface are implicitly public.
Encapsulation
The idea behind encapsulation is to ensure that implementation details are not visible to users. Encapsulation involves three ideas. As the name suggests, it’s about making a sort of capsule. The capsule contains something inside, some of which you can access from the outside, and some of which you cannot. First, you bundle attribute values or data, and behaviors or functions, that manipulate those values together into a self-contained object. Second, you can expose certain data and functions of that object, which can be accessed from other objects. Third, you can restrict access to certain data and functions to only within that object. In short, encapsulation forms a self-contained object by bundling the data and functions it requires to work, exposes an interface whereby other objects can access and use it, and restricts access to certain inside details.
Encapsulation in JAVA:
The first step in encapsulation is data hiding. The variable of one class will be hidden from the other classes, accessible only through the methods of the current class. In Account.java the account_no, name, email and amount is a private declared variable. This is called data hiding. Then the method for accessing the private variable are defined with the help of methods called getter and setter. Getter Methods are methods that retrieve data, and their names typically begin with get and end with the name of the attribute whose value you will be returning. Here getName is the example of getter method which returns name. Setter Methods change data, and their names typically begin with set and end with the name of the variable you wish to set. Here setName is the setter method to set name in private variable. Data integrity is why you have Getter and Setter Methods. In order to change a piece of data, you need to go through the correct channels.
Inheritance
Many behaviors and systems in the real world operate through repetitious actions. We can model behaviors using methods. It lets us generalize behaviors and it eliminates the need to have identical code written throughout a program. We can generalize repetitious code that we would need to write by making a separate method and calling it. This helps us to reduce the amount of near identical looking code throughout our system. Methods are a way of applying the same behavior to a different set of data. Generalization is frequently used when designing algorithms, which are meant to be used to perform the same action on different sets of data. We can generalize the actions into its own method, and simply pass it through a different set of data through arguments. Generalization happens to be one of the main design principles of object-oriented modeling and programming. But it’s achieved differently than what we’ve just seen with methods. In generalization we take repeated, common, or shared characteristics between two or more classes and factor them out into another class. Specifically, you can have two classes, a parent class and a child class. When a child class inherits from a parent class, the child class will have the attributes and behaviors of the parent class. You place common attribute and behaviors in your parent class. There can be multiple child classes that inherit from a parent class, and they all will receive these common attributes and behaviors. The child classes can also have additional attributes and behaviors, which allow them to be more specialized in what they can do. In standard terminology, a parent class is known as a superclass and a child class is called the subclass.
Inheritance in JAVA:
Inheritance is the process that enables one class to acquire the properties of another. With inheritance, the information is placed in a more manageable, hierarchical order. When you inherit from an existing class, you can reuse methods and fields of the parent class. The class inheriting the properties of the another is subclass or derived class. The class whose properties are inherited is the superclass.
Here, Dog is the subclass, and Animal is the superclass. Recall the protected access modifier, which makes the members visible only to the subclasses. Here d.legs gets printed where legs is a private variable in animal class.
Polymorphism
A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism. Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. Polymorphism refers to the idea of “having many forms”. Polymorphism occurs when there is a hierarchy of classes related to each other through inheritance.
Polymorphism in JAVA:
A call to a member method will cause a different implementation to be executed, depending on the type of the object invoking the methods. Dog and Cat are classes that inherit from the Animal class. Each class has its own implementation of the makeSound() method. As all Cat and Dog objects are Animal objects, we have created two reference variables of type Animal, and pointed them to the Cat and Dog objects.