Java unit 2
Java unit 2
Hierarchical abstractions refer to the organization of classes in a parent-child hierarchy, where child
classes inherit properties and behaviors from parent classes.
Example:
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
A base class object is an instance of the parent class from which subclasses inherit.
Example:
class Animal {
void eat() {
System.out.println("Eating");
}
}
A subclass is a class that inherits from another class, whereas a subtype refers to a type derived from
another type, ensuring substitutability.
Example:
class Animal {
void eat() {
System.out.println("Eating");
}
}
Example:
class Vehicle {
void drive() {
System.out.println("Driving");
}
}
The `super` keyword is used to refer to the immediate parent class object.
Example:
class Animal {
void eat() {
System.out.println("Animal eating");
}
}
The `final` keyword prevents a class from being subclassed or a method from being overridden.
Example:
class Dog {
final void bark() {
System.out.println("Barking");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.bark(); // Barking
}
}
```
Polymorphism allows one interface to be used for a general class of actions. Method overriding is a form
of polymorphism.
Example:
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.makeSound(); // Bark
}
}
8. Abstract Classes
An abstract class cannot be instantiated and can contain abstract methods that must be implemented
by subclasses.
Example:
void sleep() {
System.out.println("Sleeping");
}
}
9. Packages
Example:
CLASSPATH is an environment variable that tells the Java compiler where to look for classes and
packages.
export CLASSPATH=.:/path/to/your/classes
import java.util.ArrayList;
Classes can have implementation details, whereas interfaces only declare methods without
implementing them (unless they are default or static methods).
class Animal {
void eat() {
System.out.println("Eating");
}
}
interface AnimalInterface {
void eat();
}
Example:
interface Animal {
void eat();
Example:
interface Animal {
void eat();
System.out.println("Dog eating");
}
You can create instances of classes that implement interfaces and use the interface type to refer to
them.
Example:
interface Animal {
void eat();
System.out.println("Dog eating");
Yes, but all variables in interfaces are implicitly `public`, `static`, and `final`.
Example:
interface Animal {
void eat();
Example:
interface Animal {
void eat();
void walk();
System.out.println("Dog eating");
}
System.out.println("Dog walking");
18. How do you use `java.io` package for input and output operations?
The `java.io` package provides classes for system input and output through data streams, serialization,
and the file system.
Example:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;