001.
CHAPTER
01
OOPs Concepts
Explain method overloading and method overriding.
Answer:
Method overloading occurs when there
are two or more methods
in a class with the same name but with different number/type of
arguments. The following code demonstrates this:
public class Calculator {
public int add (int a, int b) {
return a+b;
}
public double add (double a, double b) {
return a+b;
}
public int add (int a) {
return a+a;
}
}
Method overriding occurs when there is a method in a sub–class
that has the same name and number of arguments as a super–
class method. The following code demonstrates this:
public class Animal {
public void saySomething () {
System.out.println(“I am an animal”);
002.
a.
b.
c.
d.
e.
003.
}
}
public class Dog extends Animal {
public void saySomething () {
System.out.println(“I am a dog”);
}
}
Explain the benefits of OOPs.
Answer:
Following are the benefits of OOPs:
Reusability: OOPs
principles like Inheritance,
Composition
and polymorphism help
in reusing existing code
Extensibility: Code written
using OOPs principles like
Inheritance
makes code extensible
Do'stlaringiz bilan baham: