Classes and Objects
A class in Java is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into a single unit.
public class Car {
String color;
String model;
int year;
void displayInfo() {
System.out.println("Model: " + model + ", Color: " + color + ", Year: " + year);
}
}
An object is an instance of a class. It has state and behavior as defined by its class.
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // myCar is an object of Car
myCar.color = "Red";
myCar.model = "Toyota";
myCar.year = 2020;
myCar.displayInfo(); // Output: Model: Toyota, Color: Red, Year: 2020
}
}
Inheritance
Inheritance allows one class to inherit the properties and methods of another class.
Single Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
}
}
Multilevel Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("This mammal walks.");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.walk();
dog.bark();
}
}
Hierarchical Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("The cat meows.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
Cat cat = new Cat();
cat.eat();
cat.meow();
}
}
Multiple Inheritance (via Interfaces)
interface CanFly {
void fly();
}
interface CanSwim {
void swim();
}
class Duck implements CanFly, CanSwim {
public void fly() {
System.out.println("The duck flies.");
}
public void swim() {
System.out.println("The duck swims.");
}
}
public class Main {
public static void main(String[] args) {
Duck duck = new Duck();
duck.fly();
duck.swim();
}
}
Method Overloading and Overriding
Method Overloading
Method overloading allows a class to have more than one method with the same name, but different parameters (different type or number of parameters).
class MathOperations {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println(math.add(5, 3)); // Output: 8
System.out.println(math.add(5, 3, 2)); // Output: 10
System.out.println(math.add(5.5, 3.2)); // Output: 8.7
}
}
Method Overriding
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
class Animal {
void sound() {
System.out.println("This animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: The dog barks.
}
}
Constructors
A constructor is a special method that is called when an object is instantiated. It has the same name as the class and does not have a return type.
class Car {
String color;
String model;
Car(String color, String model) {
this.color = color;
this.model = model;
}
void displayInfo() {
System.out.println("Model: " + model + ", Color: " + color);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Red", "Toyota");
myCar.displayInfo(); // Output: Model: Toyota, Color: Red
}
}
Access Modifiers
Access modifiers determine the visibility of a class, constructor, method, or field. Java has four access modifiers:
- public: The member is accessible from any other class.
- private: The member is accessible only within its own class.
- protected: The member is accessible within its own package and by subclasses.
- default (no modifier): The member is accessible only within its own package.
class Person {
public String name;
private int age;
protected String address;
String phoneNumber; // default access
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.name = "John"; // public access
person.setAge(25); // public method
// person.age = 25; // private access, not allowed
person.address = "123 Street"; // protected access
person.phoneNumber = "555-1234"; // default access
}
}
Superclass
A superclass is a class from which other classes inherit properties and methods.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method from Animal class
dog.bark(); // Method from Dog class
}
}
Interface
An interface is a reference type in Java that can contain only constants, method signatures, default methods, static methods, and nested types. It cannot contain instance fields or constructors. Classes that implement interfaces must provide implementations for all abstract methods in the interface.
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: The dog barks.
}
}
Abstract Class
An abstract class is a class that cannot be instantiated on its own and is meant to be subclassed. It can have abstract methods (without implementation) and concrete methods (with implementation).
abstract class Animal {
abstract void sound();
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
dog.eat();
}
}
Differences Between Interface and Abstract Class
Feature | Interface | Abstract Class |
---|---|---|
Implementation | Cannot provide any implementation | Can provide partial or full implementation |
Multiple Inheritance | Can implement multiple interfaces | Can extend only one class |
Default Methods | Can have default and static methods | Cannot have default methods (until Java 8) |
Access Modifiers | Methods are implicitly public | Can have different access modifiers |
Constructors | Cannot have constructors | Can have constructors |