DAY3 Notes
DAY3 Notes
1. Polymorphism, 2. Abstraction
QUESTIONS(Theory)
Polymorphism in Java is a core concept of object-oriented programming (OOP) that allows objects to
be treated as instances of their parent class. It facilitates flexibility and the ability to define methods
in multiple forms. Polymorphism is primarily achieved through method overriding and method
overloading.
In Compile time Polymorphism, the call is In Run time Polymorphism, the call is not
resolved by the compiler. resolved by the compiler.
It is also known as Static binding, Early It is also known as Dynamic binding, Late
binding and overloading as well. binding and overriding as well.
It provides fast execution because the It provides slow execution as compare to early
method that needs to be executed is binding because the method that needs to be
known early at the compile time. executed is known at the runtime.
Compile time polymorphism is less flexible Run time polymorphism is more flexible as all
as all things execute at compile time. things execute at run time.
Abstraction refers to hiding the implementation details of a code and exposing only the necessary
information to the user.
Abstract classes allow you to define some default behavior while forcing subclasses to implement
specific methods. Interfaces focus purely on behavior, providing no concrete methods.
5. What is mean by abstract method?
Abstract classes and methods provide a way to define a blueprint or template for a class without
providing a complete implementation.
You can't create an object of an abstract class type. However, you can use pointers and references to
abstract class types. You create an abstract class by declaring at least one pure virtual member
function.
An interface can declare static methods, which are invoked without reference to a particular object.
Static interface methods are distinct from default methods, abstract interface methods, and non-
static private interface methods, all of which are instance methods.
No, we can not declare interface as final . Interface in Java is similar to a class but it contains only
abstract methods and fields, which are final and static . Since all the methods are abstract ;
therefore, we cannot instantiate interface.
9. How will achieve multiple inheritance in java, write a code for that?
interface Parent1
void method1();
interface Parent2
void method2();
In this example, we have two interfaces, Parent1 and Parent2 , each with a method. The Child class
implements both interfaces, effectively achieving multiple inheritance.