SlideShare a Scribd company logo
1 of 21
Static and Dynamic binding
Static Binding: it is also known as early binding the compiler resolve the
binding at compile time. all static method call is resolved at compile time
itself. Because static method are class method and they are accessed using
class name itself. Hence there is no confusion for compiler to resolve. which
version of method is going to call.
ClassA
{
PublicstaticvoidA1()
{
S.O.P(“”Iamin A”);
}
}
ClassB extendsA
{
PublicstaticvoidA1()
{
S.O.P(‘IaminB”);
}
}
ClassC extendsB
{
PublicstaticvoidA1()
{
S.O.P(“Iamin C”);
}
}
ClassTT
{
Publicstaticvoidmain(Stringarr[])
{
A a= newC();//methodcall isstaticallybindedsoitwill call atcompile time itself andprintClassA
a.A1(); //method
B.A1()//use of classname toaccessthe staticmethod
}
}
Dynamic Binding: dynamic binding also known as late binding it
means method implementation that actually determined at run time and
not at compile time.
ClassA
{
Publicdoit()
{
S.O.P(“Iamin A”);
}
}
ClassB extendsA
{
Publicdoit()
{
S.O.P(“Iamin B”);
}
}
ClassC extendsB
{
Publicdoit()
{
S.O.P(“Iamin C”);
}
}
ClassTest
{
Publicstaticvoidmain(Stringarr[])
{
A x= newB();
x.doit();//itwill print Iamin B
}}
Anotherexampleof dynamicbindinggivenas
classA1
{
voidwho()
{
System.out.println("i aminAAA");
}
}
classB1 extendsA1
{
voidwho()
{
System.out.println("i amoinBBB");
}
}
class C1 extendsB1
{
voidwho()
{
System.out.println("i amincc");
}
}
classDynamicbinding
{
publicstaticvoidmain(Stringarr[])
{
A1 a=newB1();
a.who();
}
}
Note: x is reference variable of object of type A but it is instantiated an object of
Class B because of new B().JRE will look at this statement and say even though x is
clearly declared as type A,it is instantiated as an object of class B, so it will run the
version of doit method that is defined.
UNIT-3
Abstract Method:
It is an method without implementation it represents what to be done. but does
not specify how it will be done.
Any class which contains one or more abstract method is defined as abstract
class.
An abstract class has following characteristics’.
1. It cannot be instantiated(i.e its object is not created).
2. It forces all its subclasses to provide the implementation of its abstract
method.
If any subclass does not provide implementation of even a single abstract
method of super class then subclass is also declared as abstract.
e.g
abstract classA
{
AbstractvoidcallMe(); //abstractmethod
{
ClassB extendsA
{
VoidcallMe()
{
S.O.P(“thisisacall for me”);
}
Public staticvoidmain(Stringarr[])
{
B b= newB();
b.callMe();
}
}
Note:
1. Abstractclass isnot interface.
2. An abstractclass musthave an abstract method
3. Abstractclass can have constructor,membervariable andnormal methods.
4. Abstractclassescan neverbe instantiated.
5. When you extend abstract class with abstract method you must define the
abstract method in child class. or make child class abstract.
Abstract is an important feature of OOPs.it means hiding complexity. Abstract
class used to provide abstraction.
Example: we are casting instance of car type under Vehicle .now vehicle
reference can be used to provide implementation but it will hide the actual
implementation process.
abstract class vehicle
{
public abstract void engine();
}
public class Car extends Vehicle
{
Public void engine()
{
System.out.println(“Car engine”);
//here you can write any other code
}
public static void main(String arr[])
{
Vehicle v=new Car();
v.engine();
}
}
Output: car Engine
Abstract methods are usually declared where two or more subclasses are expected to do
similar things in different ways through different implementation.
These subclasses extend the same abstract class and provide different implementation for
abstract method.
INTERFACE:
Interfaceis collection of abstractmethods and static final data members.
Interfacecannotbe instantiated but their reference variablecan be created.
Syntax:
interface identifier
{
Static final data members;
Implicit abstractmethod();
}
e.g.
interface Printable
{
Void print ();
}
Interface is implemented by class. If a class implements’ an interface then it need
to provide public definition of all interface methods. Otherwise class is made
abstract
Syntax of implementation:
Class identifier implements interface-name
{
Public definition of interface method;
Additional method of class
}
e.g
Class A implements Printable
{
Public void print ()
{
S.O.P(“A is printable”);
}
}
Difference between class and interface
{
Class interface
Degree of abstraction:
Class supportZero to 100 % abstraction.i.e.
in a class we can have all abstract have all
abstract methods all non abstract method
or both combination
Interface support only 100% abstraction
i.e. they can only have abstract methods
Type of inheritance:
Class facilitate feature based inheritance
(all part of class is inherited in another
class) in java as well as in life multiple
feature based inheritance is not supported.
It facilitate role based inheritance (only
specific part of class is called).in java as
well as in real life multiple role based
inheritance is supported.
Inheritance Purpose Analogy
Feature based
inheritance
Code reusability and run
time polymorphism
Represent blood relation
of real life .A is son of B.in
such relation both feature
and name of is inherited
from single family.i.e
multiple feature based
inheritance is not
supported in real life .
same is the case with java
Role based inheritance
Run time polymorphism Represent non blood
relations of real life . e.g A
is friend of C and C is a
doctor . in such realtion
only name is inherited .
each name represents a
role in real life multiple
role is played by same
person i.e multiple role
based inheritance .
If a class impliments interface then interface name is inherited by the class
e.g
public interface Doable
{
Void canbeDone();
}
Class A impliments Doable
{
Public void canbe Done()
{
;;;;;
}
}
Now A is Doable
Fromfigure we can found that Krishna is playing different role at different
instance.
During softwaredevelopment programmers need to refer classes of other
programmers class can be referenced in the following three ways.
1. By Name(we can access the instance variableand method using class name)
2. By family(inheritance is the best example)
3. By Role(we need interface here)
1.in the firstapproach a class is directly referenced by its name . in this
approach in order to refer the class its name mustbe known available
classes is called static class environment referencing and referenced class.
Tight coupling created the maintenance problem.
e.g
let the programmer A and B who are working on a software.
Programmer A is assigned a class name one
Programmer Bis assigned a class name two
Class one is simple and A will nedd only 10 hours to complete.
Class two is complex and B nedd 10 days to complete.
krishna
VISHNU
DWARKADHESH
VASUDEV
KAHANA
Class one has a dependence on class two
Class one
{
Public static voidinvoke(twox)
{
x.display();
}
}
Class two
{
/will complete in10 days
}
Limitation:
 A need to wait till completion of class two in order to complete his
class.
 If B changes name of his class. A will require to modify his class(tight
coupling)
2.By Family:
Class one
{
Public static void
invoke(canbeDisplayed x)
{
x.display();
}
Abstractclass canbeDisplayed
{
Abstractvoid display()
}
Class two extends canbeDisplayed
{
/will complete in 10 days
}
Advantage:
 Unknown and unavailable classes canbe referencedas long as they are
part of referencedfamily.
 Name of referenceclasses canbe changedwithout affecting the reference
class
Disadvantage:
 Top most class of family must be known and available.
 Facility of referencing unknownand unavailable class is combinedto
one family
3.By Role:
Class one
{
Public static void
invoke(canbeDisplayed x)
{
x.display();//completed in 10 min
;;;;;;;;;;;;;;;;;;;;;
}
InterfacecanbeDisplayed
{
Void disaplay() //completed in 10 min
}
Class two impliments canbeDisplayed
{
//completed in 10 days
}
Advantages:
1. Any class belongs toany family which plays the referenced role canbe
referenced. this facility of referencing unknownand unavailable classes is
calleddynamic classing environment.
2. Only the role nedd to be known torefrence class
3. Role basedreference creates loosecoupling between refrencing and
refrencedclass.
Following use case demonstrate the neddof role basedinheritance i.e
requirement of only name in inheritance.
Let there are three programmer namedA ,B and C A is defining a class
named PQR whichconatins P,Q,and R methods.
A needtoexpose only method P of this class toClass B.
And only method Q toclass C
Let A can achieve his object only as
Claa of A interface Class of B
Class PQR impliments
P,Q
{
Interface P
{
Void P()
Class Puser
{
Public static void
Public void P()
{
;;;;;;;;;;;;;
‘’’’’’
}
Public void Q()
{
;;;;;;;;;;;;;;
;;;;;;;;;;;;;;
}
Public void R()
{
;;;;;;;;;;;;
;;;;;;;;;;;;;
}
}
}
Interface Q
{
Void Q
{
Void Q();
}
invoke(P x)
{
x.P();
;;;;;;;;;;;;
}
}
Class of C
Class Quser
{
Public static void
invoke(Q x)
{
Public static void
invoke(Q x)
{
x.Q();
;;;;;;;;;;;;;;;;;;;
}
}
Example of interface: thereare two interfaces one for whattype and one
for vegetable class. Through these two interfaces we can decide what type
of fruit or vegetable has.
interface Fruit {
public boolean hasAPeel();
//has a peel must be implemented in any class implementing Fruit
//methods in interfaces must be public
}
interface Vegetable {
public boolean isARoot();
//is a root mustbe implemented in any class implementing Vegetable
//methods in interfaces must be public
}
class whattype{
static String doesThisHaveAPeel(FruitfruitIn)
{
if (fruitIn.hasAPeel())
return "This has a peel";
else
return "This does not havea peel";
}
static String isThisARoot(Vegetable vegetableIn)
{
if (vegetableIn.isARoot())
return "This is a root";
else
return "This is not a root";
}
static String doesThisHaveAPeelOrIsThisRoot(
Tomato tomatoIn)
{
if (tomatoIn.hasAPeel() && tomatoIn.isARoot())
return "This both has a peel and is a root";
else if (tomatoIn.hasAPeel() || tomatoIn.isARoot())
return "This either has a peel or is a root";
else
return "This neither has a peel or is a root";
}
}
class Tomato implements Fruit, Vegetable {
boolean peel = false;
boolean root= false;
public Tomato() {}
public boolean hasAPeel()
//must havethis method,
// becauseFruit declared it
{
return peel;
}
public boolean isARoot()
//must have this method,
// because Vegetable declared it
{
return root;
}
public static void main(String[] args) {
//Part one: making a tomato
Tomato tomato = new Tomato();
System.out.println(whattype.isThisARoot(tomato));
//output is: This is not a root
System.out.println(
whattype.doesThisHaveAPeel(tomato));
//output is: This does not have a peel
System.out.println
(whattype.doesThisHaveAPeelOrIsThisRoot
(tomato));
//output is: This neither has a peel or is a root
//Part two: making a fruit
//Fruit = new Fruit();
//can not instantiate an interface like
// this becauseFruit is not a class
Fruit tomatoFruit = new Tomato();
//can instantiate by interface like
// this becauseTomato is a class
//System.out.println(
// FandVUtility.isThisARoot(tomatoFruit));
//can not treat tomatoFruitas a Vegetable
// withoutcasting it to a Vegetable or Tomato
System.out.println(
whattype.doesThisHaveAPeel(tomatoFruit));
//output is: This does not have a peel
//System.out.println
// (whattype.doesThisHaveAPeelOrIsThisRoot
// (tomatoFruit));
//can not treat tomatoFruitas a Vegetable
// withoutcasting it to a Vegetable or Tomato
}
}
Example: write a programto calculate area of class rectangle and class
triangle through interface .
class Rectangle
{
public float compute(float x, float y)
{
return(x* y);
}
}
class Triangle
{
public float compute(float x,float y)
{
return(x * y/2);
}
}
class InterfaceArea
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
System.out.println("Area Of Rectangle = "+ rect.compute(1,2));
System.out.println("Area Of Triangle= "+ tri.compute(10,2));
}
}
Example: make the interface for addition and subtraction and access the
classes with various methods in it.
interface Addd
{
public int Add(int x,int y);
public int sub(int a,int b);
}
interface Subb
{
public double sub(double x,double y);
}
class Additionimplements Addd
{
public int Add(intx,int y)
{
return x+y;
}
public int sub(intx,int y)
{
return x-y;
}
public float Add( float x ,float y)
{
return x+y;
}
public double Add(doublex , double y, double z)
{
return x+y+z;
}
}
class Subtractionimplements Subb
{
public int sub(intx,int y)
{
return x-y;
}
public float sub(floatx,float y)
{
return x-y;
}
public double sub(doublex , double y)
{
return x-y;
}
}
class Math
{
public static void main(String arr[])
{
Addd a1=new Addition();
Subb a11= new Subtraction();
System.out.println("addition is="+a1.Add(10,29));
System.out.println("subtraction is="+a1.sub(20,12));
System.out.println("subtraction is="+a11.sub(20.0,12.0));
}
}

More Related Content

What's hot

8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
Tuan Ngo
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10
Terry Yoast
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 

What's hot (20)

chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Interface
InterfaceInterface
Interface
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
Interfaces in JAVA !! why??
Interfaces in JAVA !!  why??Interfaces in JAVA !!  why??
Interfaces in JAVA !! why??
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java interface
Java interfaceJava interface
Java interface
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
inheritance
inheritanceinheritance
inheritance
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Opps
OppsOpps
Opps
 
Interface in java
Interface in javaInterface in java
Interface in java
 

Similar to Static binding

Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
quantumiq448
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 

Similar to Static binding (20)

Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
 
Design pattern
Design patternDesign pattern
Design pattern
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Java Reflection Concept and Working
Java Reflection Concept and WorkingJava Reflection Concept and Working
Java Reflection Concept and Working
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Inheritance
InheritanceInheritance
Inheritance
 
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Java tutoria part 2
Java tutoria part 2Java tutoria part 2
Java tutoria part 2
 
Java
JavaJava
Java
 
Inheritance
InheritanceInheritance
Inheritance
 
Java 06
Java 06Java 06
Java 06
 
Java02
Java02Java02
Java02
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 

More from vishal choudhary

More from vishal choudhary (20)

SE-Lecture1.ppt
SE-Lecture1.pptSE-Lecture1.ppt
SE-Lecture1.ppt
 
SE-Testing.ppt
SE-Testing.pptSE-Testing.ppt
SE-Testing.ppt
 
SE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.pptSE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.ppt
 
SE-Lecture-7.pptx
SE-Lecture-7.pptxSE-Lecture-7.pptx
SE-Lecture-7.pptx
 
Se-Lecture-6.ppt
Se-Lecture-6.pptSe-Lecture-6.ppt
Se-Lecture-6.ppt
 
SE-Lecture-5.pptx
SE-Lecture-5.pptxSE-Lecture-5.pptx
SE-Lecture-5.pptx
 
XML.pptx
XML.pptxXML.pptx
XML.pptx
 
SE-Lecture-8.pptx
SE-Lecture-8.pptxSE-Lecture-8.pptx
SE-Lecture-8.pptx
 
SE-coupling and cohesion.ppt
SE-coupling and cohesion.pptSE-coupling and cohesion.ppt
SE-coupling and cohesion.ppt
 
SE-Lecture-2.pptx
SE-Lecture-2.pptxSE-Lecture-2.pptx
SE-Lecture-2.pptx
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
 
SE1.ppt
SE1.pptSE1.ppt
SE1.ppt
 
SE-Lecture-4.pptx
SE-Lecture-4.pptxSE-Lecture-4.pptx
SE-Lecture-4.pptx
 
SE-Lecture=3.pptx
SE-Lecture=3.pptxSE-Lecture=3.pptx
SE-Lecture=3.pptx
 
Multimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptxMultimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptx
 
MultimediaLecture5.pptx
MultimediaLecture5.pptxMultimediaLecture5.pptx
MultimediaLecture5.pptx
 
Multimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptxMultimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptx
 
MultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptxMultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptx
 
Multimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptxMultimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptx
 
Multimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptxMultimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptx
 

Recently uploaded

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Static binding

  • 1. Static and Dynamic binding Static Binding: it is also known as early binding the compiler resolve the binding at compile time. all static method call is resolved at compile time itself. Because static method are class method and they are accessed using class name itself. Hence there is no confusion for compiler to resolve. which version of method is going to call. ClassA { PublicstaticvoidA1() { S.O.P(“”Iamin A”); } } ClassB extendsA { PublicstaticvoidA1() { S.O.P(‘IaminB”); } } ClassC extendsB { PublicstaticvoidA1() { S.O.P(“Iamin C”); }
  • 2. } ClassTT { Publicstaticvoidmain(Stringarr[]) { A a= newC();//methodcall isstaticallybindedsoitwill call atcompile time itself andprintClassA a.A1(); //method B.A1()//use of classname toaccessthe staticmethod } } Dynamic Binding: dynamic binding also known as late binding it means method implementation that actually determined at run time and not at compile time. ClassA { Publicdoit() { S.O.P(“Iamin A”); } } ClassB extendsA { Publicdoit() { S.O.P(“Iamin B”); }
  • 3. } ClassC extendsB { Publicdoit() { S.O.P(“Iamin C”); } } ClassTest { Publicstaticvoidmain(Stringarr[]) { A x= newB(); x.doit();//itwill print Iamin B }} Anotherexampleof dynamicbindinggivenas classA1 { voidwho() { System.out.println("i aminAAA"); } } classB1 extendsA1
  • 4. { voidwho() { System.out.println("i amoinBBB"); } } class C1 extendsB1 { voidwho() { System.out.println("i amincc"); } } classDynamicbinding { publicstaticvoidmain(Stringarr[]) { A1 a=newB1(); a.who(); } }
  • 5. Note: x is reference variable of object of type A but it is instantiated an object of Class B because of new B().JRE will look at this statement and say even though x is clearly declared as type A,it is instantiated as an object of class B, so it will run the version of doit method that is defined. UNIT-3 Abstract Method: It is an method without implementation it represents what to be done. but does not specify how it will be done. Any class which contains one or more abstract method is defined as abstract class. An abstract class has following characteristics’. 1. It cannot be instantiated(i.e its object is not created). 2. It forces all its subclasses to provide the implementation of its abstract method. If any subclass does not provide implementation of even a single abstract method of super class then subclass is also declared as abstract. e.g abstract classA { AbstractvoidcallMe(); //abstractmethod { ClassB extendsA { VoidcallMe()
  • 6. { S.O.P(“thisisacall for me”); } Public staticvoidmain(Stringarr[]) { B b= newB(); b.callMe(); } } Note: 1. Abstractclass isnot interface. 2. An abstractclass musthave an abstract method 3. Abstractclass can have constructor,membervariable andnormal methods. 4. Abstractclassescan neverbe instantiated. 5. When you extend abstract class with abstract method you must define the abstract method in child class. or make child class abstract. Abstract is an important feature of OOPs.it means hiding complexity. Abstract class used to provide abstraction. Example: we are casting instance of car type under Vehicle .now vehicle reference can be used to provide implementation but it will hide the actual implementation process. abstract class vehicle { public abstract void engine(); } public class Car extends Vehicle { Public void engine() { System.out.println(“Car engine”);
  • 7. //here you can write any other code } public static void main(String arr[]) { Vehicle v=new Car(); v.engine(); } } Output: car Engine Abstract methods are usually declared where two or more subclasses are expected to do similar things in different ways through different implementation. These subclasses extend the same abstract class and provide different implementation for abstract method. INTERFACE: Interfaceis collection of abstractmethods and static final data members. Interfacecannotbe instantiated but their reference variablecan be created. Syntax: interface identifier { Static final data members; Implicit abstractmethod(); }
  • 8. e.g. interface Printable { Void print (); } Interface is implemented by class. If a class implements’ an interface then it need to provide public definition of all interface methods. Otherwise class is made abstract Syntax of implementation: Class identifier implements interface-name { Public definition of interface method; Additional method of class } e.g Class A implements Printable { Public void print () { S.O.P(“A is printable”); } }
  • 9. Difference between class and interface { Class interface Degree of abstraction: Class supportZero to 100 % abstraction.i.e. in a class we can have all abstract have all abstract methods all non abstract method or both combination Interface support only 100% abstraction i.e. they can only have abstract methods Type of inheritance: Class facilitate feature based inheritance (all part of class is inherited in another class) in java as well as in life multiple feature based inheritance is not supported. It facilitate role based inheritance (only specific part of class is called).in java as well as in real life multiple role based inheritance is supported. Inheritance Purpose Analogy Feature based inheritance Code reusability and run time polymorphism Represent blood relation of real life .A is son of B.in such relation both feature and name of is inherited from single family.i.e multiple feature based inheritance is not supported in real life . same is the case with java Role based inheritance Run time polymorphism Represent non blood relations of real life . e.g A is friend of C and C is a doctor . in such realtion
  • 10. only name is inherited . each name represents a role in real life multiple role is played by same person i.e multiple role based inheritance . If a class impliments interface then interface name is inherited by the class e.g public interface Doable { Void canbeDone(); } Class A impliments Doable { Public void canbe Done() { ;;;;; } } Now A is Doable
  • 11. Fromfigure we can found that Krishna is playing different role at different instance. During softwaredevelopment programmers need to refer classes of other programmers class can be referenced in the following three ways. 1. By Name(we can access the instance variableand method using class name) 2. By family(inheritance is the best example) 3. By Role(we need interface here) 1.in the firstapproach a class is directly referenced by its name . in this approach in order to refer the class its name mustbe known available classes is called static class environment referencing and referenced class. Tight coupling created the maintenance problem. e.g let the programmer A and B who are working on a software. Programmer A is assigned a class name one Programmer Bis assigned a class name two Class one is simple and A will nedd only 10 hours to complete. Class two is complex and B nedd 10 days to complete. krishna VISHNU DWARKADHESH VASUDEV KAHANA
  • 12. Class one has a dependence on class two Class one { Public static voidinvoke(twox) { x.display(); } } Class two { /will complete in10 days } Limitation:  A need to wait till completion of class two in order to complete his class.  If B changes name of his class. A will require to modify his class(tight coupling) 2.By Family: Class one { Public static void invoke(canbeDisplayed x) { x.display(); } Abstractclass canbeDisplayed { Abstractvoid display() } Class two extends canbeDisplayed { /will complete in 10 days } Advantage:  Unknown and unavailable classes canbe referencedas long as they are part of referencedfamily.  Name of referenceclasses canbe changedwithout affecting the reference class Disadvantage:  Top most class of family must be known and available.
  • 13.  Facility of referencing unknownand unavailable class is combinedto one family 3.By Role: Class one { Public static void invoke(canbeDisplayed x) { x.display();//completed in 10 min ;;;;;;;;;;;;;;;;;;;;; } InterfacecanbeDisplayed { Void disaplay() //completed in 10 min } Class two impliments canbeDisplayed { //completed in 10 days } Advantages: 1. Any class belongs toany family which plays the referenced role canbe referenced. this facility of referencing unknownand unavailable classes is calleddynamic classing environment. 2. Only the role nedd to be known torefrence class 3. Role basedreference creates loosecoupling between refrencing and refrencedclass. Following use case demonstrate the neddof role basedinheritance i.e requirement of only name in inheritance. Let there are three programmer namedA ,B and C A is defining a class named PQR whichconatins P,Q,and R methods. A needtoexpose only method P of this class toClass B. And only method Q toclass C Let A can achieve his object only as Claa of A interface Class of B Class PQR impliments P,Q { Interface P { Void P() Class Puser { Public static void
  • 14. Public void P() { ;;;;;;;;;;;;; ‘’’’’’ } Public void Q() { ;;;;;;;;;;;;;; ;;;;;;;;;;;;;; } Public void R() { ;;;;;;;;;;;; ;;;;;;;;;;;;; } } } Interface Q { Void Q { Void Q(); } invoke(P x) { x.P(); ;;;;;;;;;;;; } } Class of C Class Quser { Public static void invoke(Q x) { Public static void invoke(Q x) { x.Q(); ;;;;;;;;;;;;;;;;;;; } } Example of interface: thereare two interfaces one for whattype and one for vegetable class. Through these two interfaces we can decide what type of fruit or vegetable has. interface Fruit { public boolean hasAPeel(); //has a peel must be implemented in any class implementing Fruit //methods in interfaces must be public }
  • 15. interface Vegetable { public boolean isARoot(); //is a root mustbe implemented in any class implementing Vegetable //methods in interfaces must be public } class whattype{ static String doesThisHaveAPeel(FruitfruitIn) { if (fruitIn.hasAPeel()) return "This has a peel"; else return "This does not havea peel"; } static String isThisARoot(Vegetable vegetableIn) { if (vegetableIn.isARoot()) return "This is a root"; else return "This is not a root"; } static String doesThisHaveAPeelOrIsThisRoot( Tomato tomatoIn) { if (tomatoIn.hasAPeel() && tomatoIn.isARoot()) return "This both has a peel and is a root"; else if (tomatoIn.hasAPeel() || tomatoIn.isARoot()) return "This either has a peel or is a root"; else return "This neither has a peel or is a root";
  • 16. } } class Tomato implements Fruit, Vegetable { boolean peel = false; boolean root= false; public Tomato() {} public boolean hasAPeel() //must havethis method, // becauseFruit declared it { return peel; } public boolean isARoot() //must have this method, // because Vegetable declared it { return root; } public static void main(String[] args) {
  • 17. //Part one: making a tomato Tomato tomato = new Tomato(); System.out.println(whattype.isThisARoot(tomato)); //output is: This is not a root System.out.println( whattype.doesThisHaveAPeel(tomato)); //output is: This does not have a peel System.out.println (whattype.doesThisHaveAPeelOrIsThisRoot (tomato)); //output is: This neither has a peel or is a root //Part two: making a fruit //Fruit = new Fruit(); //can not instantiate an interface like // this becauseFruit is not a class Fruit tomatoFruit = new Tomato(); //can instantiate by interface like // this becauseTomato is a class //System.out.println( // FandVUtility.isThisARoot(tomatoFruit)); //can not treat tomatoFruitas a Vegetable // withoutcasting it to a Vegetable or Tomato System.out.println( whattype.doesThisHaveAPeel(tomatoFruit)); //output is: This does not have a peel //System.out.println
  • 18. // (whattype.doesThisHaveAPeelOrIsThisRoot // (tomatoFruit)); //can not treat tomatoFruitas a Vegetable // withoutcasting it to a Vegetable or Tomato } } Example: write a programto calculate area of class rectangle and class triangle through interface . class Rectangle { public float compute(float x, float y) { return(x* y); } } class Triangle { public float compute(float x,float y) { return(x * y/2); } } class InterfaceArea { public static void main(String args[]) { Rectangle rect = new Rectangle(); Triangle tri = new Triangle();
  • 19. System.out.println("Area Of Rectangle = "+ rect.compute(1,2)); System.out.println("Area Of Triangle= "+ tri.compute(10,2)); } } Example: make the interface for addition and subtraction and access the classes with various methods in it. interface Addd { public int Add(int x,int y); public int sub(int a,int b); } interface Subb { public double sub(double x,double y); } class Additionimplements Addd { public int Add(intx,int y) { return x+y; } public int sub(intx,int y) { return x-y; } public float Add( float x ,float y) {
  • 20. return x+y; } public double Add(doublex , double y, double z) { return x+y+z; } } class Subtractionimplements Subb { public int sub(intx,int y) { return x-y; } public float sub(floatx,float y) { return x-y; } public double sub(doublex , double y) { return x-y; } } class Math { public static void main(String arr[])
  • 21. { Addd a1=new Addition(); Subb a11= new Subtraction(); System.out.println("addition is="+a1.Add(10,29)); System.out.println("subtraction is="+a1.sub(20,12)); System.out.println("subtraction is="+a11.sub(20.0,12.0)); } }