Classes, Objects, & Interfaces in Java
An interface is basically an abstract class with the sole purpose of holding attributes for another class to implement.
Vocabulary Check đź“–
You instantiate an object by declaring its name and/or parameters to be used in the program.
Class inheritance is when a class extends a superclass and inherits its contents.
An object's state is the data that represents it stored in variables.
When you implement an interface, you are using the methods in it.
You override methods in an interface to make them suit your class object.
The methods in an interface are called attributes.
What Is A Class?
What Is An Object?
What Is An Interface?
What Is A Class?
When I think of a blueprint, I think of a design for assembling parts that come together to create, you guessed it, an object. But what I couldn't wrap my head around was the idea of a virtual object.
You've probably heard this classic definition: A class is a blueprint for an object. And if you were anything like me, you didn't think that made any sense. When I think of a blueprint, I think of a design for assembling parts that come together to create, you guessed it, an object. But what I couldn't wrap my head around was the idea of a virtual object.
You can see how this is a problem. Especially in the realm of object-oriented programming. And when creating an app, the app may involve multiple objects working in tandem with each other. So it's a huge mistake to brush aside this misunderstanding. I realized my issue was that I couldn't grasp what a class was without fully understanding the thing it defines.
First thing to understand is you can't have a class (unless it's static) without an object in mind. If I had an idea to create a calculator object, then I would need a "design" or class of "parts" or methods that perform the behavior of a calculator.
Here is an example of a class in Java:
public class RightTriangle{
//state
}
The second thing to remember is that there are two types of classes, a regular java class, and an abstract class. An abstract class is basically a class that cannot be instantiated. But that's another topic for another day.
And lastly, understand the class hierarchy. There is the base/parent/superclass and the child class. A child class is a class that extends another (parent) class. The reason for this is so you can inherit the code of the first class. Here is what that hierarchy looks like:
Rules of class inheritance:
- A child class that is not abstract must implement the interfaces and methods of the parent class.
- Classes extend classes and implement interfaces.
- A parent class can have as many child classes as it wants.
- A child class can only inherit from one parent class.
Unlike C++, the concept of Multiple Inheritance is not supported by Java.
What Is An Object?
An object is an instance of a class. Is it clicking? If not, I'll explain. An object is an idea, an instance of something in the real world. The class provides the definition— the assembly— of the instance. How do you create an object? By instantiating it like so:
RightTriangle rtriangle = new RightTriangle();
As depicted above, a new instance of type Triangle has been created. Triangle is a data type we have fabricated simultaneously with our triangle object. And by the way, there are other ways to instantiate objects, which I will make a note to discuss in the future.
What is an Interface?
Alright, this is something that took me a while to get.
"An interface is kinda like a class that doesn't do anything? And has no body, whatever that means."
— Young Inexperienced Me.
Yeah... when your definition sounds that uncertain, it's time to relearn. So what is an interface, exactly? An interface is basically an abstract class with the sole purpose of holding attributes for another class to implement. If you have experience with Scala or Python, this is the near equivalent of a trait.
interface Shape{
public void getPerimeter();
public void getArea();
}
public class RightTriangle implements Shape {
double height = 12;
double base = 8;
double hypotenuse = Math.sqrt(Math.pow(height, 2) + Math.pow(base, 2)); // 14.422...
double area = base * height / 2;
double perimeter = height + base + hypotenuse;
@Override
public void getPerimeter(){
System.out.println("The perimeter of the right triangle is " + perimeter);
}
@Override
public void getArea(){
System.out.println("The area of the right triangle is " + area);
}
public static void main(String[] args) {
RightTriangle rtriangle = new RightTriangle();
rtriangle.getPerimeter(); // will return 34.422...
rtriangle.getArea();// will return 48.0
}
}
In the Example above, we have created an interface Shape
with attributes getPerimeter()
and getArea()
that RightTriangle
implements. From there, if we want to add behavior to these empty methods, we have to override them. Inside of the braces ([]), we would put the code that makes getPerimeter() unique to the class RightTriangle, same for getArea(). While using the annotation @Override
isn't necessary, it tells the compiler to makes sure that the methods are in fact overridden. Think of it as the equivalent to a proof on your Algebra worksheet.
There are rules to when and how you use an interface:
- A class cannot extend an interface.
- Multiple classes can implement the same interface.
- An interface never has state.
- Because it must have immutability.
- Therefore all variables in an interface must be final.
- Once an interface is implemented, its attributes must be overriden.
- The only thing that can extend an interface is another interface.
- An interface cannot implement another interface.
Interfaces are very interesting. And they can be used to your advantage if you understand how, when, and why. I will detail more on interfaces in a future post.
A class describes an object, an object represents a class, and an interface provides common attributes for any class to implement. Now that you understand these three concepts, you get the gist of object-oriented programming in Java!