You are on page 1of 14

S.

NO Inheritance Polymorphism

Inheritance is one in which a new


class is created (derived class) that Whereas polymorphism is that which
1.
inherits the features from the can be defined in multiple forms.
already existing class(Base class).

Whereas it is basically applied to


2. It is basically applied to classes.
functions or methods.

Polymorphism allows the object to


Inheritance supports the concept of decide which form of the function to
3. reusability and reduces code length implement at compile-time
in object-oriented programming. (overloading) as well as run-time
(overriding).

Inheritance can be single, hybrid, Whereas it can be compiled-time


4. multiple, hierarchical and polymorphism (overload) as well as run-
multilevel inheritance. time polymorphism (overriding).

While it is also used in pattern


5. It is used in pattern designing.
designing.

Example : Example :
The class bike can be inherit from The class bike can have method name
6. the class of two-wheel vehicles, set_color(), which changes the bike’s
which is turn could be a subclass of color based on the name of color you
vehicles. have entered.

Single Inheritance
In single inheritance, subclasses inherit the features of one superclass. In the
image below, class A serves as a base class for the derived class B.

Single inheritance

Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class, and as
well as the derived class also acts as the base class for other classes. In the
below image, class A serves as a base class for the derived class B, which in
turn serves as a base class for the derived class C. In Java, a class cannot
directly access the grandparent’s members .

3. Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for
more than one subclass. In the below image, class A serves as a base class for
the derived classes B, C, and D.
4. Multiple Inheritance (Through Interfaces)
In Multiple inheritances , one class can have more than one superclass and
inherit features from all parent classes. Please note that Java
does not support multiple inheritances with classes. In Java, we can achieve
multiple inheritances only through Interfaces. In the image below, Class C is
derived from interfaces A and B.

Multiple Inheritance

Hybrid Inheritance
It is a mix of two or more of the above types of inheritance. Since Java doesn’t
support multiple inheritances with classes, hybrid inheritance involving
multiple inheritance is also not possible with classes. In Java, we can achieve
hybrid inheritance only through Interfaces if we want to involve multiple
inheritance to implement Hybrid inheritance.
However, it is important to note that Hybrid inheritance does not necessarily
require the use of Multiple Inheritance exclusively. It can be achieved through
a combination of Multilevel Inheritance and Hierarchical Inheritance with
classes, Hierarchical and Single Inheritance with classes. Therefore, it is
indeed possible to implement Hybrid inheritance using classes alone, without
relying on multiple inheritance type.
final keyword is used in different contexts. First of all, final is a non-access
modifier applicable only to a variable, a method, or a class. The following are
different contexts where final is used.

Characteristics of final keyword in java: In Java, the final keyword is used


to indicate that a variable, method, or class cannot be modified or extended.
Here are some of its characteristics:

 Final variables: When a variable is declared as final, its value


cannot be changed once it has been initialized. This is useful for
declaring constants or other values that should not be modified.
 Final methods: When a method is declared as final, it cannot be
overridden by a subclass. This is useful for methods that are part of a
class’s public API and should not be modified by subclasses.
 Final classes: When a class is declared as final, it cannot be
extended by a subclass. This is useful for classes that are intended to
be used as is and should not be modified or extended.

Public private protected

<html>
<head>
<title>Parameter Applet</title>
</head>
<body>
<h1>Parameter Applet</h1>
<p>This applet displays the name and age of a person that are passed as
parameters from the HTML file.</p>
<applet code="ParamApplet.class" width="300" height="100">
<!-- Specify the parameters using param tags -->
<param name="name" value="Alice">
<param name="age" value="25">
</applet>
</body>
</html>

B. Java, Inheritance is an important pillar of OOP(Object-Oriented Programming). It


is the mechanism in Java by which one class is allowed to inherit the features(fields
and methods) of another class. In Java, Inheritance means creating new classes based
on existing ones. A class that inherits from another class can reuse the methods and
fields of that class. In addition, you can add new fields and methods to your current
class as well.
Why Do We Need Java Inheritance?
 Code Reusability: The code written in the Superclass is common to all
subclasses. Child classes can directly use the parent class code.

 Method Overriding: Method Overriding is achievable only through
Inheritance. It is one of the ways by which Java achieves Run Time
Polymorphism.
 Abstraction: The concept of abstract where we do not have to provide all
details is achieved through inheritance. Abstraction only shows the
functionality to the user.

How to Use Inheritance in Java?


The extends keyword is used for inheritance in Java. Using the extends keyword
indicates you are derived from an existing class. In other words, “extends” refers to
increased functionality.

Syntax :
class derived-class extends base-class
{
//methods and fields
}

Method Overloading Method Overriding

Method overloading is a compile-time Method overriding is a run-time


polymorphism. polymorphism.

Method overriding is used to grant the


Method overloading helps to increase specific implementation of the method which
the readability of the program. is already provided by its parent class or
superclass.

It is performed in two classes with


It occurs within the class.
inheritance relationships.

Method overloading may or may not


Method overriding always needs inheritance.
require inheritance.
Method Overloading Method Overriding

In method overloading, methods must


In method overriding, methods must have the
have the same name and different
same name and same signature.
signatures.

