Você está na página 1de 21

Presentation by:

Joshi
INHERITANCE
INHERITANCE
• Acquiring the properties of an existing Object
into newly creating Object to overcome the
redeclaration of properties in deferent
classes.
• These are 3 types:
1.Simple Inheritance
SUPER SUPER

extends extends

SUB SUB 1 SUB 2


INHERITANCE
2. Multi Level 3. Multiple
Inheritance Inheritance
SUPER
SUPER 1 SUPER 2
extends

SUB
implements
extends

SUB SUB SUB


extends
• Is a keyword used to inherit a class
from another class
• Allows to extend from only one class

class One class Two extends One


{ {
int a=5; int b=10;
} }
super
• ‘super’ is a keyword used to refer to hidden
variables of super class from sub class.
• super.a=a;
• It is used to call a constructor of super class
from constructor of sub class which should
be first statement.
• super(a,b);
• It is used to call a super class method from
sub class method to avoid redundancy of
code
• this.addNumbers(a, b);
ABSTRACT METHOD

• A method with only declaration and


without any definition.
• It should be declared with a keyword
abstract.

• syntax:
abstract returntype methodName(arguments);
ABSTACT CLASS
• A class with at least one abstract
method.
• Explicitly creating an instance on
abstract class is not possible as it is an
incomplete class, but implicitly it
creates an instance when we try to
create an instance of its sub class.
• Abstract methods has to be overridden
by extending the abstract class.
ABSTRACT CLASS
abstract class Calculator
{
int sum(int a, int b)
{
return a+b;
}
abstract double squareRoot(double d);
}
INTERFACE
INTERFACE
• A class with all abstract methods.
• Instance will not be created for interface
either explicitly or implicitly.
• All the properties of interface are static
and final.
• We can implement any number of
interfaces into single class.
• Used in Dynamic Polymorphism
syntax of Interface

interface <interface_name>
{
static final variables;
abstract methods declaration();
}
implements
• Is a keyword used to implement one or more
interfaces into a class.
interface A interface B
{ {
} }
class NewClass implements A, B
{}
class NewClass extends One implements A, B
{}
Polymorphism
• Many ways of outputs for single type
of request.

There are 2 types


– Static Polymorphism
• Over Loading of Methods (Early Binding)
– Dynamic Polymorphism
• Dynamic Method Dispatch (Late Binding)
Polymorphism Example
class PolyDemo
{
public static void main(String args[])
{
Fig f;
f=new Circle();
f.area();
f=new Rectangle();
f.area();
}
}
Access Modifiers
static
• Static Variables are class variable but not
instance variable which are declared and
initialised once in its life and shared by
any no. of instances.

• Static Method or Variables can be used


with class name without creating an
instance or with reference variables of
instances.

Ex: Math.sqrt(5);
final
• final keyword is used declare constants
which can not change its value of
definition.
• final Variables can not change its
value.
• final Methods can not be Overridden
or Over Loaded
• final Classes can not be extended or
inherited
Access Specifiers
Access Specifiers
Same Pack Same Pack Diff Pack
In Diff Pack
Sub Diff Sub
Class Diff Class
Class Class Class

Private OK NO NO NO NO

Default OK OK Instance NO NO

Protect OK OK Instance OK NO

Public OK OK Instance OK Instance


Packages
• It is group of classes and interfaces
• classes or interfaces can be
imported into another programme
with the help of packages only.
• It is used for better organisation of
classes.
Syntax:
package <package_name>;

import package.classes;

Você também pode gostar