Chapter Three
Chapter Three
3
Static Method
4
Final keyword
• The final keyword in java is used to restrict the user. The java
final keyword can be used in many context. Final can be:
Java final variable
• If you make any variable as final, you cannot change the value of
final variable(It will be constant).
Java final method
• If you make any method as final, you cannot override it.
Java final class
• If you make any class as final, you cannot extend it.
5
this Keyword
• As you know, it is illegal in Java
to declare two local variables
with the same name inside the
same or enclosing scopes.
• Interestingly, you can have local
variables, including formal
parameters to methods, which
overlap with the names of the
class’ instance variables.
• However, when a local variable
has the same name as an instance
variable, the local variable hides
the instance variable
6
Chapter 3: Inheritance and Polymorphism
Inheritance Basics
• Acquiring the properties from one class to another class
• Producing new class from already existing class.
• Reusability of code is main advantage of inheritance
• To inherit a class, you simply incorporate the definition of one class
into another by using the extends keyword.
• The general form of a class declaration that inherits a superclass is
shown here:
7
Inheritance Basics(cont’d…)
• You can only specify one superclass for any subclass that you create.
• Java does not support the inheritance of multiple super classes into a
single subclass.
• You can create a hierarchy of inheritance in which a subclass
becomes a superclass of another subclass. However, no class can be a
superclass of itself.
• Although a subclass includes all of the members of its superclass,
it cannot access those members of the superclass that have been
declared as private.
• A class member that has been declared as private will remain private
to its class.
• It is not accessible by any code outside its class, including subclasses.
8
Using the super Keyword
• The second is used to access a member of the superclass that has been
hidden by a member of a subclass
10
Polymorphism
Overloading Methods
13
Method Overriding(cont’d…)
• 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.
15
Abstract class
17
Interfaces
• using interface, you can specify what a class must do, but not how it
does it.
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
//…
return-type method-nameN(parameter-list);
19
Defining an Interface(cont’d…)
• When it is declared as public, the interface can be used by any other code.
• In this case, the interface must be the only public interface declared in the
file, and the file must have the same name as the interface. name is the name
of the interface, and can be any valid identifier.
• The methods that are declared in interface have no bodies. They end with a
semicolon after the parameter list.
}
21
Implementing Interfaces
• Once an interface has been defined, one or more classes can implement
that interface.
• The general form of a class that includes the implements clause looks
like this:
class classname [implements interface [,interface…]] {
// class-body
22
Casting
• It is about converting one data type into another data type
• Type cast operator is used to convert one data type into
another data type.
• Data type represents the type of the data stored into a variable.
• There are two kinds of data types:
Primitive Data type: Primitive data type represents singular
values.
• e.g.: byte, short, int, long, float, double, char, boolean.
23
Casting(cont’d…)
• Using casting we can convert a primitive data type into another primitive
data type. This is done in two ways, widening and narrowing.
• Widening: Converting a lower data type into higher data type is called
widening.
• e.g.: char ch = 'a';
• int n = (int ) ch;
• e.g.: int n = 12;
• float f = (float) n;
• Narrowing: Converting a higher data type into lower data type is called
narrowing.
• e.g.: int i = 65;
• char ch = (char) i;
• e.g.: float f = 12.5;
• int i = (int) f; 24
Casting(cont’d…)
25