0% found this document useful (0 votes)
19 views36 pages

Java Part 2

- A class is a blueprint that defines objects and members like variables and methods. - Objects are instances of a class that are stored in memory. They contain the non-static members defined in their class. - A class contains both static and non-static members. Static members are associated with the class while non-static members are associated with objects.

Uploaded by

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

Java Part 2

- A class is a blueprint that defines objects and members like variables and methods. - Objects are instances of a class that are stored in memory. They contain the non-static members defined in their class. - A class contains both static and non-static members. Static members are associated with the class while non-static members are associated with objects.

Uploaded by

rahul nigam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

JAVA PART 2

 Fundamental of oops:
Object: It is a Block of Memory which can have multiple variables
and methods in it.
*) Every Object will have reference(Address).
*) An Object represents realworld entity/object.
*) It can store Hetrogeneous type of data.
Realworld Object: Anything which has existence in real world is
known as Realworld object.
Ex: Car,laptop,Tv etc…….
*) Every object will have its own properties & behaviours.
Note: Since the states & behaviours belongs to one particular object
, If I need to store all these things we need one dedicated memory
location(Container). But we don’t have any predefined datatype
which helps to create such container.
Hence we can design our own datatype with the help of a class,
Which can be used as non-primitive datatype.
 Class:
*) Class is a keyword.
*) It is used to design a user defined non-primitive datatype to store
the details of object.
*) It can also be considered as blueprint of an object.
*) We need class for execution of program.
*) In a class we can create members like methods( to store
behaviours of an object) as well as variable(to store the states of
object).
*) Every classname can be a Non-primitive datatype.
*) The java language also comes up with lot of built-in classes
i.e, String, System etc……
Ex: class Employee
{
// design variables to store states.
// design methods to store behaviours.
}

Note:
*) The non-primitive datatype will have only default values i.e null.
*) They will not be having any default size.
*) Since the name of the class itself can be used as a non-primitive
datatype.
*) we can have any members of non-primitive datatype in java.
Members of Class: Anything which is declared within class block is
known as members of the class.
Ex: Methods, Variables, Initializers, Constructors etc….
 The members of the class cab be classified into 2 Types..
1.) Static Members of class.
2.) Non-Static Members of class.

1.) Static Members of class:


Any members of the class which is prefixed with static modifier is
known as static member of class.
In java we have Following static members.
i) Static Method
ii) Static Variable
iii) Static Initializers.

i) Static Methods: A method which is prefixed with static modifier is


known as static methods.
*) The static members will have the memory allocated inside class
static area.
*) Since the static members get the memory inside class static area,
they also known as class members.
*) A block which belongs to a static member is known as Static
Context.
*) For executing a Static Context we will have a frame getting created
inside Stack Area, which is known as Static Frame.
*) Every static frame will have a built-in reference pointing to its
class-static area.
*) The static members of the class can be used within the static
context in the following ways…
i.) Directly with the help of members name or member signature.
ii.) with the help of class using Access operator(.).

Access Operator(.) :
*) It is a Binary Operator.
*) It is used to access a member present in another class.

How to use a static members of one class inside static context of


another class ?
We can use static members inside another class with the help of
classname along with access operator.
Note:
i) Initial Class: The class which is given as input to java command is
known as initial class.
ii) Dependent Class: A Class used inside another class is known as
dependent class.
ii) Static Variable: A Global Variable which is prefixed with static
modifier / keyword is known as Static Variable.
*) It should be declared inside class block.
Note: A local variable cannot be made static.

Points to be Remember:-
1.) A static variable gets its memory allocated inside class static area.
2.) Since all static variables will be present inside class static area,
they are also known as Class Variables.
3.) A static variable will be assigned with default values.
4.) A static variable can be used anywhere within the class also in a
different class.
i) same class: Directly using variable name.
ii) diff class: With the help of classname as reference.
5.) A static variable of one class can be accessed inside another class
with the help of classname.
6.) In java we can have local variable as well as global variable with
the same name.
7.) In such situations the highest priority will be given the local
variables.
8.) Global variable can be used inside all the members of the class.,
whereas the local variables can be used only inside the block where
it is declared.
iii) Static Initializers: Initializers are the members of the class which
are used for initialization purpose.
In java we have two types of Initializers:
1) Static declare & Initialization statement.
2) Static block.

1.) Static declare & Initialization statement: The static Initializer


