class Computer {
Computer() {
System.out.println("Constructor of Computer class.");
}
void computer_method() {
System.out.println("Power gone! Shut down your PC soon...");
}
public static void main(String[] args) {
Computer my = new Computer();
Laptop your = new Laptop();
my.computer_method();
your.laptop_method();
}
}
class Laptop {
Laptop() {
System.out.println("Constructor of Laptop class.");
}
void laptop_method() {
System.out.println("99% Battery available.");
}
}
Step-by-Step Explanation:
This software is divided into two classes: Computer and Laptop. In this example, it explains how inheritance is implemented in Java through the Laptop class which is a subclass of Computer.
Class: Computer
class Computer {
Computer() {
System.out.println(“Constructor of Computer class.”);
}
void computer_method () {
System.out.println(“Power gone! Shut down your PC soon.”);
}
}
1. Constructor of Computer Class:
- The class constructor Computer() is declared. A class’s constructor is automatically invoked whenever a class instance or object is being created.
- The constructor prints the message “Constructor of Computer class.” whenever the object of the Computer class is created.
2. Method computer_method():
This is another typical instance method of the class Computer. On being invoked, it prints “Power gone! Shut down your PC soon.”
Class: Laptop (Sub-class of Computer)
class Laptop
{
Laptop()
{
System.out.println(“Constructor of Laptop class.”);
}
void laptop_method() {
System.out.println(“99% Battery available.”);
}
}
1. Constructor of Laptop Class:
The constructor Laptop() is called when an object of the Laptop class is created. It prints the message “Constructor of Laptop class.”.
2. Method laptop_method():
This method prints the message “99% Battery available.” when invoked. This method is defined in the Laptop class.
Main Method (Entry Point of Program)
public class Main {
public static void main(String[] args) {
Computer my = new Computer();
Laptop your = new Laptop();
my.computer_method();
your.laptop_method();
}
1. Defining Object for Computer Class:
- The line Computer my = new Computer(); declares an instance of the class Computer.
- When new Computer() is invoked, the constructor of the Computer class is invoked, printing:
Constructor of Computer class.
2. Instantiating Laptop Class:
Laptop your = new Laptop(); instantiates the Laptop class.
When new Laptop() is invoked, it calls the Laptop class’s constructor, printing the following output:
Constructor of Laptop class.
Displayed on a console
3. Invocation of computer_method() on Computer Object
my.computer_method(); invokes computer_method() of Computer class from my object
This program prints
Power gone! Shutdown your PC now.
4. Calling laptop_method() on Laptop Object:
your.laptop_method(); calls the laptop_method() of the Laptop class using the your object.
The method laptop_method() prints:
99% Battery available.
Final Output:
Power gone! Shut down your PC soon.
99% Battery available.
Conclusion:
1. Computer Class:
- The constructor is used to print “Constructor of Computer class.” while creating a Computer object.
- The computer_method() is used to print “Power gone! Shut down your PC soon.”.
2. Laptop Class:
- The constructor is used to print “Constructor of Laptop class.” while creating a Laptop object.
- The laptop_method() is utilized to print “99% Battery available.”
3. Main Method:
- It creates instances of both Computer and Laptop classes.
- It calls computer_method() and laptop_method() on the respective instances for printing relevant messages.