SlideShare a Scribd company logo
A CASE STUDY ON “JAVA”
By – Ayush Gupta
Himanshu Tripathi
Neeraj Kr Sahu
Sub Topics:–
• Polymorphism
• Encapsulation
• Primitive types in Java
• Casting in Java
• Types of error in Java
POLYMORPHISM IN JAVA
• Polymorphism in Java is a concept by which we can perform a single action in different
ways. Polymorphism is derived from 2 Greek words: ‘poly’ and ‘morphs’.
• The word "poly" means many and "morphs" means forms. So polymorphism means
“many forms”.
• There are two types of polymorphism in Java: Compile-time polymorphism and
Runtime polymorphism.
• We can perform polymorphism in java by ‘method overloading’ and ‘method
overriding’.
• If you overload a static method in Java, it is the example of compile time
polymorphism.
• Here, we will focus on runtime polymorphism in java.
RUNTIME POLYMORPHISM IN JAVA
• Runtime polymorphism or Dynamic Method Dispatch is a process in
which a call to an overridden method is resolved at runtime rather than
compile-time.
• In this process, an overridden method is called through the reference
variable of a super class. The determination of the method to be called
is based on the object being referred to by the reference variable.
EXAMPLE OF JAVA RUNTIME POLYMORPHISM
• In this example, we are creating two classes Bike and Splendor.
• Splendor class extends Bike class and overrides its run ( ) method. We
are calling the run method by the reference variable of Parent class.
• Since it refers to the subclass object and subclass method overrides the
Parent class method, the subclass method is invoked at runtime.
EXAMPLE OF JAVA RUNTIME POLYMORPHISM
CONT.
class Bike
{
void run(){System.out.println("running");
}
}
class Splendor extends Bike
{
void run()
{
System.out.println("running safely with 60km");
}
public static void main(String args[])
{
Bike b = new Splendor();//upcasting
b.run();
}
}
Output:
running safely with 60km.
METHOD OVERRIDING
•If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.
•In other words, If a subclass provides the specific implementation of the method
that has been declared by one of its parent class, it is known as method
overriding.
•Usage of Java Method Overriding
•Method overriding is used to provide the specific implementation of a method
which is already provided by its superclass.
•Method overriding is used for runtime polymorphism.
METHOD OVERRIDING
Class Super
{
int x=10;
void disp()
{
system.out.println(“Super class” + x);
}
}
Class Test
{
Public static void main(String ar[] )
{
Sub.obj= new Sub();
Obj.disp();
}
}
Class Sub extends Super
{
int y=20;
void disp()
{
System.out.println(“Super”+x);
System.out.println(“Sub”+y);
}
}
OUTPUT:-
Super 10
Sub 20
METHOD OVERLOADING
• If a class has multiple methods having same name but different in parameters, it is known as
Method Overloading.
• If we have to perform only one operation, having same name of the methods increases the
readability of the program.
• Suppose you have to perform addition of the given numbers but there can be any number of
arguments, if you write the method such as a(int , int) for two parameters, and b(int , int , int)
for three parameters then it may be difficult for you as well as other programmers to
understand the behavior of the method because its name differs.
• Method overloading increases the readability of the program.
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new
DisplayOverloading();
obj disp('a');
obj disp('a',10);
}
}
Output:
a
a 10
ENCAPSULATION
• 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.
• We can create a fully encapsulated class in Java by making all the data
members of the class private. Now we can use setter and getter
methods to set and get the data in it. The Java Bean class is the
example of a fully encapsulated class.
ADVANTAGE OF ENCAPSULATION
These are benefits of Java Encapsulation:
Data Hiding – It can provide the programmer to hide the inner classes and the user
to give access only to the desired codes. It allows the programmer to not allow the
user to know how variables and data store.
Flexibility – With this, we can make the data as read-only or write-only as we
require it to be.
Reusability – It allows the user to a programmer to use the program again and
again.
Testing of the code – Ease of testing becomes easy.
PRIMITIVE TYPES
• int 4 bytes
• short 2 bytes
• long 8 bytes
• byte 1 byte
• float 4 bytes
• double 8 bytes
• char Unicode encoding (2 bytes)
• boolean {true,false}
Behaviors is
exactly as in
C++
JAVA PRIMITIVE DATA TYPES
Data Type Characteristics Range
byte 8 bit signed integer -128 to 127
short 16 bit signed integer -32768 to 32767
int 32 bit signed integer -2,147,483,648 to 2,147,483,647
long 64 bit signed integer -9,223,372,036,854,775,808 to- 9,223,372,036,854,775,807
float 32 bit floating point number + 1.4E-45 to
+ 3.4028235E+38
double 64 bit floating point number + 4.9E-324 to
+ 1.7976931348623157E+308
boolean true or false NA, note Java booleans cannot be converted to or from other
types
char 16 bit, Unicode Unicode character, u0000 to uFFFF Can mix with integer
types
Constants:-
• 37 integer
• 37.2 float
• 42F float
• 0754 integer (octal)
• 0xfe integer (hexadecimal)
Primitive types - cont.
CASTING
• Casting is the temporary conversion of a variable from its original
data type to some other data type.
• With primitive data types if a cast is necessary from a less inclusive
data type to a more inclusive data type it is done automatically.
int x = 5;
double a = 3.5;
double b = a * x + a / x;
double c = x / 2;
• if a cast is necessary from a more inclusive to a less inclusive data
type the class must be done explicitly by the programmer
failure to do so results in a compile error.
double a = 3.5, b = 2.7;
int y = (int) a / (int) b;
y = (int)( a / b );
y = (int) a / b; //syntax error
PRIMITIVE CASTING
double
float
long
int
short,
char
byte
Outer ring is most inclusive
data type.
Inner ring is least inclusive.
In expressions variables and
sub expressions of less
inclusive data types are
automatically cast to more
inclusive.
If trying to place expression
that is more inclusive into
variable that is less inclusive,
explicit cast must be
performed.
From MORE to LESS
SYNTAX ERRORS, RUNTIME ERRORS, AND LOGIC ERRORS
You learned that there are three categories of errors: syntax errors,
runtime errors, and logic errors.
• Syntax errors arise because the rules of the language have not
been followed. They are detected by the compiler.
• Runtime errors occur while the program is running if the
environment detects an operation that is impossible to carry out.
• Logic errors occur when a program doesn't perform the way it
was intended to.
A CASE STUDY ON “JAVA”
By – Ayush Gupta
Himanshu Tripathi
Neeraj Kr Sahu
Next Topic – Exception Handling

