0% found this document useful (0 votes)
4 views7 pages

Java Inheritance, Main Method & Control Structures

Inheritance in Java allows a child class to use properties and methods of a parent class, promoting code reuse and method overriding. The main() method serves as the entry point for Java programs, and without it, execution cannot begin. The Scanner class is used for taking input from the keyboard, while control structures manage the flow of execution through decision-making and looping statements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Java Inheritance, Main Method & Control Structures

Inheritance in Java allows a child class to use properties and methods of a parent class, promoting code reuse and method overriding. The main() method serves as the entry point for Java programs, and without it, execution cannot begin. The Scanner class is used for taking input from the keyboard, while control structures manage the flow of execution through decision-making and looping statements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q.1 What is an Inheritance in Java ?

Inheritance means one class (child) can use the properties and methods of another class (parent).
It helps in code reuse and method overriding.

=> Inheritance is implemented using the extends keyword.


=> Child class can modify the parent class method to provide its own implementation.

Advantages of Inheritance:
Code Reusability: Common code is written once in the parent class and reused by child classes.
Reduced Redundancy: No need to duplicate code across multiple classes.
Easy Maintenance: Changes in the parent class automatically reflect in all child classes.
Q.2 What is the main() method in Java?

=> The main method is the starting point of every Java program.
=> The JVM begins program execution from the main method.
=> Without it, Java does not know where to start the execution.
Syntax of main() method
public static void main(String[] args){

Explanation

1. public
• It is an access modifier.
• JVM must access this method from anywhere, so it must be public.

2. static
• Allows main() to run without creating an object of the class.
• JVM can call it directly.

3. void
• Means the method returns nothing.

4. main
• Name of the method. JVM looks for this exact method to start the program.

5. String[] args
• Used to take command-line inputs from the user.
• args is just a variable name; it can be anything (like a, data).

❌ What happens if there is no main method?


• The program will not run.
• JVM will throw an error:
Error: Main method not found in class ...

Because JVM needs a starting point, and without main(), execution cannot begin.
Q.3 What is Scanner class in Java ?
OR
How we can take the Input from the keyboard in Java ?

Answer:
You can take input from the keyboard in Java using the Scanner class. Scanner reads data like integers,
strings, and doubles from the user. It is present in the [Link] package.

Q.4 What are control structures in java ? [Impp for Long answer type Question]

=> Control structures in Java are statements that control the flow of execution in a program. They
decide which code runs, how many times it runs, and under what conditions.

1. Decision Making Statements (Conditional control statements):


I) if statement:
=> Used to execute a block of code only when a condition is true.
=> The if statement checks a boolean condition (true/false).
=> If the condition is true, the code inside the block [Link] the condition is false, the block is
skipped.
Syntax
if (condition) {
// Write code here
}
ii) if-else statement
=>Used when you want to run one block if condition is true, and another if condition is false.
Syntax
if (condition) {
// code if true
} else {
// code if false
}

iii) if-else ladder


=> Used when you have multiple conditions to check one after another.
Syntax
if (condition1) {
// code
} else if (condition2) {
// code
} else if (condition3) {
// code
} else {
// default code
}

[Link] Control (Repetition)

I) for loop :
=>The for loop is used when the number of iterations is known.
=> Executes the block repeatedly as long as the condition is true.

Syntax :
for(initialization; condition; increment/decrement) {

// code

=> Initialization runs only once at the start.


=> Increment/decrement updates the loop control variable after each iteration.
=> Can be used to traverse arrays.

ii) While Loop


=> The while loop is used when the number of iterations is not fixed.
Syntax:
while (condition) {
// code to execute
}

=> The condition is checked first; if true, the loop executes.


=> If the condition is false initially, the loop may not execute even once.

=> Can become infinite if the condition never becomes false

iii) do-while loop.

=> The do-while loop executes the code block at least once, even if the condition is false.

Syntax:

do {

// code to execute

} while (condition);

=> Condition is checked after executing the loop body.

=> Useful when you want at least one execution.

=> Can become infinite if the condition never becomes false.

Q.5 What is break and continue statement ?


OR
What are Jump statements in java ?

1. break
• Terminates the loop or switch statement immediately.

• Control moves outside the loop.

• Useful to exit early when a condition is met.

Example
for(int i=1; i<=5; i++) {

if(i==3){
break;
}
[Link](i);
}

// Output Will be : 1 2
2. continue

• Skips the current iteration of the loop.

• Control goes to the next iteration of the loop.

Example

for(int i=1; i<=5; i++) {

if(i==3) continue;

[Link](i);

// Output: 1 2 4 5

Q.6 What is the difference between While and do-while loop ? [IMP]

Q.7 What is switch statement in java ?

=> The switch statement is used for multi-way decision making.

=> It checks the value of a variable/expression and executes the matching case.

=> If no case matches, default case executes (optional).

=> It is an alternative to multiple if-else if statements.


Q.8 Why Java is platform Independent ? [[Link]]

OR
Why Java program is Platform independent ?

OR

Explain how Java achieves platform independence with an example.

Answer:

Java is platform independent because it does not compile programs directly into machine code. Instead, Java
programs are compiled into bytecode, which can run on any system having a Java Virtual Machine (JVM). The
JVM interprets the bytecode into the machine code of the underlying operating system, allowing the same
program to run on different platforms without modification.

=================================Happy Learning :) =================================

You might also like