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

class_inh_constr

In Java, a class serves as a blueprint defining the properties and behaviors of objects, while an object is an instance of a class with its own unique state and behavior. The document also explains single and multi-level inheritance, where a class can inherit from one or more classes, and discusses constructors, which are special methods used to initialize objects. Examples illustrate the concepts of classes, objects, inheritance, and constructors in Java programming.

Uploaded by

nagabhushansv2
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

class_inh_constr

In Java, a class serves as a blueprint defining the properties and behaviors of objects, while an object is an instance of a class with its own unique state and behavior. The document also explains single and multi-level inheritance, where a class can inherit from one or more classes, and discusses constructors, which are special methods used to initialize objects. Examples illustrate the concepts of classes, objects, inheritance, and constructors in Java programming.

Uploaded by

nagabhushansv2
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

In Java, classes and objects are fundamental concepts of object-oriented

programming (OOP). Here's a brief definition and explanation for each:

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

class Dog extends Animal {


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

// Multi-Level Inheritance
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps");
}
}

public class InheritanceDemo {


public static void main(String[] args) {
// Demonstrating single inheritance
Dog dog = new Dog();
dog.eat(); // From Animal class
dog.bark(); // From Dog class

// Demonstrating multi-level inheritance


Puppy puppy = new Puppy();
puppy.eat(); // From Animal class
puppy.bark(); // From Dog class
puppy.weep(); // From Puppy class
}
}
Explanation:
Single Inheritance :

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.
---------------

In Java, a constructor is a special method used to initialize objects. It is called


when an object of a class is created and has the same name as the class.
Constructors can take parameters, allowing for different ways to initialize objects
based on the values provided.

Key Characteristics of Constructors:


Name : A constructor has the same name as the class.
No Return Type : Constructors do not have a return type, not even void.
Initialization : They are primarily used to initialize object attributes.
Called Automatically : A constructor is called automatically when an object is
instantiated.
Types of Constructors:
There are two main types of constructors in Java:

Default Constructor : A constructor that does not take any parameters. If no


constructors are defined in a class, Java provides a default constructor
automatically.

Parameterized Constructor : A constructor that takes parameters, allowing you to


initialize an object with specific values at the time of creation.

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

public class ConstructorDemo {


public static void main(String[] args) {
// Creating an object using the default constructor
Car car1 = new Car();
car1.displayDetails(); // Output defaults

// Creating an object using the parameterized constructor


Car car2 = new Car("Toyota Camry", "Red", 2021);
car2.displayDetails(); // Output car details with provided values
}
}
Explanation of the Code:
Default Constructor : The Car class has a default constructor that initializes all
attributes to default values.
Parameterized Constructor : There is also a parameterized constructor that allows
initialization of the attributes with specific values when creating a Car object.
In the main method, two objects are created: one using the default constructor and
the other using the parameterized constructor.
Output:
When you run the program, the output will look like this:

Car Model: Unknown


Car Color: Unknown
Car Year: 0
Car Model: Toyota Camry
Car Color: Red
Car Year: 2021
This demonstrates how constructors are used for initializing an object's attributes
at the moment it is created.

You might also like