More Related Content

What's hot (20)

Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Java packages
Java packagesJava packages
Java packages
 
Java interface
Java interfaceJava interface
Java interface
 
Introduction to django framework
Introduction to django frameworkIntroduction to django framework
Introduction to django framework
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java notes
Java notesJava notes
Java notes
 
Main method in java
Main method in javaMain method in java
Main method in java
 
Inheritance and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In Java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Java
JavaJava
Java
 
Java applet
Java appletJava applet
Java applet
 

Similar to A Case Study on Java. Java Presentation

Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsRaja Sekhar
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1Sherihan Anver
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and PolymorphismKartikKapgate
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class featuresRakesh Madugula
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP conceptsAhmed Farag
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentalsAnsgarMary
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Conceptsmdfkhan625
 
Java Basics for selenium
Java Basics for seleniumJava Basics for selenium
Java Basics for seleniumapoorvams
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptxKrutikaWankhade1
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptxrayanbabur
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java conceptsChikugehlot
 
Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1PawanMM
 
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov Nayden Gochev
 

Similar to A Case Study on Java. Java Presentation (20)

Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwords
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
 
Introduction to OOP concepts
Introduction to OOP conceptsIntroduction to OOP concepts
Introduction to OOP concepts
 
Java_notes.ppt
Java_notes.pptJava_notes.ppt
Java_notes.ppt
 
Introduction to oop and java fundamentals
Introduction to oop and java fundamentalsIntroduction to oop and java fundamentals
Introduction to oop and java fundamentals
 
Java basics training 1
Java basics training 1Java basics training 1
Java basics training 1
 
Unit 4
Unit 4Unit 4
Unit 4
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
Java Basics for selenium
Java Basics for seleniumJava Basics for selenium
Java Basics for selenium
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 
Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1
 
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
 
Cse java
Cse javaCse java
Cse java
 

Recently uploaded

The solar dynamo begins near the surface
The solar dynamo begins near the surfaceThe solar dynamo begins near the surface
The solar dynamo begins near the surfaceSérgio Sacani
 
INSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere UniversityINSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere UniversitySteffi Friedrichs
 
Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Sérgio Sacani
 
Gliese 12 b, a temperate Earth-sized planet at 12 parsecs discovered with TES...
Gliese 12 b, a temperate Earth-sized planet at 12 parsecs discovered with TES...Gliese 12 b, a temperate Earth-sized planet at 12 parsecs discovered with TES...
Gliese 12 b, a temperate Earth-sized planet at 12 parsecs discovered with TES...Sérgio Sacani
 
