Back to Course

Data Structures and Algorithms with Java

0% Complete
0/0 Steps

Data Structures and Algorithms are two of the most important topics in computer science. In order to understand and implement these concepts, it is essential to have a good knowledge of classes and objects in Java. Classes and objects are the fundamental building blocks of Java and are used to create complex programs. By understanding classes and objects, a programmer can create efficient and effective data structures and algorithms.

What are Classes and Objects?

Classes and objects are the two core components of object-oriented programming. A class is a blueprint or template used to define the properties, structure, and behavior of objects. An object is an instance of a class and is used to store data in a program. A class is like a cookie cutter and an object is like a cookie.

A class is declared using the “class” keyword followed by the class name. In the following example, we create a class called Person with two instance variables (name and age) and one method (sayHello).

public class Person {
   // instance variables
   private String name;
   private int age;
   
   // method
   public void sayHello() {
      System.out.println("Hello, my name is " + name);
   }
}

An object is created from a class using the “new” keyword followed by the class name and parentheses. In the following example, we create an object called john from the Person class.

Person john = new Person();

We can then use the john object to access the instance variables of the Person class.

john.name = "John Smith";
john.age = 32;

We can also use the john object to call the sayHello() method.

john.sayHello(); // prints "Hello, my name is John Smith"

Constructors and Methods

A constructor is a special method that is used to create an object. Constructors are declared using the same syntax as other methods, but with the method name being the same as the class name. In the following example, we create a constructor for the Person class.

public class Person {
   // instance variables
   private String name;
   private int age;
   
   // constructor
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }
   
   // method
   public void sayHello() {
      System.out.println("Hello, my name is " + name);
   }
}

The constructor for the Person class takes two parameters (name and age) and assigns them to the instance variables of the same name.

We can then use the constructor to create an object.

Person john = new Person("John Smith", 32);

We can also use the john object to call the sayHello() method.

john.sayHello(); // prints "Hello, my name is John Smith"

Access Modifiers

Access modifiers are keywords used to limit the access of a class, object, or method. The three most common access modifiers are public, private, and protected.

Public classes, objects, and methods can be accessed by any other class or object in the program. Private classes, objects, and methods can only be accessed by the class or object that declared them. Protected classes, objects, and methods can only be accessed by the class or object that declared them, as well as any subclasses of that class.

In the following example, we create a class called BankAccount with two instance variables (balance and interestRate) and two methods (deposit and withdraw). The instance variables are declared as private, which means they can only be accessed by the BankAccount class. The methods are declared as public, which means they can be accessed by any other class or object in the program.

public class BankAccount {
   // instance variables
   private double balance;
   private double interestRate;
   
   // methods
   public void deposit(double amount) {
      balance += amount;
   }
   
   public void withdraw(double amount) {
      balance -= amount;
   }
}

Inheritance

Inheritance is a way for a class to inherit the properties, structure, and behavior of another class. This allows us to create a class hierarchy and to reuse code.

In the following example, we create a class called SavingsAccount which inherits from the BankAccount class. The SavingsAccount class has all the same instance variables and methods as the BankAccount class, but also has an additional instance variable (monthlyFee) and method (applyFee).

public class SavingsAccount extends BankAccount {
   // instance variable
   private double monthlyFee;
   
   // constructor
   public SavingsAccount(double interestRate, double monthlyFee) {
      this.interestRate = interestRate;
      this.monthlyFee = monthlyFee;
   }
   
   // method
   public void applyFee() {
      balance -= monthlyFee;
   }
}

We can then create an object from the SavingsAccount class and use it to access the instance variables and methods of both the BankAccount and SavingsAccount classes.

SavingsAccount savingsAccount = new SavingsAccount(0.05, 5.00);
savingsAccount.deposit(100.00);
savingsAccount.withdraw(50.00);
savingsAccount.applyFee();

Interfaces

An interface is a type of class that is used to define the behavior of objects. Interfaces are declared using the “interface” keyword followed by the interface name. In the following example, we create an interface called Comparable which defines a compareTo() method.

public interface Comparable {
   int compareTo(Object other);
}

A class can implement an interface by declaring it using the “implements” keyword followed by the interface name. In the following example, we create a class called Person which implements the Comparable interface.

public class Person implements Comparable {
   // instance variables
   private String name;
   private int age;
   
   // constructor
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }
   
   // method
   public int compareTo(Object other) {
      Person otherPerson = (Person) other;
      if (this.age < otherPerson.age) {
         return -1;
      } else if (this.age > otherPerson.age) {
         return 1;
      } else {
         return 0;
      }
   }
}

Conclusion

Classes and objects are the fundamental building blocks of object-oriented programming in Java. A class is a blueprint or template used to define the properties, structure, and behavior of objects. An object is an instance of a class and is used to store data in a program. Constructors, methods, access modifiers, inheritance, and interfaces are used to create and manipulate classes and objects. By understanding classes and objects, a programmer can create efficient and effective data structures and algorithms.

Exercises

Create a class called BankAccount with two instance variables (balance and interestRate) and two methods (deposit and withdraw).

public class BankAccount {
   // instance variables
   private double balance;
   private double interestRate;
   
   // methods
   public void deposit(double amount) {
      balance += amount;
   }
   
   public void withdraw(double amount) {
      balance -= amount;
   }
}

Create a class called SavingsAccount which inherits from the BankAccount class. The SavingsAccount class should have an additional instance variable (monthlyFee) and method (applyFee).

public class SavingsAccount extends BankAccount {
   // instance variable
   private double monthlyFee;
   
   // constructor
   public SavingsAccount(double interestRate, double monthlyFee) {
      this.interestRate = interestRate;
      this.monthlyFee = monthlyFee;
   }
   
   // method
   public void applyFee() {
      balance -= monthlyFee;
   }
}

Create an interface called Comparable which defines a compareTo() method.

public interface Comparable {
   int compareTo(Object other);
}

Create a class called Person which implements the Comparable interface and has two instance variables (name and age).

public class Person implements Comparable {
   // instance variables
   private String name;
   private int age;
   
   // constructor
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }
   
   // method
   public int compareTo(Object other) {
      Person otherPerson = (Person) other;
      if (this.age < otherPerson.age) {
         return -1;
      } else if (this.age > otherPerson.age) {
         return 1;
      } else {
         return 0;
      }
   }
}

Create an object called john from the Person class and use it to call the compareTo() method.

Person john = new Person("John Smith", 32);
Person jane = new Person("Jane Doe", 28);
john.compareTo(jane); // returns 1