gets executed during the loading process of a class.
*) The static initializers gets executed only once for class loading.
Syntax: static datatype identifier= value/exp;
2.) Static block: An Anonymous block which is prefixed with static
modifiers is known as static block.
*) It is also static Initializer.
Syntax:
static
{
// java stmts;
}
Note:
The static block gets executed during the loading process of
class ( i.e: before the execution of main method).
*) The static block gets executed only once for the loading process of
a class.
Characteristics of Static Block:
i) It doesn’t have a name.
ii) It doesn’t have a return type.
iii) It doesn’t have a formal arguments.
iv) It cannot accept any inputs from the user.
v) The programmer cannot call it explicitly.

Note: Inside a class we can have more than one static block & they
gets executed in top to bottom order.

LOADING PROCESS OF A CLASS


1.) A class static area will be created for given class (initial class).
2.) All the methods ( static as well as non-static ) will be loaded to
method area.
3.) If static methods are present ,its reference will be stored
(Method signature+address) in class static area.
4.) If a static variable is present, it will get its memory allocated
inside class static area with default value.
5.) If static initializers are present, they gets executed directly.
6.) The loading process is said to be complete.
2.) Non-Static Members of class:
Any member declared inside class block without prefixing static
modifier is known as Non-static member of a class.
In java we have following Non-static members
i) Non-static Variable.
ii) Non-static Method.
iii) Non-static Initializers.
iv) Non-static Constructor.

Note:
*) The non-static members gets their memory allocated inside
object.
*) If we need to access any non-static members…. The first thing we
must do id create an object.

What is Object?
*) An object is a block of memory created during in runtime inside
heap area.
*) The object also known as Instance Of A Class.
*) The process of creating an object is known as INSTANCIATION.
Syntax to create an object:
new constructor of the class;

Syntax for Constructor of the class:


Classname();
Therefore the actual syntax of object creation is:
new classname();

 new:
*) new is a keyword.
*) It is a unary operator.
*) It is used to create an object along with constructor of the class.
*) new will create a block of memory inside heap area and it will
return the address of it after the loading process is completed.
*) The type of address generated for the object will always be
specific to its class. The address of object generated will always be in
the below format:
classname@hexadecimal value

 Constructor:
Constructor are used to load the non-static members into the object.

Note:
1.) We can have multiple objects created for one single class.
2.) Everytime we create a new object, it will create a new block of
memory with different address.
3.) Everytime we create an object , the non-static members will be
loaded into an object.
Non-Primitive Datatypes:
*) They are user defined datatypes which are used to store the
following things:
i) The default values of non-primitive datatypes i.e null.
ii) Address of an object.
*) The variables which are created using non-primitive datatypes is
known as Non-primitive variables / Reference Variables.

 Reference Variable: A variable which is created for the non-


primitive datatypes is known as Reference variables.
*) The Reference Variables are used to store address of object.
Syntax to create reference variable: classname identifier;

Note:
Inside static context we can access static members in 3 ways:
i) Directly with the help of member name.
ii) With the help of classname & access Operator.
iii) With the help of address of the object.
Inside a static context we can access non-static members in
following ways:
i) Only with the help of Address of the object.
i) Non-static Variable: A variable which is not prefixed with static
modifier is known as non-static variable.
*) They gets their memory allocated inside object created in heap
area.
*) Non static variable can be accessed with the help of object
reference or address.
*) Non static variables will be allocated in every object of the class
created.
*) Non static variables initialized with the default values.

ii) Non-static Method: A method which is not prefixed with static


modifier is known as non-static method.
*) Non-static method gets their memory allocated inside object
created in heap area.
*) A block which belongs to non-static method is called as non-static
context.
*) Non-static methods can be accessed inside static context of same
class using Object address/ reference.
*) Non static Methods will be allocated in every object of the class
created.
*) Inside Non-static context, we can use both static & Non-static
members of the same class directly without any reference.
Non-Static Context:
Any block or Any instruction which belongs to Non-static member of
a class is known as Non-static Context.
Note:
1.) We can use the Non-static members of the class inside Non-static
Context directly.
2.) We can use static members of the class inside non-static context
directly.