Topography and sediments of the floor of the Bay of Bengal
Topography and sediments of the floor of the Bay of BengalTopography and sediments of the floor of the Bay of Bengal
Topography and sediments of the floor of the Bay of BengalMd Hasan Tareq
 
A Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on EarthA Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on EarthSérgio Sacani
 
Plasma proteins_ Dr.Muralinath_Dr.c. kalyan
Plasma proteins_ Dr.Muralinath_Dr.c. kalyanPlasma proteins_ Dr.Muralinath_Dr.c. kalyan
Plasma proteins_ Dr.Muralinath_Dr.c. kalyanmuralinath2
 
Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...
Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...
Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...Sérgio Sacani
 
SCHISTOSOMA HEAMATOBIUM life cycle .pdf
SCHISTOSOMA HEAMATOBIUM life cycle  .pdfSCHISTOSOMA HEAMATOBIUM life cycle  .pdf
SCHISTOSOMA HEAMATOBIUM life cycle .pdfDebdattaGhosh6
 
Aerodynamics. flippatterncn5tm5ttnj6nmnynyppt
Aerodynamics. flippatterncn5tm5ttnj6nmnynypptAerodynamics. flippatterncn5tm5ttnj6nmnynyppt
Aerodynamics. flippatterncn5tm5ttnj6nmnynypptsreddyrahul
 
Cell Immobilization Methods and Applications.pptx
Cell Immobilization Methods and Applications.pptxCell Immobilization Methods and Applications.pptx
Cell Immobilization Methods and Applications.pptxCherry
 
Isolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptxIsolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptxGOWTHAMIM22
 
Mitosis...............................pptx
Mitosis...............................pptxMitosis...............................pptx
Mitosis...............................pptxCherry
 
Structural annotation................pptx
Structural annotation................pptxStructural annotation................pptx
Structural annotation................pptxCherry
 
Microbial bio Synthesis of nanoparticles.pptx
Microbial bio Synthesis of nanoparticles.pptxMicrobial bio Synthesis of nanoparticles.pptx
Microbial bio Synthesis of nanoparticles.pptxCherry
 
Continuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsContinuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsSérgio Sacani
 
mixotrophy in cyanobacteria: a dual nutritional strategy
mixotrophy in cyanobacteria: a dual nutritional strategymixotrophy in cyanobacteria: a dual nutritional strategy
mixotrophy in cyanobacteria: a dual nutritional strategyMansiBishnoi1
 
Gliese 12 b: A Temperate Earth-sized Planet at 12 pc Ideal for Atmospheric Tr...
Gliese 12 b: A Temperate Earth-sized Planet at 12 pc Ideal for Atmospheric Tr...Gliese 12 b: A Temperate Earth-sized Planet at 12 pc Ideal for Atmospheric Tr...
Gliese 12 b: A Temperate Earth-sized Planet at 12 pc Ideal for Atmospheric Tr...Sérgio Sacani
 
GBSN - Microbiology (Unit 7) Microbiology in Everyday Life
GBSN - Microbiology (Unit 7) Microbiology in Everyday LifeGBSN - Microbiology (Unit 7) Microbiology in Everyday Life
GBSN - Microbiology (Unit 7) Microbiology in Everyday LifeAreesha Ahmad
 
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...Sahil Suleman
 

Recently uploaded (20)

The solar dynamo begins near the surface
The solar dynamo begins near the surfaceThe solar dynamo begins near the surface
The solar dynamo begins near the surface
 
INSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere UniversityINSIGHT Partner Profile: Tampere University
INSIGHT Partner Profile: Tampere University
 
Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...Jet reorientation in central galaxies of clusters and groups: insights from V...
Jet reorientation in central galaxies of clusters and groups: insights from V...
 
Gliese 12 b, a temperate Earth-sized planet at 12 parsecs discovered with TES...
Gliese 12 b, a temperate Earth-sized planet at 12 parsecs discovered with TES...Gliese 12 b, a temperate Earth-sized planet at 12 parsecs discovered with TES...
Gliese 12 b, a temperate Earth-sized planet at 12 parsecs discovered with TES...
 
Topography and sediments of the floor of the Bay of Bengal
Topography and sediments of the floor of the Bay of BengalTopography and sediments of the floor of the Bay of Bengal
Topography and sediments of the floor of the Bay of Bengal
 
A Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on EarthA Giant Impact Origin for the First Subduction on Earth
A Giant Impact Origin for the First Subduction on Earth
 
Plasma proteins_ Dr.Muralinath_Dr.c. kalyan
Plasma proteins_ Dr.Muralinath_Dr.c. kalyanPlasma proteins_ Dr.Muralinath_Dr.c. kalyan
Plasma proteins_ Dr.Muralinath_Dr.c. kalyan
 
Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...
Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...
Emergent ribozyme behaviors in oxychlorine brines indicate a unique niche for...
 
SCHISTOSOMA HEAMATOBIUM life cycle .pdf
SCHISTOSOMA HEAMATOBIUM life cycle  .pdfSCHISTOSOMA HEAMATOBIUM life cycle  .pdf
SCHISTOSOMA HEAMATOBIUM life cycle .pdf
 
Aerodynamics. flippatterncn5tm5ttnj6nmnynyppt
Aerodynamics. flippatterncn5tm5ttnj6nmnynypptAerodynamics. flippatterncn5tm5ttnj6nmnynyppt
Aerodynamics. flippatterncn5tm5ttnj6nmnynyppt
 
Cell Immobilization Methods and Applications.pptx
Cell Immobilization Methods and Applications.pptxCell Immobilization Methods and Applications.pptx
Cell Immobilization Methods and Applications.pptx
 
Isolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptxIsolation of AMF by wet sieving and decantation method pptx
Isolation of AMF by wet sieving and decantation method pptx
 
Mitosis...............................pptx
Mitosis...............................pptxMitosis...............................pptx
Mitosis...............................pptx
 
Structural annotation................pptx
Structural annotation................pptxStructural annotation................pptx
Structural annotation................pptx
 
Microbial bio Synthesis of nanoparticles.pptx
Microbial bio Synthesis of nanoparticles.pptxMicrobial bio Synthesis of nanoparticles.pptx
Microbial bio Synthesis of nanoparticles.pptx
 
Continuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discsContinuum emission from within the plunging region of black hole discs
Continuum emission from within the plunging region of black hole discs
 
mixotrophy in cyanobacteria: a dual nutritional strategy
mixotrophy in cyanobacteria: a dual nutritional strategymixotrophy in cyanobacteria: a dual nutritional strategy
mixotrophy in cyanobacteria: a dual nutritional strategy
 
Gliese 12 b: A Temperate Earth-sized Planet at 12 pc Ideal for Atmospheric Tr...
Gliese 12 b: A Temperate Earth-sized Planet at 12 pc Ideal for Atmospheric Tr...Gliese 12 b: A Temperate Earth-sized Planet at 12 pc Ideal for Atmospheric Tr...
Gliese 12 b: A Temperate Earth-sized Planet at 12 pc Ideal for Atmospheric Tr...
 
GBSN - Microbiology (Unit 7) Microbiology in Everyday Life
GBSN - Microbiology (Unit 7) Microbiology in Everyday LifeGBSN - Microbiology (Unit 7) Microbiology in Everyday Life
GBSN - Microbiology (Unit 7) Microbiology in Everyday Life
 
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...
Alternative method of dissolution in-vitro in-vivo correlation and dissolutio...
 

