0% found this document useful (0 votes)
16 views

OOPs Assignment

Uploaded by

Ayush Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

OOPs Assignment

Uploaded by

Ayush Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

OOPs Assignment

Ques 1.What is Inheritance JAVA ?


ANS - Inheritance in Java is a mechanism in which one object
acquires all the properties and behaviors of a parent object

Ques 2.What is SuperClass And SubClass ?


ANS – Superclass – A superclass is the class from which many
subclasses can be created. The subclasses inherit the characteristics of
a superclass. The superclass is also known as the parent class or base
class.

Subclass - A subclass is a class derived from the


superclass. It inherits the properties of the superclass and also contains
attributes of its own

Ques 3.How is Inheritance implemented/achieved in


java
ANS - Inheritance in Java is implemented using
the extends keyword. It is a mechanism in which one class inherits the
properties and methods of another class. The class that inherits is
called the subclass or derived class, and the class from which it inherits
is called the superclass or base class.
Example - class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {


@Override
public void eat() {
System.out.println("Dog is eating");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
animal.eat(); // Prints "Animal is eating"

Dog dog = new Dog();


dog.eat(); // Prints "Dog is eating"
}
}

Ques 4.What is Polymorphism ?


ANS - Polymorphism is an object-oriented programming concept that
refers to the ability of a variable, function, or object to take
on multiple forms. In a programming language exhibiting polymorphism,
class objects belonging to the same hierarchical tree (inherited from a
common parent class) may have functions with the same name, but with
different behaviors.

Ques 5.Differentiate between method overloading and


overriding .
ANS -
No. Method Overloading Method Overriding

1) Method overloading is Method overriding is


used to increase the used to provide the
specific
readability of the implementation of the
program. method that is already
provided by its super
class.

2) Method overloading is Method overriding


performed within class. occurs in two
classes that have IS-A
(inheritance)
relationship.

3) In case of method In case of method


overloading, parameter overriding, parameter
must be different. must be same.

4) Method overloading is Method overriding is


the example of compile the example of run
time polymorphism. time polymorphism.

5) In java, method Return type must be


overloading can't be same or covariant in
performed by changing method overriding.
return type of the
method only. Return
type can be same or
different in method
overloading. But you
must have to change the
parameter.

Ques 6.What is Abstraction explained with an Example ?


ANS - Abstraction in Java is the process in which we only show
essential details/functionality to the user. The non-essential
implementation details are not displayed to the user.
Simple Example to understand Abstraction:
Television remote control is an excellent example of abstraction. It
simplifies the interaction with a TV by hiding the complexity behind simple
buttons and symbols, making it easy without needing to understand the
technical details of how the TV functions.
// Abstract Class declared
abstract class Animal {
private String name;

public Animal(String name) { this.name = name; }

public abstract void makeSound();

public String getName() { return name; }


}

// Abstracted class
class Dog extends Animal {
public Dog(String name) { super(name); }

public void makeSound()


{
System.out.println(getName() + " barks");
}
}

// Abstracted class
class Cat extends Animal {
public Cat(String name) { super(name); }

public void makeSound()


{
System.out.println(getName() + " meows");
}
}

// Driver Class
public class AbstractionExample {
// Main Function
public static void main(String[] args)
{
Animal myDog = new Dog("Buddy");
Animal myCat = new Cat("Fluffy");

myDog.makeSound();
myCat.makeSound();
}
}
Ques 7. What is the Difference between an abstract
method and final method in java ? Explain with an
example
ANS - Final Class: A class which is declared with the “Final” keyword
is known as the final class. The final keyword is used to finalize the
implementations of the classes, the methods and the variables used in
this class. The main purpose of using a final class is to prevent the
class from being inherited (i.e.) if a class is marked as final, then no
other class can inherit any properties or methods from the final class. If
the final class is extended, Java gives a compile-time error. The
following is an example of how a final class is declared. However, a
compile-time error is given because this final class is being inherited.
// Final class
final class Super {
private int data = 100;
}
public class Sub extends Super {
public static void main(String args[])
{
}
}
Abstract Class: A class that is declared using the “abstract” keyword is
known as an abstract class. The main idea behind an abstract class is
to implement the concept of Abstraction. An abstract class can have
both abstract methods(methods without body) as well as the concrete
methods(regular methods with the body). However, a normal class(non-
abstract class) cannot have abstract methods. The following is an
example of how an abstract class is declared.
// an abstract class

