Oops Through Java Unit-2
Oops Through Java Unit-2
UNIT-2
1. Explain implementation of inheritance with an example program
Inheritance in Java:
In Java, inheritance is implemented using the extends keyword. A subclass inherits fields and methods
from a superclass, allowing for code reuse and extending functionality.
Example Program:
In this example, we'll create a Vehicle class as the parent class and a Car class as the child class that
inherits from Vehicle.
// Parent Class (Superclass)
class Vehicle {
// Fields (Attributes)
String make;
String model;
int year;
// Constructor
public Vehicle(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
1
// Constructor for Car class
public Car(String make, String model, int year, int doors) {
// Call the parent class constructor
super(make, model, year);
this.doors = doors;
}
// Constructor
public Animal(String name) {
this.name = name;
3
}
4
// Main Class
public class Main {
public static void main(String[] args) {
// Creating an object of Dog class
Dog dog = new Dog("Buddy");
The Mammal class inherits from Animal using extends Animal. This means Mammal inherits the name
field and the eat () method.
It has a constructor that calls the Animal constructor using super(name).
Additionally, the Mammal class introduces a sleep () method.
Grandchild Class (Dog):
The Dog class extends Mammal, which means it inherits both the fields and methods from Animal
(through Mammal).
The constructor of the Dog class calls the Mammal constructor using super(name), which further
invokes the Animal constructor.
The Dog class introduces a bark () method, which is specific to dogs.
Main Class:
An object dog of type Dog is created. This object can call methods from all the parent classes: eat ()
(from Animal), sleep () (from Mammal), and bark() (from Dog).
Output:
Buddy is eating.
5
Buddy is sleeping.
Buddy is barking.
3. Explain method overriding and usage of super keyword.
Method Overriding occurs when a subclass provides its specific implementation of a method that is
already defined in its superclass. The method in the subclass must have the same name, return type,
and parameter list as the method in the superclass.
Overriding allows the subclass to modify or extend the behavior of the superclass method, providing
a more specific implementation.
Usage of the super Keyword:
The super keyword in Java is used to refer to the immediate parent class of the current class. It has
several uses:
Accessing Parent Class Methods: You can use super to call a method of the parent class, even if it is
overridden in the child class.
Accessing Parent Class Constructor: You can use super () to call the parent class constructor.
Accessing Parent Class Fields: You can use super to refer to parent class fields, particularly if they are
hidden by fields in the subclass.
Example of super Keyword:
// Parent Class
class Animal {
// Method to be overridden
public void sound() {
System.out.println("Animal makes sound");
}
}
// Child Class
class Dog extends Animal {
// Overriding the sound() method
@Override
public void sound() {
super.sound(); // Calling the method of the parent class (Animal)
System.out.println("Dog barks");
}
}
6
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Calling the overridden method in Dog
}
}
Output:
Animal makes sound
Dog barks
4. Describe concept of interfaces.
An interface in Java is a reference type, similar to a class, that can contain only abstract methods
(methods without body) and constant variables (public, static, and final). An interface specifies a
contract or blueprint for the classes that implement it. In other words, it defines a set of behaviors
(methods) that the implementing classes must provide. Interfaces are used to represent a "what" an
object can do, without specifying how it does it.
Interfaces are crucial in Java for achieving abstraction and multiple inheritance.
Key Characteristics of Interfaces:
Abstract Methods: All methods in an interface are implicitly abstract (i.e., they don’t have a body).
From Java 8 onwards, interfaces can also have default methods with a body and static methods.
Constants: Variables declared in an interface are implicitly public, static, and final. They must be
initialized when declared.
Multiple Inheritance: A class can implement multiple interfaces, which allows Java to support
multiple inheritance of behavior (since a class can only extend one class but can implement multiple
interfaces).
No Constructor: Interfaces cannot have constructors, as they cannot be instantiated directly.
interface MyInterface {
// Abstract method
void myMethod();
Interfaces are used to define what a class can do, but not how it does it. They provide a way to achieve
abstraction and multiple inheritance of behavior in Java.
Example of Using an Interface:
// Defining an Interface
interface Animal {
// Abstract method
void sound();
// Default method
default void sleep() {
System.out.println("Animal is sleeping.");
}
}
9
default or static methods introduced in Java 8), and the implementing class must fill in the behavior of
those methods.
11
When you run a Java program or compile Java code, the JVM or the Java compiler looks through the
directories and JAR (Java ARchive) files specified in the class path to locate the necessary classes. If
a class is not found in the class path, the JVM will throw a ClassNotFoundException.
You can specify the class path directly when compiling or running Java programs by using the -cp or
-class path option.
Example:
javac -cp /path/to/classes:/path/to/lib/some-library.jar MyClass.java
java -cp /path/to/classes:/path/to/lib/some-library.jar MyClass
10. Describe concept of Access protection.
In Java, access protection refers to the mechanisms that control the visibility and accessibility of
classes, methods, variables, and constructors from other classes or packages. These mechanisms help
in implementing encapsulation, which is a core principle of object-oriented programming, by
controlling how and where the members of a class can be accessed.
Java provides four main access modifiers that define the scope of access to classes and their members
(fields, methods, and constructors). These access modifiers are:
public
protected
default (no modifier)
12
private
1. public Access Modifier:
Scope: The public access modifier allows the class, method, or variable to be accessible from any other
class (within any package).
Usage: It is used when you want to make a class, method, or field accessible universally.
Example:
public class Car {
public String model; // This field is accessible from any other class
public void display() { // This method is accessible from any other class
System.out.println("Car model: " + model);
}
}
2. protected Access Modifier:
Scope: The protected modifier allows access to the member within:
The same package.
Subclasses (even if the subclass is in a different package).
Usage: It is typically used when you want a method or field to be accessible by classes within the same
package or subclasses that extend the class, regardless of whether those subclasses are in the same
package.
Example:
class Animal {
protected String species; // Can be accessed by subclasses and other classes in the same
package
private void setName(String name) { // Can only be accessed within the Person class
this.name = name;
}
16