Question 1:
Inheritance is a core concept of Object-Oriented Programming (OOP) where one class (child) can use the properties and methods of another class (parent).
Types of Inheritance:
-
Single Inheritance – One child from one parent
-
Multilevel Inheritance – Chain of inheritance
-
Hierarchical Inheritance – Multiple children from one parent
-
Multiple Inheritance – One class inherits from multiple (using interfaces in Java)
-
Hybrid Inheritance – Combination of types above
Inheritance Hierarchy is a tree-like structure formed when multiple classes inherit from one or more base classes.
class Animal {
void speak() {
System.out.println("Animal speaks");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
}
}
An object is a real instance of a class. It has actual values and can use the class methods.
Quesion : 4
What is a Method?
A method is a block of code that performs a specific task.
For example, a method named greet() might just print “Hello”.
What is Method Overloading?
Method Overloading means having multiple methods with the same name but different parameters (either number or type).
class Demo {
void greet() {
System.out.println("Hello!");
}
void greet(String name) {
System.out.println("Hello, " + name);
}
}
Abstraction:
abstract class mobileUser{
abstract void sendMessage();
}
class Rahim extends mobileUser{
@Override
void sendMessage(){
System.out.println("Hello I am Rahim");
}
}
class Karim extends mobileUser{
@Override
void sendMessage(){
System.out.println("Hello I am Karim");
}
}
class Main {
public static void main(String[] args) {
mobileUser mu;
mu = new Rahim();
mu.sendMessage();
mu = new Karim();
mu.sendMessage();
}
}
0 Comments