// Abstract parent class


abstract class Book {

// Abstract method without body


public abstract void page();
}

// shayar class extends Book class


public class shayar extends Book {
// Declaring the abstract method
public void page()
{
System.out.println("Geek");
}

// Driver code
public static void main(String args[])
{
Book obj = new shayar();
obj.page();
}
}

Ques 8.What is the final class in java?


ANS - If you make any class as final, you cannot extend it.
1. final class Bike{}
2.
3. class Honda1 extends Bike{
4. void run(){System.out.println("running safely with 100kmph");}
5.
6. public static void main(String args[]){
7. Honda1 honda= new Honda1();
8. honda.run();
9. }
10. }
Ques 9.Differentiate between abstraction and
encapsulation.
ANS –
Abstraction Encapsulation

Abstraction is a feature of OOPs Encapsulation is also a feature of OOPs. It hides the code
that hides and data into a single entity or unit so that the data can
the unnecessary detail but be protected from the outside world.
shows the essential information.

It solves an issue at Encapsulation solves an issue at implementation level.


the design level.

It focuses on It focuses on internal working.


the external lookout.

It can be implemented It can be implemented by using the access


using abstract modifiers (private, public, protected).
classes and interfaces.

It is the process It is the process of containing the information.


of gaining information.

In abstraction, we use abstract We use the getters and setters methods to hide the
classes and interfaces to hide data.
the code complexities.

The objects The object need not to abstract that result in


are encapsulated that helps to encapsulation.
perform abstraction.
Ques 10.Difference between Runtime and Compile time
polymorphism with an example.
ANS - Compile Time Polymorphism: Whenever an object is bound
with its functionality at the compile time, this is known as the compile-
time polymorphism. At compile-time, java knows which method to call
by checking the method signatures. So this is called compile-time
polymorphism or static or early binding. Compile-time polymorphism is
achieved through method overloading. Method Overloading says you
can have more than one function with the same name in one class
having a different prototype. Function overloading is one of the ways to
achieve polymorphism but it depends on technology and which type of
polymorphism we adopt. In java, we achieve function overloading at
compile-Time. The following is an example where compile-time
polymorphism can be observed.
public class GFG {

// First addition function


public static int add(int a, int b)
{
return a + b;
}

// Second addition function


public static double add(
double a, double b)
{
return a + b;
}

// Driver code
public static void main(String args[])
{
// Here, the first addition
// function is called
System.out.println(add(2, 3));

// Here, the second addition


// function is called
System.out.println(add(2.0, 3.0));
}
}

Run-Time Polymorphism: Whenever an object is bound with the


functionality at run time, this is known as runtime polymorphism. The
runtime polymorphism can be achieved by method overriding. Java
virtual machine determines the proper method to call at the runtime,
not at the compile time. It is also called dynamic or late binding.
Method overriding says the child class has the same method as
declared in the parent class. It means if the child class provides the
specific implementation of the method that has been provided by one
of its parent classes then it is known as method overriding. The
following is an example where runtime polymorphism can be observed.
// Implementing a class
class Test {

// Implementing a method
public void method()
{
System.out.println("Method 1");
}
}

// Defining a child class


public class GFG extends Test {

// Overriding the parent method


public void method()
{
System.out.println("Method 2");
}

// Driver code
public static void main(String args[])
{
Test test = new GFG();

test.method();
}
}
Compile Time Polymorphism Run time Polymorphism

In Compile time Polymorphism, the In Run time Polymorphism, the call


call is resolved by the compiler. is not resolved by the compiler.

It is also known as Static binding,


It is also known as Dynamic binding,
Early binding and overloading as
Late binding and overriding as well.
well.

Method overloading is the compile- Method overriding is the runtime


time polymorphism where more than polymorphism having the same
one methods share the same name method with same parameters or
with different parameters or signature but associated
signature and different return type. withcompared, different classes.

It is achieved by function overloading It is achieved by virtual functions


and operator overloading. and pointers.

It provides fast execution because It provides slow execution as


the method that needs to be compare to early binding because
executed is known early at the the method that needs to be
compile time. executed is known at the runtime.

Compile time polymorphism is less Run time polymorphism is more


flexible as all things execute at flexible as all things execute at run
compile time. time.

Inheritance is not involved. Inheritance is involved.

You might also like