SlideShare a Scribd company logo
11
Most read
12
Most read
17
Most read
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. Java Presentation
A CASE STUDY ON “JAVA”
By – Ayush Gupta
Himanshu Tripathi
Neeraj Kr Sahu
Next Topic – Exception Handling

More Related Content

PPT
JQuery introduction
PPTX
Introduction to java
PDF
Spring Data JPA from 0-100 in 60 minutes
PDF
Java - OOPS and Java Basics
PPTX
Spring Boot and REST API
PPT
Django, What is it, Why is it cool?
PPTX
Computer Science:Java jdbc
PPTX
Java Spring Framework
JQuery introduction
Introduction to java
Spring Data JPA from 0-100 in 60 minutes
Java - OOPS and Java Basics
Spring Boot and REST API
Django, What is it, Why is it cool?
Computer Science:Java jdbc
Java Spring Framework

What's hot (20)

PPTX
Django Girls Tutorial
PPTX
Core java complete ppt(note)
PPTX
Full stack devlopment using django main ppt
PPTX
jQuery
PPT
Eclipse introduction IDE PRESENTATION
PPTX
Java Virtual Machine (JVM), Difference JDK, JRE & JVM
PPTX
Introduction to Spring Boot
PDF
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
PPTX
Intro to Java
PDF
Spring Framework - AOP
PDF
A Basic Django Introduction
PDF
Web Development with Python and Django
PPTX
Introduction to spring boot
PPTX
Applets in java
KEY
Introduction to Django
PPTX
Introduction to java
PPT
Spring Boot in Action
PPTX
Introduction to Spring Framework
PPTX
Training on Core java | PPT Presentation | Shravan Sanidhya
PDF
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Django Girls Tutorial
Core java complete ppt(note)
Full stack devlopment using django main ppt
jQuery
Eclipse introduction IDE PRESENTATION
Java Virtual Machine (JVM), Difference JDK, JRE & JVM
Introduction to Spring Boot
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Intro to Java
Spring Framework - AOP
A Basic Django Introduction
Web Development with Python and Django
Introduction to spring boot
Applets in java
Introduction to Django
Introduction to java
Spring Boot in Action
Introduction to Spring Framework
Training on Core java | PPT Presentation | Shravan Sanidhya
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Ad

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

PPT
Java OOP s concepts and buzzwords
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
PPTX
PPT Lecture-1.4.pptx
PPTX
Java interview questions 1
PPTX
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
PPTX
Inheritance and Polymorphism
PPTX
Basics of Java
PPT
Md06 advance class features
PPTX
Introduction to OOP concepts
PPTX
OOP-JAVA-ONLYUNIT-2-PPT_removed (1).pptx
PPT
Java_notes.ppt
PPTX
Introduction to oop and java fundamentals
PPT
Core Java Concepts
PPT
Java Basics for selenium
PPTX
Super Keyword in Java.pptx
PPTX
PROGRAMMING IN JAVA
PPTX
full defination of final opp.pptx
PPTX
Module--fundamentals and operators in java1.pptx
Java OOP s concepts and buzzwords
JAVA Class Presentation.pdf Vsjsjsnheheh
PPT Lecture-1.4.pptx
Java interview questions 1
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Inheritance and Polymorphism
Basics of Java
Md06 advance class features
Introduction to OOP concepts
OOP-JAVA-ONLYUNIT-2-PPT_removed (1).pptx
Java_notes.ppt
Introduction to oop and java fundamentals
Core Java Concepts
Java Basics for selenium
Super Keyword in Java.pptx
PROGRAMMING IN JAVA
full defination of final opp.pptx
Module--fundamentals and operators in java1.pptx
Ad

Recently uploaded (20)

PDF
Phytochemical Investigation of Miliusa longipes.pdf
PPTX
famous lake in india and its disturibution and importance
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
PDF
Biophysics 2.pdffffffffffffffffffffffffff
PDF
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
PDF
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
PDF
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
PDF
AlphaEarth Foundations and the Satellite Embedding dataset
PPTX
Derivatives of integument scales, beaks, horns,.pptx
PPTX
2. Earth - The Living Planet earth and life
PPTX
microscope-Lecturecjchchchchcuvuvhc.pptx
PPTX
2Systematics of Living Organisms t-.pptx
PPTX
INTRODUCTION TO EVS | Concept of sustainability
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
PPT
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PDF
An interstellar mission to test astrophysical black holes
PDF
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
PDF
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
PPTX
Classification Systems_TAXONOMY_SCIENCE8.pptx
Phytochemical Investigation of Miliusa longipes.pdf
famous lake in india and its disturibution and importance
7. General Toxicologyfor clinical phrmacy.pptx
Biophysics 2.pdffffffffffffffffffffffffff
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
AlphaEarth Foundations and the Satellite Embedding dataset
Derivatives of integument scales, beaks, horns,.pptx
2. Earth - The Living Planet earth and life
microscope-Lecturecjchchchchcuvuvhc.pptx
2Systematics of Living Organisms t-.pptx
INTRODUCTION TO EVS | Concept of sustainability
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
The World of Physical Science, • Labs: Safety Simulation, Measurement Practice
ECG_Course_Presentation د.محمد صقران ppt
An interstellar mission to test astrophysical black holes
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
Classification Systems_TAXONOMY_SCIENCE8.pptx

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.
  • 19. A CASE STUDY ON “JAVA” By – Ayush Gupta Himanshu Tripathi Neeraj Kr Sahu Next Topic – Exception Handling