0% found this document useful (0 votes)
2 views79 pages

Module2 Session 1

The document outlines the core fundamentals of Java, covering data types, variables, operators, control statements, classes, methods, constructors, and inheritance. It explains key concepts such as method overloading, recursion, access control, static members, final variables, and the Object class. Additionally, it discusses abstract classes and the use of the final keyword in relation to inheritance.

Uploaded by

mahimass2508
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views79 pages

Module2 Session 1

The document outlines the core fundamentals of Java, covering data types, variables, operators, control statements, classes, methods, constructors, and inheritance. It explains key concepts such as method overloading, recursion, access control, static members, final variables, and the Object class. Additionally, it discusses abstract classes and the use of the final keyword in relation to inheritance.

Uploaded by

mahimass2508
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 2

Core JAVA Fundamentals


DATA TYPES
• Two types:
– Primitive data types-predefined by the language
and is named by a reserved keyword
–Non-primitive data types- Defined by the user.
Primitive data types
• Eight primitive types of data: byte, short, int, long,
char, float, double, and boolean.
• Integers: whole-valued signed numbers (byte,
short, int, and long).
• Floating-point numbers : numbers with fractional
precision (float and double).
• symbols in a character set, like letters and
numbers (char)
• Boolean: a special type for representing true/false
values (boolean)
Literals
• A constant value in Java is created by using a
literal representation.
– 1. Integer Literals (-, 0,0x)
– 2. Floating-Point Literals 3
– 3. Boolean Literals
– 4. Character Literals
– 5. String Literals
Escape sequences
Variables
• is the basic unit of storage in a Java program.
• defined by the combination of an identifier, a
type, and an optional initializer.
• all variables have a scope, which defines their
visibility, and a lifetime.
– type identifier [ = value ][, identifier [= value ] …]
– Dynamic Initialization
– Scope and Lifetime of Variables
– Type Promotion
Arrays
• One-Dimensional Arrays: type var-name[ ];
• Multidimensional Arrays: int twoD[][] = new
int[4][5];
• Type[ ] var-name;
Operators
• arithmetic,
• bitwise,
• relational and
• logical.
Operator Precedence
Control Statements
• Selection statements: if and switch
• Iteration Statements: for, while, and do-while
• Jump Statements: break, continue, and return
Class Fundamentals
• class is a template for an object, and an object
is an instance of a class
Declaring Objects
• When we create a class, we are creating a new
data type. –We can use this type to declare
objects of that type.
• Obtaining objects of a class is a two-step process.
– First, we must declare a variable of the class
type.
• This variable does not define an object.
• It is simply a variable that can refer to an object.
– Second, we must acquire an actual, physical
copy of the object and assign it to that variable
(using new operator)
Introducing Methods
• The general form of a method:
type name(parameter-list)
{ // body of method }
• Here, type specifies the type of data returned by the
method.
• If the method does not return a value, its return type
must be void.
• Methods that have a return type other than void
return a value to the calling routine using the following
form of the return statement:
return value;
Returning a Value
• a method that takes parameters
Constructor
• The automatic initialization of an object is performed
through the use of a constructor.
• A constructor initializes an object immediately upon
creation.
• It has the same name as the class in which it resides.
• Once defined, the constructor is automatically called
immediately after the object is created, before the
new operator completes.
• Constructor have no return type, not even void
because the implicit return type of a class constructor
is the class type itself.
Contd…

• If there is no constructor in a class, compiler


automatically creates a default constructor.
• Two types of constructors
– Default constructor – has no arguments
– Parameterized constructor –has arguments
(parameters)
this Keyword
• this keyword is to refer to the object that
invoked it.
• this can be used inside any method to refer to
the current object.
• this is always a reference to the object on
which the method was invoked.
• We can use this anywhere a reference to an
object of the current class’ type is permitted.
Method Overloading
• It is possible to define two or more methods with
same name within the same class, but their parameter
declarations should be different.
• This is called method overloading.
• This is a form of polymorphism (many forms)
• Overloaded methods must differ in the type and/or
number of their parameters. (return types is not
significant.)
• When an overloaded method is invoked, Java uses the
type and/or number of arguments to determine which
version of the overloaded method to actually call
Overloading -through automatic type
conversions
• In the previous example when test( ) is called with
an integer argument inside . (Overload, no
matching method is found with int as argument).
• However, Java can automatically convert an
integer into a double, and this conversion can be
used to resolve the call.
• Therefore, when test(int) is not found, Java
elevates int to double and then calls test(double).
Recursion
• Recursion is the process of defining something
in terms of itself.
• A method that calls itself is called recursive
function.
Example
Access Control
• Through encapsulation, we can control what parts
of a program can access the members of a class.
• By controlling access, we can prevent misuse.
• For example, allowing access to data only through
welldefined set of methods, we can prevent the
misuse of that data.
• How a member can be accessed is determined by
the access specifier attached to its declaration.
Access modifiers
• Java’s access modifiers are:
– public - member can be accessed by any other code.
(ACCESSIBLE TO ALL)
– private - member can only be accessed by other
members of its class.
– Protected - applies only when inheritance is involved.
Member can be accessed within the package and by
any of its subclasses.
– Default - When no access specifier is there, then its
access specifier is default.
• It can be accessed within its own package, but cannot be
accessed outside of its package
Example
Text page 143
Consolidation
Static Members
• Normally, a class member must be accessed only in
conjunction with an object of its class.
• It is possible to create a member that can be used by itself,
without reference to a specific instance.
• To create such a member, precede its declaration with the
keyword static.
• When a member is declared static, it can be accessed before
any objects of its class are created, and without reference to
any object.
• It is possible to declare both methods and variables to be
static.
• The most common example of a static member is main( ).
main( ) is declared as static because it must be called before
any objects exist.
Contd….

• Instance variables declared as static are global


variables.
• When objects of its class are declared, separate
copy of a static variable is NOT made.
• All instances(objects) of the class share the same
static variable.
• Methods declared as static(static methods) have
several restrictions:
– static methods can only call other static methods.
– static methods must only access static data.
– static methods cannot refer to this or super.
Contd….

• If we need to do computation to initialize your


static variables, we can declare a static block that
gets executed exactly once, when the class is first
loaded.
• if we want to call a static method from outside its
class, we can do so using the following general
form:
[Link]( );
• Here classname is the name of the class in which
the static method is declared.
Static method invocation
page 145
Non static method invocation
Final Variables
• The final variables cannot be modified
• A variable can be declared as final by prefixing final
keyword.
• Can do in two ways:
– First, you can give it a value when it is declared.
– Second, you can assign it a value within a constructor.
– The first approach is the most common
• We can use final variables as if they were constants,
without fear that a value has been changed.
• Variables declared as final do not occupy memory on a per
instance basis.
E.g. final int FILE_NEW = 1; (CAPITAL LETTERS) for final variables
final int FILE_OPEN = 2;
Arrays Revisited
• They are implemented as objects.
• The size of an array—that is, the number of
elements that an array can hold.
• Found in its length instance variable –
member instance.
• All arrays have this variable, and it will always
hold the size of the array.
• PAGE 149
Example
Nested Classes
String Class
• String is probably the most commonly used class in
Java’s class library.
• Every string you create is actually an object of type
String.
• Objects of type String are immutable; once a String
object is created, its contents cannot be altered.
• To change a string, you can always create a new one
that contains the modifications.
• Java defines a peer class of String, called StringBuffer,
which allows strings to be altered.
• Java defines one operator for String objects: + - to
concatenate two strings.
Page 153
Command line arguments
Variable length arguments
• Inheritance
• Super class
• Sub class
• the keywords super
• protected Members
Inheritance
• Inheritance -is one of the important features of
object-oriented programming because it allows the
creation of hierarchical classifications.
• Using inheritance, you can create a general class that
defines characteristics common to a set of related
items.
• This class can then be inherited by other, more specific
classes, each adding those things that are unique to it.
• A class that is inherited is called a superclass and the
class that does the inheriting is called a subclass.
• It inherits all of the members defined by the superclass
and adds its own, unique elements.
Contd…

• To inherit a class, we have to use extends keyword along


with subclass definition.
• The general form of a class declaration that inherits a
superclass is –
class subclass-name extends superclass-name { // body
of class }
• EXAMPLE PAGE 161
• Java does not support the inheritance of multiple
superclasses into a single subclass.
– can only specify one superclass for any subclass
– can create a hierarchy of inheritance in which a subclass
becomes a superclass of another subclass.
– no class can be a superclass of itself.
Member Access and Inheritance
Major Concepts
Superclass Variable Can Reference a
Subclass Object page 166
Using super
Using super to Call Superclass
Constructors page 167
Second Use for super page 170
Creating a Multilevel Hierarchy
• Can build hierarchies that contain as many
layers of inheritance as you like.
• It is perfectly acceptable to use a subclass as a
superclass of another.
• For example, given three classes called A, B,
and C, C can be a subclass of B, which is a
subclass of A
• Example at: Page 172
Protected members
When Constructors Are Called
• In a class hierarchy, constructors are called in order of
derivation, from superclass to subclass.
• When subclass object is created, it first calls superclass
constructor then only it calls subclass constructor.
• If super( ) is not used to call superclass constructor,
then the default constructor of each superclass will be
executed before executing subclass constructors.
• Example: Page 175
• A superclass has no knowledge of any subclass, any
initialization it needs to perform is separate from and
possibly prerequisite to any initialization performed by
the subclass. Therefore, it must be executed first.
Method Overriding
• In a class hierarchy, when a method in a subclass
has the same name and type signature as a
method in its superclass, then the method in the
subclass is said to override the method in the
superclass.
• When an overridden method is called from within
its subclass, it will always refer to the version of
that method defined by the subclass.
• The version of the method defined by the
superclass will be hidden.
• Page 176
• To access the superclass version of an overridden
method, we can do using super keyword.
• Page 176
• Method overriding occurs only when the names and
the type signatures of the two methods are identical.
• If they are not, then the two methods are simply
overloaded.
• Page 177
• Overridden methods allow Java to support run-time
polymorphism.
The Object Class
• There is one special class, Object, defined by
Java.
• All other classes are subclasses of Object.
• That is, Object is a superclass of all other
classes.
• Reference variable of type Object can refer to
an object of any other class.
Methods in Object class
• The methods getClass( ), notify( ), notifyAll( ), and
wait() are declared as final.
• The equals( ) method compares the contents of two
objects.
– It returns true if the objects are equivalent, and false
otherwise.
• The toString( ) method returns a string that contains a
description of the object on which it is called.
– This method is automatically called when an object is
output using println( ).
– Many classes override this method.
Using Abstract Classes
• There are situations in which we want to define a superclass
that declares the structure of a given abstraction without
providing a complete implementation of every method.
• That is, create a superclass that only defines a generalized
form that will be shared by all of its subclasses, leaving it to
each subclass to fill in the details.
• Such a class determines the nature of the methods that the
subclasses must implement.
• One way this situation can occur is when a superclass is
unable to create a meaningful implementation for a
method.
Contd….

• If we want some way to ensure that a subclass should


override all necessary methods then we can make
them abstract methods.
• For making a method an abstract method we have use
abstract type modifier.
• These methods are referred to as subclasser
responsibility because they have no implementation
specified in the superclass.
• Thus, a subclass must override them—it cannot simply
use the version defined in the superclass.
• To declare an abstract method, use this general form:
abstract type name(parameter-list);
Contd….

• Abstract function has no body in superclass.


• Any class that contains one or more abstract methods must
also be declared abstract
• Abstract class can have non abstract methods(concrete
methods) also.
• To declare a class abstract, use the abstract keyword in
front of the class keyword at the beginning of the class
declaration.
abstract class classname
{

}
Using final with Inheritance
• The keyword final has three uses.
– First, it can be used to create the equivalent of a
named constant.
– The other two uses of final apply to inheritance.
• Using final to Prevent Overriding
• Using final to Prevent Inheritance
Using final to Prevent Overriding
• To prevent a method from being overridden,
specify final as a modifier at the start of its
declaration.
• Methods declared as final cannot be
overridden.
• If you attempt to do so, a compile-time error
will result/
• Example of final, Page 184, 185
Using final to Prevent Inheritance
• To prevent a class from being inherited,
precede the class declaration with final.
• Declaring a class as final implicitly declares all
of its methods as final.
• It is illegal to declare a class as both abstract
and final since an abstract class is incomplete
by itself and relies upon its subclasses to
provide complete implementations.

You might also like