In Java, everything revolves around Objects and Classes. Java is an object-oriented programming language, which means it uses objects to model real-world things. Classes are the blueprints for creating objects, defining their structure and behavior. In this tutorial, we'll cover the basics of Java objects and classes, and how to use them.
A class in Java is a blueprint from which individual objects are created. It defines a datatype by bundling data and methods that work on the data into one single unit.
A class is defined using the class
keyword. Here is a simple example:
java1public class Dog { 2 // Attributes (Variables) 3 String breed; 4 int age; 5 String color; 6 7 // Methods 8 void bark() { 9 System.out.println("Woof!"); 10 } 11 12 void displayInfo() { 13 System.out.println("Breed: " + breed + " Age: " + age + " Color: " + color); 14 } 15}
In the above example, Dog
is a class that has three attributes (breed
, age
, and color
) and two methods (bark
and displayInfo
).
An object is an instance of a class. You can create an object using the new
keyword.
java1public class TestDog { 2 public static void main(String[] args) { 3 // Creating Objects 4 Dog myDog = new Dog(); 5 Dog yourDog = new Dog(); 6 7 // Initializing objects 8 myDog.breed = "Bulldog"; 9 myDog.age = 5; 10 myDog.color = "White"; 11 12 yourDog.breed = "Shepherd"; 13 yourDog.age = 3; 14 yourDog.color = "Black"; 15 16 // Calling object methods 17 myDog.bark(); 18 myDog.displayInfo(); 19 20 yourDog.bark(); 21 yourDog.displayInfo(); 22 } 23}
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes:
java1public class Dog { 2 String breed; 3 int age; 4 String color; 5 6 // Constructor 7 public Dog(String breed, int age, String color) { 8 this.breed = breed; 9 this.age = age; 10 this.color = color; 11 } 12 13 void bark() { 14 System.out.println("Woof!"); 15 } 16 17 void displayInfo() { 18 System.out.println("Breed: " + breed + " Age: " + age + " Color: " + color); 19 } 20}
You can create an object with the new constructor like this:
java1public class TestDog { 2 public static void main(String[] args) { 3 // Creating Objects with constructor 4 Dog myDog = new Dog("Bulldog", 5, "White"); 5 Dog yourDog = new Dog("Shepherd", 3, "Black"); 6 7 // Calling object methods 8 myDog.bark(); 9 myDog.displayInfo(); 10 11 yourDog.bark(); 12 yourDog.displayInfo(); 13 } 14}
Access modifiers in Java control the visibility of class members. The four access levels are:
Encapsulation is one of the four fundamental OOP concepts. The idea is to keep the fields (class variables) private and provide public methods to access them (getters and setters).
java1public class Dog { 2 private String breed; 3 private int age; 4 private String color; 5 6 public Dog(String breed, int age, String color) { 7 this.breed = breed; 8 this.age = age; 9 this.color = color; 10 } 11 12 // Getters and Setters 13 public String getBreed() { 14 return breed; 15 } 16 17 public void setBreed(String breed) { 18 this.breed = breed; 19 } 20 21 public int getAge() { 22 return age; 23 } 24 25 public void setAge(int age) { 26 this.age = age; 27 } 28 29 public String getColor() { 30 return color; 31 } 32 33 public void setColor(String color) { 34 this.color = color; 35 } 36 37 public void bark() { 38 System.out.println("Woof!"); 39 } 40 41 public void displayInfo() { 42 System.out.println("Breed: " + breed + " Age: " + age + " Color: " + color); 43 } 44}
Inheritance allows us to define a class that inherits all the methods and properties from another class. The class that is inherited from is called the superclass, and the class that inherits is called the subclass.
java1// Superclass 2public class Animal { 3 protected String type; 4 5 public Animal(String type) { 6 this.type = type; 7 } 8 9 public void displayType() { 10 System.out.println("Type: " + type); 11 } 12} 13 14// Subclass 15public class Dog extends Animal { 16 private String breed; 17 18 public Dog(String type, String breed) { 19 super(type); 20 this.breed = breed; 21 } 22 23 public void displayBreed() { 24 System.out.println("Breed: " + breed); 25 } 26} 27 28public class TestInheritance { 29 public static void main(String[] args) { 30 Dog myDog = new Dog("Mammal", "Bulldog"); 31 myDog.displayType(); // Inherited method 32 myDog.displayBreed(); 33 } 34}