A Case Study on Java. Java Presentation

  • 1. A CASE STUDY ON “JAVA” By – Ayush Gupta Himanshu Tripathi Neeraj Kr Sahu Sub Topics:– • Polymorphism • Encapsulation • Primitive types in Java • Casting in Java • Types of error in Java
  • 2. POLYMORPHISM IN JAVA • Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is derived from 2 Greek words: ‘poly’ and ‘morphs’. • The word "poly" means many and "morphs" means forms. So polymorphism means “many forms”. • There are two types of polymorphism in Java: Compile-time polymorphism and Runtime polymorphism. • We can perform polymorphism in java by ‘method overloading’ and ‘method overriding’. • If you overload a static method in Java, it is the example of compile time polymorphism. • Here, we will focus on runtime polymorphism in java.
  • 3. RUNTIME POLYMORPHISM IN JAVA • Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time. • In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.
  • 4. EXAMPLE OF JAVA RUNTIME POLYMORPHISM • In this example, we are creating two classes Bike and Splendor. • Splendor class extends Bike class and overrides its run ( ) method. We are calling the run method by the reference variable of Parent class. • Since it refers to the subclass object and subclass method overrides the Parent class method, the subclass method is invoked at runtime.
  • 5. EXAMPLE OF JAVA RUNTIME POLYMORPHISM CONT. class Bike { void run(){System.out.println("running"); } } class Splendor extends Bike { void run() { System.out.println("running safely with 60km"); } public static void main(String args[]) { Bike b = new Splendor();//upcasting b.run(); } } Output: running safely with 60km.
  • 6. METHOD OVERRIDING •If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. •In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding. •Usage of Java Method Overriding •Method overriding is used to provide the specific implementation of a method which is already provided by its superclass. •Method overriding is used for runtime polymorphism.
  • 7. METHOD OVERRIDING Class Super { int x=10; void disp() { system.out.println(“Super class” + x); } } Class Test { Public static void main(String ar[] ) { Sub.obj= new Sub(); Obj.disp(); } } Class Sub extends Super { int y=20; void disp() { System.out.println(“Super”+x); System.out.println(“Sub”+y); } } OUTPUT:- Super 10 Sub 20
  • 8. METHOD OVERLOADING • If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. • If we have to perform only one operation, having same name of the methods increases the readability of the program. • Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int , int) for two parameters, and b(int , int , int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs. • Method overloading increases the readability of the program.
  • 9. class DisplayOverloading { public void disp(char c) { System.out.println(c); } public void disp(char c, int num) { System.out.println(c + " "+num); } } class Sample { public static void main(String args[]) { DisplayOverloading obj = new DisplayOverloading(); obj disp('a'); obj disp('a',10); } } Output: a a 10
  • 10. ENCAPSULATION • 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. • We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it. The Java Bean class is the example of a fully encapsulated class.
  • 11. ADVANTAGE OF ENCAPSULATION These are benefits of Java Encapsulation: Data Hiding – It can provide the programmer to hide the inner classes and the user to give access only to the desired codes. It allows the programmer to not allow the user to know how variables and data store. Flexibility – With this, we can make the data as read-only or write-only as we require it to be. Reusability – It allows the user to a programmer to use the program again and again. Testing of the code – Ease of testing becomes easy.
  • 12. PRIMITIVE TYPES • int 4 bytes • short 2 bytes • long 8 bytes • byte 1 byte • float 4 bytes • double 8 bytes • char Unicode encoding (2 bytes) • boolean {true,false} Behaviors is exactly as in C++
  • 13. JAVA PRIMITIVE DATA TYPES Data Type Characteristics Range byte 8 bit signed integer -128 to 127 short 16 bit signed integer -32768 to 32767 int 32 bit signed integer -2,147,483,648 to 2,147,483,647 long 64 bit signed integer -9,223,372,036,854,775,808 to- 9,223,372,036,854,775,807 float 32 bit floating point number + 1.4E-45 to + 3.4028235E+38 double 64 bit floating point number + 4.9E-324 to + 1.7976931348623157E+308 boolean true or false NA, note Java booleans cannot be converted to or from other types char 16 bit, Unicode Unicode character, u0000 to uFFFF Can mix with integer types
  • 14. Constants:- • 37 integer • 37.2 float • 42F float • 0754 integer (octal) • 0xfe integer (hexadecimal) Primitive types - cont.
  • 15. CASTING • Casting is the temporary conversion of a variable from its original data type to some other data type. • With primitive data types if a cast is necessary from a less inclusive data type to a more inclusive data type it is done automatically. int x = 5; double a = 3.5; double b = a * x + a / x; double c = x / 2; • if a cast is necessary from a more inclusive to a less inclusive data type the class must be done explicitly by the programmer failure to do so results in a compile error. double a = 3.5, b = 2.7; int y = (int) a / (int) b; y = (int)( a / b ); y = (int) a / b; //syntax error
  • 16. PRIMITIVE CASTING double float long int short, char byte Outer ring is most inclusive data type. Inner ring is least inclusive. In expressions variables and sub expressions of less inclusive data types are automatically cast to more inclusive. If trying to place expression that is more inclusive into variable that is less inclusive, explicit cast must be performed. From MORE to LESS
  • 17. SYNTAX ERRORS, RUNTIME ERRORS, AND LOGIC ERRORS You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. • Syntax errors arise because the rules of the language have not been followed. They are detected by the compiler. • Runtime errors occur while the program is running if the environment detects an operation that is impossible to carry out. • Logic errors occur when a program doesn't perform the way it was intended to.
  • 18.
  • 19. A CASE STUDY ON “JAVA” By – Ayush Gupta Himanshu Tripathi Neeraj Kr Sahu Next Topic – Exception Handling