In method overloading, the return type


In method overriding, the return type must be
can or can not be the same, but we just
the same or co-variant.
have to change the parameter.

Static binding is being used for Dynamic binding is being used for overriding
overloaded methods. methods.

It gives better performance. The reason


Poor Performance due to compile time
behind this is that the binding of overridden
polymorphism.
methods is being done at runtime.

Private and final methods can be Private and final methods can’t be
overloaded. overridden.

The argument list should be different The argument list should be the same in
while doing method overloading. method overriding.
Basis of Exception Error
Comparis
on

Recoverabl Exception can be recovered by using


e/ the try-catch block. An error cannot be
Irrecovera recovered.
ble

Type It can be classified into two categories All errors in Java are
i.e. checked and unchecked. unchecked.

Occurrence It occurs at compile time or run time. It occurs at run time.

Package It belongs to java.lang.Exception It belongs to


package. java.lang.Error package.

Known or Only checked exceptions are known to Errors will not be known
unknown the compiler. to the compiler.

Causes It is mainly caused by the application It is mostly caused by


itself. the environment in
which the application is
running.

Example Checked Exceptions: SQLException, Java.lang.StackOverFlow


IOException ,
Unchecked java.lang.OutOfMemory
Exceptions: ArrayIndexOutOfBoundExc Error
eption, NullPointerException,
ArithmaticException
1.When type of the object is determined at run-time, it is known as dynamic binding.

2.It takes place at runtime so do it is referred to as late binding.


3.It uses overriding methods.
4.It takes place using virtual functions
5.Real objects use dynamic binding.
In the object-oriented programming, a class serves as a fundamental building block A class
accomplishes data hiding by restricting access to private variables and data, which can only be
accessed within the class and not externally. A class is like a logical blueprint used to create objects
that share common properties and methods.

To declare a member of a class static in Java, you need to use the keyword static before the
member declaration. For example, to declare a static field, you can write:
Java
public class MyClass {
static int x = 10; // a static field
}

Static members belong to the class itself, not to any instance of the class. Therefore, you can access them
without creating an object of the class, by using the class name. For example:
Java
MyClass.x = 20; // access the static field
MyClass.myMethod(); // call the static method

Z8
B. A method is a block of code that performs a specific task in Java. It can be defined by using the
following syntax:
Java
modifier return_type method_name (parameter_list) {
// method body
}
AI-generated code. Review and use carefully. More info on FAQ.

Where,

 modifier is the access type of the method, such as public, private, protected,
or default.
 return_type is the data type that the method returns, such as int, String, void, etc.
 method_name is the name of the method, which should be a verb and start with a lowercase
letter.
 parameter_list is the list of parameters that the method takes as input, separated by commas
and enclosed in parentheses. If the method has no parameters, the parentheses are left empty.

myMethod(10, "Hello"); // calling a method with two arguments

Method overriding in Java is a feature that allows a subclass to provide a different implementation of a
method that is already defined by its superclass. It means that the subclass can change the behavior of the
method without changing its name, parameters, or return type. This way, the subclass can customize or
enhance the functionality of the superclass method according to its needs.

Method overriding is useful for achieving runtime polymorphism, which means that the actual method that
is executed depends on the type of the object that invokes it. For example, if you have a superclass called
Animal and a subclass called Dog, both of them have a method called sound(). The Animal class defines
the sound() method as printing “Animal makes sound”, while the Dog class overrides the sound() method
as printing “Dog barks”. Now, if you create an object of Animal type and call its sound() method, it will
print “Animal makes sound”. But if you create an object of Dog type and call its sound() method, it will
print “Dog barks”. This is because the Dog object uses the overridden method from its own class, not the
inherited method from the Animal class.

There are some rules for method overriding in Java, such as:

 The method name, parameters, and return type must be the same as in the superclass method.
 The access modifier of the overridden method cannot be more restrictive than the superclass
method. For example, you cannot override a public method as private or protected.
 The overridden method cannot throw new or broader checked exceptions than the superclass
method.
 The overridden method cannot be marked as final, static, or abstract.
Descriptio Siz Example
Type n Default e Literals Range of values

boolean true or false false 1 bit true, false true, false

twos-
8
complement 0 (none) -128 to 127
bits
byte integer

‘a’, ‘\u0041’, ‘\ characters representation of


Unicode 16
\u0000 101’, ‘\\’, ‘\’, ‘\ ASCII values
character bits
char n’, ‘β’ 0 to 255

twos-
16
complement 0 (none) -32,768 to 32,767
bits
short integer

twos- -2,147,483,648
32
complement 0 -2,-1,0,1,2 to
bits
int intger 2,147,483,647

twos- -9,223,372,036,854,775,808
64 -2L,-
complement 0 to
bits 1L,0L,1L,2L
long integer 9,223,372,036,854,775,807

1.23e100f , -
IEEE 754 32 1.23e-
0.0 upto 7 decimal digits
floating point bits 100f , .3f ,3.14
float F

1.23456e300d
IEEE 754 64
0.0 , -123456e- upto 16 decimal digits
floating point bits
double 300d , 1e1d

You might also like