0% found this document useful (0 votes)
4 views

Java unit 2

The document covers key concepts of inheritance, packages, and interfaces in Java, including hierarchical abstractions, base class objects, subclasses, specialization, the use of the 'super' keyword, and the 'final' keyword. It also explains polymorphism, abstract classes, packages, CLASSPATH, and the differences between classes and interfaces. Additionally, it discusses defining and implementing interfaces, variables in interfaces, extending interfaces, and using the 'java.io' package for input and output operations.

Uploaded by

ariardy1396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java unit 2

The document covers key concepts of inheritance, packages, and interfaces in Java, including hierarchical abstractions, base class objects, subclasses, specialization, the use of the 'super' keyword, and the 'final' keyword. It also explains polymorphism, abstract classes, packages, CLASSPATH, and the differences between classes and interfaces. Additionally, it discusses defining and implementing interfaces, variables in interfaces, extending interfaces, and using the 'java.io' package for input and output operations.

Uploaded by

ariardy1396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

INHERITANCE, PACKAGES AND INTERFACES

1. What are hierarchical abstractions in Java?

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");
}
}

class Dog extends Animal {


void makeSound() {
System.out.println("Bark");
}
}

class Cat extends Animal {


void makeSound() {
System.out.println("Meow");
}
}

2. Base Class Object

2.: What is a base class object in Java?

A base class object is an instance of the parent class from which subclasses inherit.

Example:

class Animal {
void eat() {
System.out.println("Eating");
}
}

public class Main {


public static void main(String[] args) {
Animal animal = new Animal();
animal.eat(); // Eating
}
}
3. Subclass and Subtype

3. What is the difference between a subclass and a subtype?**

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");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Barking");
}
}

4. Forms of Inheritance: Specialization

4.What is specialization in inheritance?

Specialization is when a subclass extends a parent class by adding new features.

Example:

class Vehicle {
void drive() {
System.out.println("Driving");
}
}

class Car extends Vehicle {


void playMusic() {
System.out.println("Playing music");
}
}
```
5. Using `super`

5: How is the `super` keyword used in inheritance?

The `super` keyword is used to refer to the immediate parent class object.

Example:

class Animal {
void eat() {
System.out.println("Animal eating");
}
}

class Dog extends Animal {


void eat() {
super.eat(); // Calls the eat method of Animal class
System.out.println("Dog eating");
}
}

public class Main {


public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
}
}

6. `final` with Inheritance

6. What is the effect of using the `final` keyword in inheritance?

The `final` keyword prevents a class from being subclassed or a method from being overridden.

Example:

final class Animal {


void eat() {
System.out.println("Eating");
}
}
// This will cause an error because Animal is final
// class Dog extends Animal {
// }

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
}
}
```

7. Polymorphism and Method Overriding

7.What is polymorphism in Java?

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

8. What is an abstract class in Java?

An abstract class cannot be instantiated and can contain abstract methods that must be implemented
by subclasses.

Example:

abstract class Animal {


abstract void makeSound();

void sleep() {
System.out.println("Sleeping");
}
}

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
animal.sleep(); // Sleeping
}
}

9. Packages

9.What is a package in Java?

A package is a namespace that organizes classes and interfaces by functionality.

Example:

public class Main {


public static void main(String[] args) {
System.out.println("Hello from a package!");
}
}
// Compile: javac -d . com/example/Main.java
// Run: java com.example.Main

10. Understanding CLASSPATH

10. What is CLASSPATH in Java?

CLASSPATH is an environment variable that tells the Java compiler where to look for classes and
packages.

# Set the CLASSPATH variable

export CLASSPATH=.:/path/to/your/classes

11. Importing Packages

11. How do you import a package in Java?

You use the `import` keyword to include a package in your class.

import java.util.ArrayList;

public class Main {


public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Hello");
System.out.println(list.get(0)); // Hello
}
}

12. Differences between Classes and Interfaces

12.What is the difference between classes and interfaces in Java?

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();
}

class Dog implements AnimalInterface {


public void eat() {
System.out.println("Dog eating");
}
}
13. Defining an Interface

13. How do you define an interface in Java?

An interface is defined using the `interface` keyword.

Example:

interface Animal {

void eat();

14. Implementing an Interface

14. How do you implement an interface in Java?

A class implements an interface using the `implements` keyword.

Example:

interface Animal {

void eat();

class Dog implements Animal {

public void eat() {

System.out.println("Dog eating");
}

15. Applying Interfaces

15. How do you apply interfaces in Java?

You can create instances of classes that implement interfaces and use the interface type to refer to
them.

Example:

interface Animal {

void eat();

class Dog implements Animal {

public void eat() {

System.out.println("Dog eating");

public class Main {

public static void main(String[] args) {

Animal animal = new Dog();

animal.eat(); // Dog eating

16. Variables in Interface

16.Can interfaces have variables in Java?

Yes, but all variables in interfaces are implicitly `public`, `static`, and `final`.
Example:

interface Animal {

int legs = 4; // implicitly public, static, and final

void eat();

class Dog implements Animal {

public void eat() {

System.out.println("Dog eating with " + legs + " legs");

17. Extending Interfaces

17. Can interfaces extend other interfaces in Java?

Yes, interfaces can extend multiple interfaces.

Example:

interface Animal {

void eat();

interface Mammal extends Animal {

void walk();

class Dog implements Mammal {

public void eat() {

System.out.println("Dog eating");
}

public void walk() {

System.out.println("Dog walking");

18. Exploring `java.io`

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;

public class Main {


public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy