0% found this document useful (0 votes)
6 views6 pages

Java Inheritance and Polymorphism Overview

java_ahort_notes[1] (AutoRecovered)

Uploaded by

kishangupta3231
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Java Inheritance and Polymorphism Overview

java_ahort_notes[1] (AutoRecovered)

Uploaded by

kishangupta3231
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

java

Inheritance
1. class Subclass-name extends Superclass-name
2. {
3. //methods and fields
4. }

Q) Why multiple inheritance is not supported in java?


To reduce the complexity and simplify the language, multiple inheritance
is not supported in java.

 polymorphism

Method Overloading in Java


 Different ways to overload the method
 By changing the no. of arguments
 By changing the datatype
 Why method overloading is not possible by changing the return type
 Can we overload the main method
 method overloading with Type Promotion
If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.

Can we overload java main() method?


Yes, by method overloading. You can have any number of main methods
in a class by method overloading. But JVM calls main () method which
receives string array as arguments only. Let's see the simple example:

1. class TestOverloading4{
2. public static void main (String [] args)
{[Link]("main with String[]");}
3. public static void main(String args){[Link]("main with
String");}
4. public static void main(){[Link]("main without args");}
5. }

Method Overriding in Java


If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in Java.

Usage of Java Method Overriding


o Method overriding is used to provide the specific implementation of a
method which is already provided by its superclass.
o Method overriding is used for runtime polymorphism

Usage of Java super Keyword

1. super can be used to refer immediate parent class instance variable.


2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.

Polymorphism in Java is a concept by which we can


perform a single action in different ways.
We can perform polymorphism in java by method overloading and method
overriding.

Upcasting

If the reference variable of Parent class refers to the object of Child class,
it is known as upcasting. For example:
1. class A{}
2. class B extends A{}
1. A a=new B();//upcasting

Java instanceof
The java instanceof operator is used to test whether the object is an
instance of the specified type (class or subclass or interface).

An object of subclass type is also a type of parent class.


For example, if Dog extends Animal then object of Dog
can be referred by either Dog or Animal class.

Abstraction in Java
Abstraction is a process of hiding the implementation details and
showing only functionality to the user.

Points to Remember

o An abstract class must be declared with an abstract keyword.


o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to
change the body of the method.

Interface in Java
An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
There are mainly three reasons to use interface. They are given below.

o It is used to achieve abstraction.


o By interface, we can support the functionality of multiple
inheritance.
o It can be used to achieve loose coupling.

Q) Multiple inheritance is not supported through class in java,


but it is possible by an interface, why?
As we have explained in the inheritance chapter, multiple inheritance is
not supported in the case of class because of ambiguity. However, it is
supported in case of an interface because there is no ambiguity. It is
because its implementation is provided by the implementation class. For
example:

interface Printable{
void print();
}
interface Showable{
void print();
}

class TestInterface3 implements Printable, Showable{


public void print(){[Link]("Hello");}
public static void main(String args[]){
TestInterface3 obj = new TestInterface3();
[Link]();
}
}

Java Package
A java package is a group of similar types of classes, interfaces and sub-
packages.

There are many built-in packages such as java, lang, awt, javax, swing, net, io,
util, sql etc.

 java,
 lang,
 awt,
 javax,
 swing,
 Net
 io
 util,
 sql

Encapsulation in Java
Encapsulation in Java is a process of wrapping code and data together
into a single unit, for example, a capsule which is mixed of several
medicines.

Object class in Java


The Object class is the parent class of all the classes in java by default.
In other words, it is the topmost class of java.

Exception Handling in Java


The Exception Handling in Java is one of the powerful mechanism to
handle the runtime errors so that the normal flow of the application can
be maintained.

There are mainly two types of exceptions: checked and unchecked. An


error is considered as the unchecked exception. However, according to
Oracle, there are three types of exceptions namely:

1. Checked Exception
2. Unchecked Exception
3. Error

java catch block


Java catch block is used to handle the Exception by declaring the type of
exception within the parameter.
Java Nested try block
In Java, using a try block inside another try block is permitted. It is called
as nested try block. Every statement that we enter a statement in try
block, context of that exception is pushed onto the stack.

Java throw keyword


The Java throw keyword is used to throw an exception explicitly.

Lifecycle of Java Applet


1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.

You might also like