class_inh_constr
class_inh_constr
### Class
A class is a blueprint or template that defines the properties (attributes) and
behaviors (methods) of objects. It encapsulates data and functions into a single
unit. A class can be thought of as an abstract concept that defines the
characteristics of a particular type of object.
**Key Points:**
- A class can contain fields (variables) and methods (functions) that operate on
the fields.
- It acts as a blueprint for creating objects.
- A class can also include constructors, which are special methods used to
instantiate objects.
**Example:**
```java
class Car {
// Attributes (fields)
String color;
String model;
int year;
// Method
void start() {
System.out.println("The car is starting.");
}
}
```
### Object
An object is an instance of a class. It is a concrete entity based on the template
provided by the class. An object represents a specific element that can hold data
and perform operations defined by the class it is instantiated from. Multiple
objects can be created from the same class, each with its unique attributes.
**Key Points:**
- An object is created using the `new` keyword followed by a call to the class's
constructor.
- Objects can access class methods and attributes to perform actions or retrieve
information.
- Each object has its own state and behavior, which can differ from other objects
created from the same class.
**Example:**
```java
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car();
// Setting attributes
myCar.color = "Red";
myCar.model = "Toyota";
myCar.year = 2020;
// Accessing methods
myCar.start(); // Output: The car is starting.
// Displaying attributes
System.out.println("Car Model: " + myCar.model);
System.out.println("Car Color: " + myCar.color);
System.out.println("Car Year: " + myCar.year);
}
}
```
### Summary:
- A **class** is a blueprint for creating objects, containing attributes and
methods.
- An **object** is an instance of a class, representing a specific entity with its
own state and behavior.
-------------------------------------------
Single Inheritance
In single inheritance, a class inherits from only one superclass.
Multi-Level Inheritance
In multi-level inheritance, a class acts as a base class for another class, which
serves as a base class for yet another class.
Here is an example:
// Single Inheritance
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
// Multi-Level Inheritance
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps");
}
}
Animal is the superclass, and Dog is the subclass that inherits from Animal. The
Dog class can use methods defined in the Animal class (like eat).
Multi-Level Inheritance :
Dog is the superclass for Puppy, which means Puppy inherits from Dog (and
indirectly from Animal). The Puppy class can use methods defined in both the Dog
class and the Animal class.
Running the Program:
When you run the InheritanceDemo class, it will demonstrate the functionality of
both single and multi-level inheritance by calling methods from their respective
classes.
---------------
Example:
Here is a simple example demonstrating both a default constructor and a
parameterized constructor:
class Car {
// Attributes
String model;
String color;
int year;
// Default Constructor
public Car() {
model = "Unknown";
color = "Unknown";
year = 0;
}
// Parameterized Constructor
public Car(String model, String color, int year) {
this.model = model; // 'this' keyword refers to the current object
this.color = color;
this.year = year;
}
// Method to display car details
public void displayDetails() {
System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
System.out.println("Car Year: " + year);
}
}