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

A Case Study on Java. Java Presentation

  • 1.
    A CASE STUDYON “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 INJAVA • 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 JAVARUNTIME 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 JAVARUNTIME 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 { intx=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 • Ifa 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 voiddisp(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 inJava 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 Theseare 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 • int4 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 DATATYPES 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 isthe 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 ringis 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, RUNTIMEERRORS, 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 STUDYON “JAVA” By – Ayush Gupta Himanshu Tripathi Neeraj Kr Sahu Next Topic – Exception Handling