Java Lesson 16: Abstract Classes

Hello everybody,

Michael here, and today’s post will be on abstract classes in Java.

First off, let’s start by discussing abstract classes. What is an abstract class? Well, let’s start off by explaining the concept of data abstraction. In Java (or any other programming language), data abstraction is the process of hiding certain details of the program and showing only essential information to the user. An abstract class is a restricted class that cannot be used to create objects-abstract classes can only be accessed if other classes inherit them (I’ll explain that more later). Also, just as regular classes can have regular methods, abstract classes can have abstract methods, which are methods that don’t have a body and can only be used in abstract classes.

  • It probably seems obvious to you guys, but the magic word  you would use to make a class or method abstract is abstract .
  • Abstract methods are created without a body, but the body of an abstract method is created in a subclass.

Alright, now that I’ve given you guys some explanation, let’s create an abstract class:

abstract class Abstract
{
public abstract void password();

public void username()
{
System.out.println(“MIKE2020”);
}
}

In this example, I created an abstract class with an abstract method-password-and a regular method-username(). Since abstract methods don’t have a body, the password() method doesn’t have a body either; the body of the password() method will be inherited from a subclass.

  • Also, I know it seems like a no-brainer, but abstract classes and methods would be denoted with the abstract key-word.

Another thing I wanted to note is that it’s not possible to create an object of an abstract class, since you can only access abstract classes by inheriting them from subclasses. Trying to create an object of an abstract class will generate an error, so statements like this aren’t possible in Java:

Abstract myObj = new Abstract();

But wait-I did say that you can access abstract classes and their methods by inheriting them from subclasses. Here’s how to do that:

class Login extends Abstract
{
@Override
public void password()
{
System.out.println(“abstract10n”);
}
}

In this example, I created a subclass of Abstract called Login (the fact that it’s a subclass is denoted by the extends key-word). Since Login is a subclass of Abstract, I was able to create the body of the password() method (which consists of a single System.out.println() command). Keep in mind that since Login isn’t an abstract class, password() isn’t an abstract method.

Now that we’ve got an abstract class and a subclass, what else do we need? We would need a main class to run the program. Here’s the main class:

public class Credentials
{
public static void main (String [] args)
{
Login login1 = new Login();
login1.username();
login1.password();
}
}

Here’s the output:

run:
MIKE2020
abstract10n
BUILD SUCCESSFUL (total time: 2 seconds)

In this example, I created the main class Credentials to run the program. To access the username() and password() methods, I had to create an object of the Login class, not the Abstract class, because I can only create objects of the subclass, not the abstract class. Also, I can only access the abstract class’s methods through the subclass, not through the abstract class itself.

Anyway, you might be wondering “What is the purpose of abstraction?”. The point of abstraction is that it improves the security of your program, as abstraction hides certain details of a program and only shows a program’s important details. Program details hidden in an abstract class are harder to access, as you would need to create a new subclass to access the abstract class and all of its methods.

Thanks for reading,

Michael

 

 

 

Two Years In

Hello everybody,

It’s Michael, and today’s post will be a little different. Why would that be?

Well, today is the blog’s two year anniversary. Yup-hard to believe I launched this blog two years ago. And harder to believe that I was still living at my childhood home in Miami Springs, FL when I got the idea to launch this blog.

Just like I did for the Thank You For 1 Year post I wrote for the blog’s first anniversary, this post will be my annual appreciation/thank-you post to all of the amazing readers of this blog.

So, where do I begin? How about a little by-the-numbers rundown of this blog’s content?

Since the launch of the blog, we’ve had:

  • 71 posts (with this post being the 71st)
  • 19 R lessons
  • 9 R analyses
  • 15 Java lessons
  • 3 Java program demos
  • 10 Python lessons
  • 1 Python program demo
  • and 10 MySQL lessons (though it’s been ages since I’ve posted any SQL content)

Impressive, right? Now, it may surprise you to learn that this blog has been viewed all around the world, not just in the US. Let’s see where in the world has this blog been viewed:

  • the United States
  • Thailand
  • Vietnam
  • Saudi Arabia
  • the United Kingdom
  • Brazil
  • the Philippines
  • India
  • France
  • Italy
  • New Zealand
  • Russia

Well, looks like this blog has captured the attention of readers worldwide. Now, let’s see what the top 5 most viewed posts are, starting with my most viewed to date (I provided links to each post):

  1. R Lesson 1: Basic R commands
  2. R Analysis 9: ANOVA, K-Means Clustering & COVID-19
  3. R Analysis 8: Linear Regression & the Top 200 Albums of the 2010s
  4. Java Lesson 11: Method Making
  5. MySQL Lesson 10: Creating Views

It looks like my very first lesson is the most viewed post to date. Honestly, I thought that my post on COVID-19 would take the top spot, given that it was my first post about a current event AND that the COVID-19 pandemic is still going on.

Now, let’s do a top 5 most viewed posts year-by-year breakdown (links provided to all posts):

2018 top 5:

  1. R Lesson 1: Basic R commands
  2. MySQL Lesson 10: Creating Views
  3. R Lesson 4: Logistic Regression Models
  4. MySQL Lesson 1: Building an ER Diagram
  5. MySQL Lesson 3: Intro to Querying

2019 top 5:

  1. Java Lesson 11: Method Making
  2. R Lesson 15: Decision Trees
  3. R Lesson 16: R Loops
  4. R Lesson 14: Random Forests
  5. Java Lesson 1: Introduction to Java & “Hello World”

2020 top 5:

  1. R Analysis 9: ANOVA, K-Means Clustering & COVID-19
  2. R Analysis 8: Linear Regression & the Top 200 Albums of the 2010s
  3. Java Lesson 15: Enums
  4. Python Lesson 7: Lists & Tuples
  5. Python Program Demo 1: Using the Jupyter Notebook

Looks like my R posts have the most popular, as the R posts take up 7 of the spots on these lists. Also, my COVID-19 R analysis is the most viewed post to date in 2020 (which isn’t surprising given the current pandemic situation).

This blog has certainly come a long way since its inception two years ago. But none of these amazing stats would be possible without you-the readers. You come from all over the world, but you all come to this blog for the same purpose-to learn-and I appreciate all of you. Your support motivates me to create more great content for guys to read-and don’t worry, I’ve got years of content to come. I’m not ending this blog anytime soon.

Happy 2nd birthday Michael’ Analytics Blog! Looking forward to another great year of programming content!

As always, thanks for reading!

Michael

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

u