Due Date: April 5, 2024
Assignment Learn about java inheritence
Java Inheritance Explanation
Please read all of this.
Basic Concepts
- **Superclass (Parent Class):** The class whose features are inherited is known as the superclass
or parent class.
- **Subclass (Child Class):** The class that inherits the features from another class is known as the
subclass or child class. A subclass can add its own fields and methods in addition to the superclass
fields and methods.
- **`extends` Keyword:** In Java, inheritance is achieved using the `extends` keyword. A class that
inherits from another class uses `extends` to specify the superclass from which it is inheriting.
- **Method Overriding:** Subclasses can provide specific implementations for methods they
inherited from the superclass. This is known as overriding.
- **`super` Keyword:** The `super` keyword is a reference variable that is used to refer parent class
objects. It can be used to invoke superclass methods and to access the superclass constructor.
- **Inheritance Hierarchy:** Java supports single inheritance, meaning that a class can only extend
one other class, leading to a single inheritance hierarchy. However, a class can implement multiple
interfaces, allowing for a form of multiple inheritance.
Example of Inheritance in Java
Let's illustrate inheritance with a simple example. We'll create a superclass named `Animal` and a
subclass named `Dog` that inherits from `Animal`.
Your file names will be:
PX_Animals_lastname.java (Java program)
PX_Animals_lastname.png (Screen print of your program in the Eclipse IDE)
PX_Dog_lastname.java (Java program)
PX_Dog_lastname.png (Screen print of your program in the Eclipse IDE)
PX_AnimalDriver_lastname.java (Java program)
PX_AnimalDriver_lastname.png (Screen print of your program in the Eclipse IDE)
PX_AnimalDriver_lastname.mp4 (Video of AnimalDriver)
PX_AnimalDriver_Board_lastname.png (Explain on the Board.)
Be sure to drop off your files into google classroom.
Code is below.
Be sure to indent your code.
This will 3 different classes.
#### Superclass: Animal
```java
public class Animal
{
Java Inheritance Explanation
String type = "Animal";
public void eat() {
System.out.println("This animal eats food.");
}
}
```
#### Subclass: Dog
```java
public class Dog extends Animal { // Dog inherits from Animal
public void displayType() {
System.out.println("The type is: " + type);
}
@Override
public void eat() {
System.out.println("This dog eats dog food.");
}
}
```
#### Main Class
Java Inheritance Explanation
```java
public class AnimalDriver {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.displayType(); // Displays: The type is: Animal
myDog.eat(); // Displays: This dog eats dog food.
}
}
```
Key Points in the Example
- **Inheritance:** The `Dog` class inherits from the `Animal` class using the `extends` keyword.
- **Access to Superclass Fields:** The `Dog` class can access the `type` field from `Animal`.
- **Method Overriding:** The `Dog` class provides its own implementation of the `eat()` method,
overriding the implementation provided by `Animal`.
- **Code Reusability:** The `Dog` class reuses the fields and methods of the `Animal` class,
demonstrating the code reusability aspect of inheritance.
Inheritance is a powerful feature of Java and object-oriented programming that helps in organizing
and structuring code in a clear, hierarchical manner.
KP Java Programming – Course Outline
Section 1 - Getting Started
Java Basics
Data Types
Arrays and References
Operators and Constructs
Java Objects
Dynamic Memory Allocation
Java Methods
Java Strings
Section 2 - Classes and Objects
Class Design
Fields and Access Control
Constructors
Method Overloading
Static Methods
Inheritance
Method Overriding
Using
final
and
super
Abstract Classes and Methods
Dynamic Binding
Polymorphism
Section 3 - Working with Classes
Using
instanceof
Interfaces
Exception Handling
Exception Objects
throw points, throws clause
try, catch, finally
Section 4 - User Interfaces
Window Applications
Layout Managers
Event Handlers and Listeners
Anonymous Classes and Lambdas
Java Swing APIs
Basic GUI Controls
Menus and MenuBars
Section 5 - Generics and Collections
Why Use Generics?
Generic Classes and Interfaces
Generic Iterators
Collections
ArrayList, LinkedList, HashMap
Section 6 - Threads
Thread States
Extending the
Thread
class
Timer Thread
Implementing the
Runnable
interface
Section 7 - File I/O
Input and Output Streams
Binary and Text Files
Files and Directory Methods
Appendix
Java Language
JavaFX