0% found this document useful (0 votes)
14 views5 pages

Software Assignment 1

The document discusses object-oriented programming concepts like inheritance, abstraction, polymorphism, overriding and overloading. It provides examples of inheritance where a subclass extends a superclass and overrides methods. It also differentiates between overriding and overloading by providing code examples.

Uploaded by

atatsalama
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)
14 views5 pages

Software Assignment 1

The document discusses object-oriented programming concepts like inheritance, abstraction, polymorphism, overriding and overloading. It provides examples of inheritance where a subclass extends a superclass and overrides methods. It also differentiates between overriding and overloading by providing code examples.

Uploaded by

atatsalama
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
You are on page 1/ 5

Mohamed

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

2.Which of the following statements are true?

a) A subclass is a subset of a superclass.

b) A subclass is usually extended to contain more func�ons and more detailed informa�on than its superclass.

c) "class A extends B" means A is a subclass of B.


d) "class A extends B" means B is a subclass of A.

3.Suppose you create a class Cylinder to be a subclass of

Circle. Analyze the following code:

class Cylinder extends Circle {

double length;

Cylinder (double radius) {

Circle(radius);

}}

a) The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not

specify the length of the cylinder.

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.

4.Which of the following class defini�ons defines a legal abstract class?

a) class A { abstract void unfinished() { } }

b) class A { abstract void unfinished(); }

c) abstract class A { abstract void unfinished(); }


d) public class abstract A { abstract void unfinished(); }
5.What is the output of running class C?

class A {

public A() {

System.out.println(

"The default constructor of A is invoked");

}}

class B extends A {

public B() {

System.out.println(

"The default constructor of B is invoked");

}}

public class C {

public sta�c void main(String[] args) {

B b = new B();

}}

a) Nothing displayed

b) "The default constructor of B is invoked"

c) "The default constructor of A is invoked""The default constructor of B is invoked"


d) "The default constructor of B is invoked""The default constructor of A is invoked"

e) "The default constructor of A is invoked"


6.What is the difference between func�on “overriding” and “overloading”. Explain clearly with simple code examples

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.

Here's an example to illustrate func�on overriding:

class Animal {

public void makeSound() {

System.out.println("Animal makes a sound");

}}

class Cat extends Animal {

@Override

public void makeSound() {

System.out.println("Cat meows");

}}

public class Main {

public sta�c void main(String[] args) {

Animal animal = new Animal();

animal.makeSound(); // Output: Animal makes a sound

Cat cat = new Cat();

cat.makeSound(); // Output: Cat meows

Animal animalCat = new Cat();

animalCat.makeSound(); // Output: 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.

Here's an example to illustrate func�on overloading:

class Calculator {

public int add(int a, int b) {

return a + b;

public double add(double a, double b) {

return a + b;

} }

public class Main {

public sta�c void main(String[] args) {

Calculator calculator = new Calculator();

int sum1 = calculator.add(5, 10);

System.out.println(sum1); // Output: 15

double sum2 = calculator.add(2.5, 3.5);

System.out.println(sum2); // Output: 6.0

}}

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?

a) class A { private A();}

b) class A { }

c) class A { protected A();}

d) final class A { }

You might also like