You are on page 1of 25

Constructors

• In Java a constructor is a block of codes similar to the method. It is


called when an instance of the class is created.
• At the time of calling constructor, memory for the object is allocated.
• It is a special type of method which is used to initialize the object.
• Every time an object is created using the new() keyword, at least one
constructor is called.
• Java compiler calls a default constructor if there is no constructor
available in the class.
Note: It is called constructor because it constructs the values at the
time of object creation.
1
Constructors…

• The following are rules used to define the constructor.


• Constructor name must be the same as its class name
• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static, final, and
synchronized

• Like regular methods, constructors can be overloaded (i.e., multiple


constructors can have the same name but different signatures),
• This make it easy to construct objects with different initial data values.
2
static variable

• If you declare any variable as static, it is known as a static


variable.
• The static variable can be used to refer to the common
property of all objects (which is not unique for each object), for
example, the company name of employees, college name of
students, etc.
• It gets memory only once in the class area at the time of class
loading.
• It makes your program memory efficient (i.e., it saves
memory).

3
Static Method

• Sometimes a method performs tasks that does not depend on an object


• Such methods applies to the class in which it is declared as a whole
and it is known as static methods
• To declare a method as static, place keyword static before the return
type in method declaration.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an
instance of a class.
• A static method can access static data member and can change the
value of it.

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 keyword super refers to the superclass of the class in which


super appears. It can be used in two ways:
• To call a superclass constructor.

• The second is used to access a member of the superclass that has been
hidden by a member of a subclass

• The statement super() invokes the no-arg constructor of its


superclass, and the statement super(arguments) invokes the
superclass constructor that matches the arguments.
9
Using the super Keyword(cont’d…)
• The statement super() or super(arguments) must appear in the
first line of the subclass constructor; this is the only way to
explicitly invoke a superclass constructor
• You must use the keyword super to call the superclass constructor.
Invoking a superclass constructor’s name in a subclass causes a
syntax error.
• A constructor is used to construct an instance of a class.
• Unlike properties and methods, the constructors of a superclass are
not inherited in the subclass.
• They can only be invoked from the constructors of the subclasses, using
the keyword super.

10
Polymorphism

Overloading Methods

• In Java it is possible to define two or more methods within the same


class that share the same name, as long as their parameter
declarations are different.

• When this is happened, the methods are said to be overloaded, and


the process is referred to as method overloading.

• When an overloaded method is invoked, Java uses the type and/or


number of arguments as its guide to determine which version of the
overloaded method to actually call.
11
Overloading Methods(cont’d…)

• Thus, overloaded methods must differ in the type and/or


number of their parameters.

• While overloaded methods may have different return types,


the return type alone is insufficient to distinguish two versions
of a method.

• When Java encounters a call to an overloaded method, it


simply executes the version of the method whose parameters
match the arguments used in the call.
12
Method Overriding

• Overridden methods allow Java to support run-time


polymorphism.

• Polymorphism is essential to object-oriented programming for


one reason:
• it allows a general class to specify methods that will be common to
all of its derivatives, while allowing subclasses to define the specific
implementation of some or all of those methods.

13
Method Overriding(cont’d…)

• Method overriding means when a subclass inherits methods from a


superclass.

• Sometimes it is necessary for the subclass to modify the implementation of


a method defined in the superclass.
• This is referred to as 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.
14
Method Overriding(cont’d…)

• 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.

• 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

• Sometimes you will want to 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.

• One way this situation can occur, is when a superclass is unable to


create a meaningful implementation for a method.

• To declare a class abstract, you simply use the abstract keyword


in front of the class keyword at the beginning of the class
declaration.
16
Abstract class(cont’d…)

• There can be no objects of an abstract class.


• That is, an abstract class cannot be directly instantiated with the
new operator.
• Such objects would be useless, because an abstract class is not fully
defined.
• An abstract method is defined without implementation. Its
implementation is provided by the subclasses. Abstract method is
defined as
• abstract type name(parameter-list);

17
Interfaces
• using interface, you can specify what a class must do, but not how it
does it.

• Once it is defined, any number of classes can implement an interface.

• Also, one class can implement any number of interfaces.

• To implement an interface, a class must create the complete set of


methods defined by the interface.

• However, each class is free to determine the details of its own


implementation
18
Defining an Interface
• An interface is defined much like a class.
• This is a simplified general form of an interface:
access interface name {

return-type method-name1(parameter-list);

return-type method-name2(parameter-list);

type final-varname1 = value;

type final-varname2 = value;

//…

return-type method-nameN(parameter-list);

type final-varnameN = value;

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.

• They are, essentially, abstract methods; there can be no default


implementation of any method specified within an interface. Each class that
includes an interface must implement all of the methods.
20
Interface definition(cont’d…)
• Variables can be declared inside of interface declarations.

• They are implicitly final and static, meaning they cannot be


changed by the implementing class. They must also be initialized.

• All methods and variables are implicitly public.

• Here is an example of an interface definition.


interface Callback {

void callback(int param);

}
21
Implementing Interfaces

• Once an interface has been defined, one or more classes can implement
that interface.

• To implement an interface, include the implements clause in a class


definition, and then create the methods defined by the 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…)

 Referenced Data type: Referenced data type represents multiple


values.
• e.g.: class, String
• Using casting we can convert one class type into another class type if
they are related by means of inheritance.
• Generalization: Moving back from subclass to super class is called
generalization or
• widening or up casting.
• Specialization: Moving from super class to sub class is called
specialization or narrowing or down casting.

25

You might also like