Constructor:
Constructor is a special non-static members which is used to load all
the non-static members of a class into the object.
Note:
1.) The name of the constructor must be same as that of classname.
2.) The constructor is just similar to a method except the return
type.
3.) Constructor doesn’t return anything after the execution, hence
constructor has no returntype.
4.) In java every class created must and should have a constructor.
5.) Therefore, the compiler will always check whether a programmer
has created atleast one constructor inside the class or not.
6.) If it is not created , then compiler will add a constructor into the
class file such constructors are known as Default Constructor.
Types of Constructors
1.) Default Constructor
2.) User-Defined Constructor.
i.) No-Argument Constructor.
ii.) Parameterized Constructor.
1.) Default Constructor: A No-argument constructor added by the
compiler during the compilation is known as Default Constructor.
*) It will be added only when the user forgets to create even a single
constructor inside the class.

2.) User-Defined Constructor: The constructor created by the users


or programmers are known as user-defined constructor.
Syntax to create Constructor:
[modifier] classname ([Formal Arguments])
{
//java stmts;
}
i) No-Arguments Constructor: A constructor which doesn’t
have a formal arguments declared is known as No-
arguments
ii) Constructor.
Note: Whenever we create an object using No-Argument
Constructor, we need to initialize the states using extra-instructions
which is the main disadvantage of No-argument Constructor ,i.e we
will not be able to initialize the states of the object during object
creation itself.
*) To Overcome this Problem ,java Provides us one more member
known as parameterized constructor.

ii) Parameterized Constructor: A constructor which has a formal


arguments created during declaration is known as Parameterized
Constructor.
*) The Parameterized Constructor are used for initializing states of
object.

LOADING PROCESS OF OBJECT


1.) new keyword creates a block of memory inside heap area.
2.) The constructor used for object creation is called implicitly.
3.) The Loading of Non-static members into the object takes place.
a.) If any Non-static variables is present ,it will be loaded with
default values.
b.) If any Non-static method is present ,its reference will be
stored inside the object.
4.) If any Non-static initializer are present ,they get executed directly.
5.) Once the execution of constructor is completed , the address of
object will b-e returned.
 this:
*) “this” is a keyword.
*) It is a non-static variable which can be used only inside non-static
context.
*) “this” keyword will always have the address of the current object.

Purpose of this keyword:


*) It is used to access non-static members of the class.
*)Whenever we are in a situation where global variable & local
variable are having the same name.. to access the global variable we
can use “this” keyword
Non-Static –Initializer

 In java we have following non-static-initializers.


i.) Non-static declare & initialization statement.
ii.) Non-static block.
i.) Non-static declare & initialization statement:
Syntax: Datatype identifier=value/Expression;
ii) Non-static block: An Anonymous block which is not prefixed with
static modifier is known as Non-static block.
*) The non-static blocks gets executed during loading process of
object i.e, it gets executed for each & every object created.
*) If we have more than one non-static block inside the class, they
gets executed in top to bottom order.

Purpose of Non-static block:


1.) The Non-static block is use to count the number of objects
created.
2.) Usage of non-static block in the realtime , The Non-static blocks
are used to generate the Id’s automatically to the object.
ENCAPSULATION
The process of binding both states and behaviours of an object
together is known as Encapsulation.
*) We can achieve Encapsulation with the help of class.

Data Hiding: The Process of restricting the direct access to the states
of the class but providing controlled access with the help of
behaviours of the same class is known as Data Hiding.
*) Data Hiding is possible only inside encapsulated class.

Steps to achieve DataHiding:


1.) Prefix the data member with the help of private access
modifier.
2.) Define the getter & setter method based on Requirement.

Private:
*) It is a Keyword.
*) It is a access modifier.
*) Any members which is prefixed with private access modifier can
be accessed only within the same class.
1.) Getter Method: It is a Method which is used to print the private
members data from different class.
*) It returns the data of members which has to be read.
Syntax: Datatype of private_Mem getPrivateMemName()
{
Return Private_Mem_name;
}

2.) Setter Method: It is a Method which is used to modify the data of


private member of different class.
*) Setter method must always accept an input , it must have formal
arguments.
Syntax: void setPrivateMemName ( DatatypeofprivateMem ident)
{
this.PrivateMemberName=identifier;
}
NOTE: we can achieve the flexibility of accessing the private
members data in following ways:
1.) Readable
2.) Readable & Modify
3.) Only Modify
4.) Not Readable & Not Modifiable
RELATIONSHIP
Relationship means some Connection or some type of bond
between two Objects.
The relationship between objects can be achieved with the help of
following ways.
1.) Has-a-relationship
2.) Is-A-relationship

