Inheritance allows one class (child class) to use fields and methods from another class (parent class). For example, a `Dog` class can inherit properties and behaviors from a general `Animal` class.
// Parent Class class Animal { // Animal class members } // Child Class class Dog extends Animal { // Dog inherits traits from Animal // additional Dog class members }
In Java, the `extends` keyword is used to create a child class from a parent class. This means the child class gets all features from the parent class.
// Parent Class class Shape { public static void main(String[] args) { Square sq = new Square(); } } // Child Class class Square extends Shape { // Square inherits from Shape }
The `super()` method allows a child class to call the constructor of its parent class. This helps in initializing inherited properties.
// Parent class class Animal { String sound; Animal(String snd) { this.sound = snd; } } // Child class class Dog extends Animal { Dog() { super("woof"); } }
The `protected` keyword allows a child class to access properties or methods from its parent class, while the `final` keyword makes methods or properties immutable (unchangeable).
class Student { protected double gpa; // gpa can be accessed by subclasses final protected boolean isStudent() { return true; } // isStudent() cannot be overridden by subclasses }
Polymorphism allows different classes to be treated as instances of the same class through a common interface. This is useful for creating flexible and reusable code.
// Parent class class Animal { public void greet() { System.out.println("The animal greets you."); } } // Child class class Cat extends Animal { public void greet() { System.out.println("The cat meows."); } } class MainClass { public static void main(String[] args) { Animal myAnimal = new Animal(); Animal myCat = new Cat(); myAnimal.greet(); // Prints: The animal greets you. myCat.greet(); // Prints: The cat meows. } }
Method overriding allows a child class to provide a specific implementation for a method already defined in its parent class.
// Parent class class Animal { public void eat() { System.out.println("The animal is eating."); } } // Child class class Dog extends Animal { @Override public void eat() { System.out.println("The dog is eating."); } }
You can store instances of different child classes in an array or ArrayList of their parent class type. This is useful for managing collections of related objects.
// Creating instances of different animals Animal cat = new Cat(); Animal dog = new Dog(); Animal pig = new Pig(); // Storing them in an array Animal[] animals = {cat, dog, pig}; // Iterating over the array and calling a method for (Animal animal : animals) { animal.sound(); }
In Java, all classes are ultimately subclasses of the `Object` class. This means they inherit methods from `Object`, such as `equals()` for comparing objects.
// Comparing two different objects String a = new String("Dracaena"); String b = new String("Haworthia"); System.out.println(a.equals(b)); // Prints: false // Comparing two identical objects String c = new String("Haworthia"); System.out.println(b.equals(c)); // Prints: true
The `equals()` method is used to compare two objects to see if they are equivalent in terms of their content. It is different from `==`, which checks if two references point to the same object.
String str1 = new String("hello"); String str2 = new String("hello"); System.out.println(str1.equals(str2)); // Prints: true
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!