0% found this document useful (0 votes)
70 views5 pages

Assignment 1

Uploaded by

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

Assignment 1

Uploaded by

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

*Objective:** Create a base class `Animal` with a method `makeSound()`.

Then
create derived classes `Dog` and `Cat` that inherit from `Animal`. Override the
`makeSound()` method in each derived class to print the appropriate sound for
each animal. - **Steps:** 1. Create the `Animal` class with a `makeSound()`
method. 2. Create the `Dog` and `Cat` classes that extend `Animal`. 3. Override
the `makeSound()` method in each subclass to print "Woof!" for `Dog` and
"Meow!" for `Cat`. 4. Create a main method to instantiate objects of `Dog` and
`Cat` and call their `makeSound()` methods.
package Assignments;

public class Basic_Inheritance {

public static void main(String[] args) {


// TODO Auto-generated method stub
class Animal {
void makeSound() {
System.out.println("Some generic animal sound");
}
}

class Dog extends Animal {


@Override
void makeSound() {
System.out.println("Woof!");
}
}

class Cat extends Animal {


@Override
void makeSound() {
System.out.println("Meow!");
}
}
class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();

myDog.makeSound(); // Output: Woof!


myCat.makeSound(); // Output: Meow!
}
}

### 2. **Inheritance with Constructors:** - **Objective:** Demonstrate the use


of constructors in inheritance. - **Steps:** 1. Create a class `Person` with fields
for `name` and `age`. Add a constructor to initialize these fields. 2. Create a
subclass `Student` that adds a field for `studentId` and a constructor to initialize
all three fields (name, age, studentId). 3. In the main method, create an instance
of `Student` and print out the details of the student using a method
`displayInfo()`.

package Assignments;

class Person {
String name;
int age;

// Constructor to initialize name and age


Person(String name, int age) {
this.name = name;
this.age = age;
}
}
class Student extends Person {
String studentId;

// Constructor to initialize name, age, and studentId


Student(String name, int age, String studentId) {
// Call the constructor of the superclass (Person)
super(name, age);
this.studentId = studentId;
}

// Method to display student information


void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Student ID: " + studentId);
}
}

package Assignments;

public class main {


public static void main(String[] args) {
// Create a Student object
Student student = new Student("Alice", 20, "S12345");

// Display the student's information


student.displayInfo();
}
}
### 3. **Method Overriding and Super Keyword:** - **Objective:** Learn about
method overriding and the use of the `super` keyword. - **Steps:** 1. Create a
base class `Vehicle` with a method `startEngine()` that prints "Vehicle engine
started." 2. Create a subclass `Car` that overrides `startEngine()` to print "Car
engine started." 3. In the `Car` class, call the `startEngine()` method of the
`Vehicle` class using the `super` keyword. 4. In the main method, create an
instance of `Car` and call its `startEngine()` method.
package Assignments;

public class Vehicle {


// Method to start the engine
public void startEngine() {
System.out.println("Vehicle engine started.");
}
}

package Assignments;

public class Car extends Vehicle {


// Override the startEngine method
@Override
public void startEngine() {
// Call the startEngine method of the Vehicle class using super
super.startEngine();
// Additional behavior for Car
System.out.println("Car engine started.");
}
}

package Assignments;

public class main3 {


public static void main(String[] args) {
// Create a Car object
Car myCar = new Car();
// Call the startEngine method
myCar.startEngine(); // Output: Vehicle engine started.
// Car engine started.
}
}

You might also like