1.) Has-a-relationship: An object is being dependent on another


object is known as Has-a-relationship.
Or
*) Has-a-relationship is just similar to relationship between a whole
object and a part of it.
Ex: Bank has a Account
Car has a engine
Books has a pages
NOTE:
*) In java we can achieve has-a-relationship between two objects by
creating a reference of one object inside another object.

Composition: The process of one object having a reference of


another object is known as Composition.
( <>----- )Representation of Has-a-relationship.
*) Composition can be achieved with the help of below design
techniques.
1.) Early Binding/Instantiation
2.) Lazy Binding/Instantiation

1.) Early Binding/Instantiation: It is a design technique in which


the Dependent Object(Composed Object) has to be created
during the loading process of Depending Object(Whole
Object).
*) we can achieve early instantiation with the help of Initializer.
Eg: Car & Engine
Book & pages
Bank & Account
Flipkart & Products.
2.) Lazy Binding/Instantiation: It is a design technique in which the
Dependent Object(Composed Object) Should not be created during
the loading process of Dependent Object(Whole Object) , Instead it
will be created only with the help of behaviour ( method of same
object).

2.) Is-A-Relationship: It is just similar to a relationship between


parent and child where the child aquires all the states & behaviours
of parent.
Eg: Cardiologist is a doctor.
Student is a Person
Car is a Vehicle
Saving Account is a Account.
*) The main purpose of using Is-A-Relationship is to achieve
Generalization , where Parent object behaves like a generalized
object and Child more is Specialized Object.
*) Whatever belongs to parent ,it also belongs to child.
*) But Whatever Created by the child , it doesn’t belongs to parent.
*) Basically such relation is called as Is-a-relationship.

Note: we can achieve Is-a-relationship with the help of very


interesting concept called as Inheritance.

Inheritance: Inheritance is a design principle which is used to achieve


Is-a relationship between two Objects, where one object will aquire
all the states & behaviours of another object.
Parent Object/Class: A Class which provides states & behaviours to
another Class is known as Parent Object/Class.
*) Parent object is also known Super Object/Base
Object/Generalized Object.

Child Object/Class: A Class which recieves states & behaviours


from another Class is known as Child Object/Class.
*) Child class is also known as Sub / Derived /Specialized class/Object

Note: In java we can achieve Inheritance using following keywords.


i) extends
ii) implements
i) extends: extends keyword can be used between 2 class or 2
Interface.
ii) implements: implements keyword is used when I want to achieve
a inheritance between Interface & Class.
( will understand about Interface & implements keyword in
Abstraction ).

Types of Inheritance
1.) Single-level Inheritance
2.) Multi-level Inheritance
3.) Hierarchical Inheritance
4.) Multiple Inheritance
5.) Hybrid Inheritance

1.) Single-level Inheritance: An Inheritance of one level is known as


Single-level Inheritance.
Or
Inheritance between one child and one parent is known as
Single-level Inheritance.

2.) Multi-level Inheritance: An Inheritance of more than one level is


known as Multi-level Inheritance.

3.) Hierarchical Inheritance: An Inheritance where one parent will


have more than one child at the same level is known as hierarchical
Inheritance.
4.) Multiple Inheritance: An inheritance where one child will have
more than one parent in the same level is known as Multiple
Inheritance.
NOTE: Multiple Inheritance is not possible in java with the help of
only class.
*) It can be achieved with the help of Inheritance.
Diamond Problem: The ambiguity in multiple Inheritance , which
arises when a super class member is tried to call from a subclass is
known as diamond problem.

5.) Hybrid Inheritance: It is a Combination of two or more


Inheritance.

*) Non-Primitive TypeCasting: The Process of converting one Non-


primitive type to Another Non-primitive type, we call it as Non-
primitive typecasting.
*) We can achieve Non-primitive typecasting only when there is Is-a-
relationship.
 Types of Non-primitive Typecasting
i) Upcasting
ii) Downcasting

i) Upcasting: The Process of Converting Subclass reference type into


Super class reference type is known as Upcasting.
*) Creating an Object of Subclass, and storing it’s address into a
reference of type superclass.
*) With Upcasted reference we can access only superclass
members/properties.
*) In order to achieve Upcasting , Is-a relationship is mandatory.
*) Upcasting will be done implicitly, hence it is also known as Auto
Upcasting.
Disadvantages of Upcasting:
i) Whenever we upcast one reference type to its parent type,
we will not be able to access subclass members with the
help of superclass reference.
Note: To overcome the problem of Upcasting we will go for
Downcasting.
 Advantages of Upcasting:
