Copyright

What is a Class in Java? - Definition & Examples

What is a Class in Java? - Definition & Examples
Coming up next: Static Nested Classes in Java: Definition & Example

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Java Objects
  • 0:54 Information Hiding
  • 2:23 Constructor
  • 3:32 Inheritance
  • 4:00 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java is an object-oriented language: at its heart are objects and classes. This lesson will define classes in Java, how they are used, and how classes can inherit information from other classes.

Java Objects

Let's have a little class (pun intended) on object-oriented programming. At its core, Java is an object-oriented language. It consists of objects, which are modules that can have data and some instructions. An object should be able to sustain itself. An Employee object, for example, contains some information about an employee. You can send salary, wage, benefit balances, etc. to the Employee object and it can do something with that info.

A class, on the other hand, is used to create objects. Think of a class as a blueprint for creating objects. In the following example, the object is really an Employee. However, we've created a class so that we can structure the object to our needs. Check out our example below. You'll notice the different portions of the Java code underneath the Employee object.

public class Employee {
  public String empName; //Employee name
  public double hourly_rate, hours_worked;
  public double getWeeklyHours() {
   return (hourly_rate * hours_worked);
  }
} //end of class

Information Hiding

One of the most powerful concepts in object-oriented programming is encapsulation, which essentially means information hiding. This means that certain information is encapsulated, or contained, within classes. Let's go back to the Employee example. There are variables within the Employee class that we don't want other parts of our program to see, such as pay rate. Another feature is that other parts of the program don't need to know how the Employee class works.

A class helps to define the object (such as, Employee, Publication, User, etc.) in object-oriented programming. Basically, a class is used to create an object. It determines the variables and methods that will be inside the object. A method is a task, such as performing a calculation or processing user input. These pieces are fairly constant.

Objects come and go when objects are created and discarded during normal program operation, but the classes are part of the structure and remain. There is a very technical term for this, which you'll hear as you learn more about object-oriented programming: instantiation. An object is instantiated when the class is invoked. Again, we return to the Employee class. The class itself remains constant. Each time Employee is used to create a new instance of an employee, we say that class has been instantiated.

How are objects instantiated? Another object-oriented term arises for this need: the constructor, which creates the new instance. Let's look at constructors next.

Constructor

The constructor lets us instantiate and create new instances of classes. Let's say we have an Employee class and are creating a new instance of Employee, the code would be:

Employee 9999 = new Employee();

The above example is a very simple example. Remember that the Employee had three variables: Employee name, pay rate, and hours worked. When we instantiate new Employee objects, it makes sense to pass those values in. Take a look at a full code snippet that shows this being shown below:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support