Professional Documents
Culture Documents
NO Inheritance Polymorphism
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.
<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>
Syntax :
class derived-class extends base-class
{
//methods and fields
}
Static binding is being used for Dynamic binding is being used for overriding
overloaded methods. methods.
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
Type It can be classified into two categories All errors in Java are
i.e. checked and unchecked. unchecked.
Known or Only checked exceptions are known to Errors will not be known
unknown the compiler. to the compiler.
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.
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
twos-
8
complement 0 (none) -128 to 127
bits
byte integer
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