i) Upcasting helps us to create a generalized Container inside
which we can store any of the subclass reference.

ii) Downcasting: The process converting Super class reference type


into sub class reference type is known as Downcasting.
*) Downcasting will not be done implicitly, we must do it Explicitly
with the help of Typecast Operator.
*) In order to achieve Downcasting , Upcasting is Mandatory.
 Advantages of Downcasting:
The Main advantage of Downcasting is we will be able to access the
members of subclass now.
POLYMORPHISM
*)Polymorphism is a Greek word.
*) Where Poly means Many, & Morphism means Forms..
(Many Forms).
*) Polymorphism is nothing but having multiple behaviours with
same name is known as Polymorphism.
Or
*) It is the ability of members to exhibits more than one forms is
known as polymorphism.
In java we can achieve Polymorphism in two ways:
i) Compile Time Polymorphism.
ii) Runtime Polymorphism.

i) Compile Time Polymorphism: If the Implementation of the


member has to be used is decided by the compiler during
compilation, then we call it as Compile Time Polymorphism.
*) The decision is taken by the Compiler.
In java , we have following Compile time polymorphisms.
1.) Method Overloading.
2.) Constructor Overloading.
3.) Shadowing
i)Variable Shadowing
ii)Method Shadowing
1.) Method Overloading: A class having methods with same name
but different formal arguments is known as Method Overloading.
*) Method Overloading is an example of compile time polymorphism
as the compiler decides which implements to be executed based on
the actual arguments passed hence it is known as compile time
Polymorphism.
2.) Constructor Overloading: A class having constructors with same
name but different formal arguments is known as Constructor
Overloading.
*) Constructor Overloading is an example of compile time
polymorphism as the compiler decides which implements to be
executed based on the actual arguments passed hence it is known as
compile time Polymorphism.
3.) Shadowing: Shadowing is nothing but having members with same
name or signature with same class virtually.
We have following types of Shadowing:-
i) Variable Shadowing.
ii) Method Shadowing.
i) Variable Shadowing: If both Superclass & Subclass are having
variables of same name( either static or non-static variable) is known
as Variable Shadowing.
It is the compiler who decides which variable to be used based on:
1.)Place of usage.
2.)Type of reference variable created.
*) If the type of reference variable is of subclass, subclass member
will be used , if the type of reference is of super class then super
class member will be used. It doesn’t depend on the type of object
created.
ii) Method Shadowing: If both Superclass & Subclass are having
methods with same signature then we call it as Method Shadowing.
Rules of Method Shadowing:
1.) There should be Is-A Relationship between classes.
2.) We should have “Static Methods” with same signature.
3.) The return type of both the methods must be same.
4.) The subclass access modifier must always have either similar
visibility or higher Visibility Compared to superclass method
[private<default<protected<public].

Method Shadowing is also example of compile time


polymorphism, as the compiler who decides which method to be
used based on:
1.)Place of usage.
2.)Type of reference variable created.
ii) Run Time Polymorphism: In the Runtime Polymorphism , the
binding between method call stmt & method Implementation to be
executed.. happens during Runtime. Hence it is known as Runtime
Polymorphism.
Note: In Runtime Polymorphism Whatever compiler will see may not
get executed.
*) We can achieve Runtime Polymorphism with the help of
Method Overriding.

1.) Method Overriding: The process of providing new


Implementation to the Super class methods from subclass is known
as Method Overriding.
Note: Rule of Method Overriding is just similar to rules of method
shadowing, but here we have “Non-static Methods” instead of
“static methods”.
*) Method Overriding is an Example for Runtime Polymorphism Since
which implementation to be executed is decided during Runtime
based on type of Object created.
Note: static methods cannot be overridden.
ABSTRACTION
Abstraction is used to provide only the necessary details or essential
features of the object & hide the implementation of it from the user.
In java we can achieve Abstraction with the help of :-
i) Class
ii) Interface
i) Abstraction with the help of Class:-
1.) With the help of class we can achieve Abstraction from 0 to 100%
2.) Abstraction class can have two types of Methods
w.r.t. Abstraction:
a.) Concrete Method
b.) Abstract Method
a.) Concrete Method: A Method which has Method implementation
is known as Concrete Method.
Syntax: [modifiers] returntype Methodname([formal Arguments])
{
// java stmts;
}
Ex: public int test() {
System.out.println(“From Test”);
return 10;
}
b.) Abstract Method: A method which is incomplete, which doesn’t
have a body is called as Abstract method.
Or
A method which doesn’t have Method implementation is known as
Abstract Method.
*) It is mandatory abstract method starts with abstract & ends with
semicolon.
Syntax: abstract [Access modifier] returntype Methodname([F A]);
Ex: abstract public void add(int a, int b);

