Software Assignment 1
Software Assignment 1
Nageeb/20196575
/CES Assignment ( 1 )
1.Object-oriented programming allows you to derive new classes from exis�ng classes. This is called ____________.
a) Encapsula�on
b) Inheritance
c) Abstrac�on
d) Generaliza�on
b) A subclass is usually extended to contain more func�ons and more detailed informa�on than its superclass.
double length;
Circle(radius);
}}
a) The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not
b) The program has a compile error because you atempted to invoke the Circle class's constructor
illegally.
c) The program compiles fine, but it has a run�me error because of invoking the Circle class's constructor
illegally.
class A {
public A() {
System.out.println(
}}
class B extends A {
public B() {
System.out.println(
}}
public class C {
B b = new B();
}}
a) Nothing displayed
Func�on overriding and func�on overloading are both concepts in object-oriented programming that involve the
declara�on of mul�ple methods with the same name. However, they differ in how they are used and how they are
resolved at run�me.
1. Func�on Overriding:
Func�on overriding occurs when a subclass provides a specific implementa�on of a method that is already defined in
its superclass. The subclass overrides the method to provide its own implementa�on, which may differ from the
implementa�on in the superclass. Overriding is a way to achieve run�me polymorphism.
class Animal {
}}
@Override
System.out.println("Cat meows");
}}
In this example, the `Animal` class has a method named `makeSound()`. The `Cat` class extends `Animal` and
overrides the `makeSound()` method with its own implementa�on. When the method is called on an instance of the
`Cat` class, the overridden method in the `Cat` class is invoked instead of the method in the `Animal` class. This
demonstrates func�on overriding.
2. Func�on Overloading:
Func�on overloading occurs when mul�ple methods with the same name are defined in the same class, but with
different parameters. These methods can have different return types, but the method name and the order or types
of parameters must be different. Overloading allows the same method name to be used for different behaviors or
opera�ons.
class Calculator {
return a + b;
return a + b;
} }
System.out.println(sum1); // Output: 15
}}
In this example, the `Calculator` class has two methods named `add()`. One method takes two integers as
parameters, and the other method takes two doubles as parameters. The methods have the same name but different
parameter types. When the `add()` method is called, the appropriate method is selected based on the arguments
passed to it. This is an example of func�on overloading.
To summarize:
- Func�on overriding occurs when a subclass provides a specific implementa�on of a method that is already defined
in its superclass.
- Func�on overloading occurs when mul�ple methods with the same name are defined in the same class, but with
different parameters.
Both func�on overriding and func�on overloading are important concepts in Java that allow for code reusability and
flexibility in designing classes and their behaviors.
7.Which of the following classes cannot be extended?
b) class A { }
d) final class A { }