Object oriented programming allows classes to inherit commonly used state and behavior from other classes through inheritance. Inheritance establishes a parent-child relationship between classes where the child inherits members from the parent. Java uses the keyword "extends" for inheritance and does not support multiple inheritance. Abstract classes and interfaces are designed to be inherited only, with abstract classes able to contain implemented methods and interfaces consisting solely of abstract methods that must be implemented in subclasses. Nested, inner, and anonymous classes can be defined within other classes, with inner classes having access to enclosing class members and anonymous classes declared without a name at instantiation.
1. Core Java
Debasish Pratihari
Inheritance :
Object oriented programming allows classes to
inherit commonly used state and behavior from
other class through inheritance.
Allows a class to acquire the members of another
class.
Parent-child/super-sub relationship is established.
Child can not restrict the scope of inheritance.
By default java classes are sub class of Object class
directly or indirectly.
Java does not
supports multiple
inheritance.
Parent/super decide the members that can be
inherited by access specifiers.
Note:
Java uses a keyword
extends to inherit.
Primarily solves two purposes. Re-usability and
Providing a generic behavior.
A super class reference can refer to any object of its
subclass.
If signature of a function in the super class is same
as the signature of a function in the sub-class then
the function is said to be over-ridden and it can’t be
called by the object of sub class.
Any other class reference can be assigned to Object
Class object.
If a function is
declared as final it
can’t be over-ridden.
Incase of a child class member having the same
name as of a parent class member then the parent
class member is suppresses. And then to call those
parent class members we have to use the super
keyword.
If a class is final
then it is restricted
to be inherited.
Compiler attaches a word super() as the first line of
the constructor by default which calls the no-args
constructor of the parent.
Note :
Super object always holds address/reference of
parent class.
Lecture/core/oops3/10
Page #1
feel the Technology…
2. Core Java
Debasish Pratihari
Abstract Class:
An abstract class is specifically designed to be
inherited only. i.e. it can’t be instantiated.
However it can be used to create object
reference.
A class can be made Abstract by using the
keyword “abstract”
If a class contains any abstract method then it
must be declared as abstract.
A abstract class may have fully defined methods
(concrete methods) also.
Abstract Method:
Abstract methods are not concrete methods.
They need to be override by the subclasses.
An abstract method force to declare its class as
abstract.
Example :
abstract class Shape{
abstract void getData();
void aboutUs(){
System.out.println(“Developed by Debasish at Lakshya”);
}
}
class Circle extends Shape{
void getData(){
//method definition here
}
}
Lecture/core/oops3/10
Page #2
feel the Technology…
3. Core Java
Debasish Pratihari
Interface:
Using the keyword interface, you can fully abstract a
class interface from its implementation.
Using interface, you can specify what a class must
do, but not how it does it.
Interfaces are lack of instance variable, and their
methods are declared without any body
A class can implement any number of interfaces
To implement an interface, a class must create a
complete set of methods defined by the interface.
All the variables of an interface are public, static and
final .All methods are abstract.
The methods of a class that implements can
interface must be declared public.
It is both permissible and common for classes that
implement interfaces to define additional members
of their own.
You can declare variables as object reference that
uses an interface rather than a class type. Any
instance of any class that implements the declared
interface can be referred to by such variable.
An interface reference variable only has knowledge
of the methods declared by its interface declaration
If a class includes an interface but does not fully
implement the methods defined by that interface,
than that class must be declared as abstract
One interface can inherit another interface by use of
the keyword extends.
When a class implements an interface that inherits
another interface, it must provide implementation for
all the methods defined within the interface
inheritance chain.
Lecture/core/oops3/10
Page #3
feel the Technology…
4. Core Java
Debasish Pratihari
Nested, Inner and Anonymous Classes:
A class can be defined inside another class, called
nested class.
Its scope is limited within its enclosing class.
A nested class can access all the members of its
enclosing class including private members. But the
enclosing class can’t access any member of the
nested class.
The nested class can be static or non-static
A static nested class can access the members of its
enclosing class by instantiating it.
An inner class is a non-static nested class.
The natural application for inner class is to declare
classes that are used only inside some other class or
that are closely related to another.
Inner classes may declare only compile time
constant fields.
inner and anonymous classes can make your Java
code easier to understand and maintain. They keep
related classes in the same source file and avoid the
proliferation of very small classes
Non-static inner classes have some limitations,
though. Specifically, they cannot declare static
initializers and static members, except in constant
fields. Besides that, inner classes declared inside
methods cannot access the method’s local variables
and parameters, unless they are declared final.
Inner-Anonymous classes are classes that don’t have
a name (hence the term anonymous). Because they
don’t have a name, there is no way to refer to them.
For this reason, their declaration must be given at
creation time, as part of the new statement.
Technically, anonymous classes are considered nonstatic inner classes, so they have the same privileges
and limitations of non-static inner classes declared
inside a method.
Lecture/core/oops3/10
Page #4
feel the Technology…