With Respect to Abstraction we can classify classes into two types:


1.) Abstract Class
2.) Concrete Class
1.) Abstract Class: A class which is prefixed with abstract modifier is
known as abstract class.
Ex: abstract class A1
{
// java stmts;
}
When Should we make a class Abstract ?
1.) If we have an Abstract method declared inside the class.
2.) If we have an Abstract method Inherited but not overridden.
Important Points To Be Remembered About Abstract Class:
1.) Abstract classes have Constructors.
2.) Abstract classes can have both Concrete Method & Abstract
Method.
3.) We can make class Abstract even if it is not having the Abstract
Method.
4.) Variable Cannot be Abstracted.
5.) We cannot Instantiate for abstract class i.e, We can’t create
object for Abstract Classes.
6.) Static Members can’t be abstracted.
7.) We cannot make Static Method Abstract.
8.) The concept of Abstraction is Valid only for Non-static Members.
9.) Private members can’t be prefixed with a Abstract Modifier.
10.) final members can’t be Abstracted.

2.) Concrete Class: A class which is not prefixed with Abstract


modifier is known as Concrete class.
*) A concrete class cannot have any Abstract Method.
Concrete Implementing Class:-
A class inside which we provide implementation to all the abstract
methods using Method Overriding is Known as Concrete
Implementing class.
 Steps to create a concrete implementing class:-
Step1: Create a Subclass for the Abstract class.
Step2: Provide Implementation to all the methods which is present
inside Abstract class.
ii) Interface:
*) It is a Keyword.
*) Interface is a user defined Non-primitive datatype which is used to
achieve multiple Inheritance & 100% Abstraction.
Syntax to create Interface:
Interface InterfaceName
{
// java stmts;
}

Note:
*) By default interface is an abstract member.
*) we cannot create an Object for interface.
*) we can compile an interface & generate a class file for that.
*) we cannot make an interface either private or protected, It can be
either default or public.
*) we cannot make an interface as final member.

Members we can have inside Interface:


1.) Inside interface we can have static methods.
2.) Inside interface we can have public abstract method.
3.) Public static final variable is allowed.
Note:
*) Methods inside interface will have public & abstract by default, so
we cannot have body for abstract method.
*) We cannot update or reinitialize the variable bcz by default
variable will be prefixed with public static & final modifiers.
*) Without initializing we cannot use final variable , so it is
mandatory to initialize variables inside interface.

Members we cannot have inside Interface:


1.) Non-static variable Not allowed.
2.) Non-public abstract methods Not allowed.
3.) Non-public static methods not allowed.
4.) Final abstract methods.
5.) Static block & Non-static block.
6.) Constructors & Initializers.
7.) Private & Protected members.
8.) Non-final static variables.

 Inheritance w.r.t. Interfaces:


*) Inheritance between interface & interface can be achieved with
the help of extends keyword.
*) Inheritance between class & interface can be achieved with the
help of implements keyword.
Note:
1.) The static methods of interfaces are not inherited.
2.) The Abstract methods & the public static final variables will be
inherited.
Case1: Interface Inheriting Interface.

*)An Interface can inherit more than one interfaces with the help of
extends keyword.
Case2: A class inherits interface.
i) A class can inherit Interface using implements keyword.
*) implements is represented using dotted line.
Ex: interface A1
{
}
Class B implements A1
{
}
ii) A class can inherit any number of interfaces.
Ex: interface A1{ interface A2{
} }
Class B implements A1,A2{
}

3.) A class can inherit one class & any no. of interfaces.
Ex: interface A1{} interface A2{} class A3{}
Class B extends A3 implements A1,A2{}

Note: Whenever a class is inheriting from both the types of members


i.e. A class as well as interfaces, we must always make sure we use
extends keyword first & then implements, else we get CTE.

4.) Interface cannot inherit class, it is not possible in java.

You might also like