Great! Let's move on to more advanced class concepts in Java.
We can cover things like:
1. Access Modifiers
2. Static Keyword
3. Inheritance
4. Polymorphism
5. Encapsulation
6. Abstraction
1. Access Modifiers in Java
Access modifiers define the visibility or accessibility of classes, methods, and variables. There
are four types of access modifiers in Java:
public: The class or member is accessible from anywhere.
private: The class or member is accessible only within the same class.
protected: The class or member is accessible within the same package and by
subclasses.
Default (no modifier): The class or member is accessible only within the same package.
Example:
public class Person {
private String name; // private variable
protected int age; // protected variable
public String city; // public variable
// Constructor
public Person(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}
// Public method
public void showInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
2. Static Keyword
The static keyword is used to indicate that a particular field or method belongs to the class itself
rather than to instances of the class.
Static Variable: Shared among all instances of a class.
Static Method: Can be called without creating an object of the class.
Example:
public class Counter {
static int count = 0; // Static variable
// Static method to increment the count
public static void incrementCount() {
count++;
public static void main(String[] args) {
Counter.incrementCount(); // Call static method without creating an object
System.out.println("Count: " + count); // Output: Count: 1
}
}
3. Inheritance
Inheritance is a fundamental object-oriented programming concept where a new class is
derived from an existing class. It allows the subclass to inherit properties and methods of the
superclass.
Example:
// Superclass (Parent class)
public class Animal {
void sound() {
System.out.println("Some animal sound");
// Subclass (Child class)
public class Dog extends Animal {
// Overriding the sound method
@Override
void sound() {
System.out.println("Bark");
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: Bark
4. Polymorphism
Polymorphism allows one method or class to take multiple forms. In Java, there are two types:
Compile-time polymorphism (Method Overloading)
Runtime polymorphism (Method Overriding)
Example of Method Overloading (Compile-time Polymorphism):
public class Calculator {
// Method to add two integers
public int add(int a, int b) {
return a + b;
// Overloaded method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(2, 3)); // Output: 5
System.out.println(calc.add(2, 3, 4)); // Output: 9
5. Encapsulation
Encapsulation is the technique of wrapping the data (variables) and code (methods) together as
a single unit. It is done by making variables private and providing public getter and setter
methods to access them.
Example:
public class Employee {
private String name; // Private variable
private int salary;
// Getter method
public String getName() {
return name;
// Setter method
public void setName(String name) {
this.name = name;
// Getter method
public int getSalary() {
return salary;
// Setter method
public void setSalary(int salary) {
if (salary > 0) {
this.salary = salary;
} else {
System.out.println("Salary must be positive!");
}
}
6. Abstraction
Abstraction is the concept of hiding the implementation details and exposing only the essential
features. This can be achieved using abstract classes and interfaces.
Abstract Class: A class that cannot be instantiated on its own and may contain abstract
methods (methods without a body).
Interface: A contract that classes must adhere to by implementing all methods declared
in the interface.
Example (Abstract Class):
abstract class Animal {
abstract void sound(); // Abstract method
void eat() { // Regular method
System.out.println("This animal eats food");
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: Bark
dog.eat(); // Output: This animal eats food
}
Example (Interface):
interface Shape {
double area(); // Abstract method
double perimeter(); // Abstract method
class Rectangle implements Shape {
double length, width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
@Override
public double area() {
return length * width;
@Override
public double perimeter() {
return 2 * (length + width);
}
public static void main(String[] args) {
Rectangle rect = new Rectangle(5, 10);
System.out.println("Area: " + rect.area()); // Output: Area: 50.0
System.out.println("Perimeter: " + rect.perimeter()); // Output: Perimeter: 30.0
If you'd like to explore any of these concepts in more detail or have more advanced topics to
dive into, let me know!