18CS653 Module 3
18CS653 Module 3
18CS653
MODULE-3
CLASSES,
METHODS,INHERITANCE
TOPICS
Chapter: 06 Introducing Classes
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
3.1.1 GENERAL FORM OF CLASS
Class: Class is defined using the keyword class
Instance Variables : Variables that are defined inside the class are called
Instance variables because each object of the class have their own variables.
Methods : Methods are also called as member functions and code is written
it is not necessary that all the methods should be defined as static and public
It is not necessary that class should have main method . We can define main
type name(parameter-list) {
// body of method
}
Type : specifies the value that the method returns. It can be of any valid data
type.
Name : name specifies the name of the method. It can be any legal identifier
other than those already used by other items within the current scope.
Parameter-list: These are the arguments that are passed to the method which
reveive the values when the methd is called. Parameters are optional. If the
method
has no parameters, then the parameter list will be empty.
Body of the Method: contains the logic part of the program.
3.4.1 ADDING METHOD TO THE CLASS
The below example demonstrate how to add a method to the class.
3.4.1 ASSIGNMENT
3.4.2 RETURNING A VALUE
The below example demonstrate how to add a method to the class that returns
value.
3.4.3 ADDING METHOD TAKES PARAMETER
The below example demonstrate how to add a method to the class with
parameters.
3.5 CONSTRUCTOR
Definition: Constructor is a special member (method) of a class which is used to
initialize the state of an object. It provides the values to the data members at the
time of object creation that is why it is known as constructor.
Characteristics of constructors :
Constructor name is same as the name of the class.
Constructor cannot have any return type not even void
Constructors are called only once when the objects are created using new
keyword.
Constructor cannot be abstract, static, final and synchronized.
Java compiler provides default constructor if you do not have any constructor
in
the class.
Constructor is called default constructor when it does not have any arguments.
EXAMPLE OF CONSTRUCTOR
3.5.1 PARAMETERIZED CONSTRUCTOR
Definition: Constructor with arguments are called parameterized constructor.
3.6 this KEYWORD
this can be used inside any method to refer to the current object. That is, this is
always a reference to the object on which the method was invoked.
3.6.1 INSTANCE VARIABLE HIDING
In Java, if there is a local variable in a method with the same name as the instance
variable, then the local variable hides the instance variable.
3.7 GARBAGE COLLECTION
In java, garbage means unreferenced objects.
Garbage Collection is process of reclaiming the runtime unused memory
automatically. In other words, it is a way to destroy the unused objects.
In order to destroy unused objects, we were using free() function in C language
and delete() in C++. But, in java it is performed automatically. So, java
provides better memory management.
Method Overloading:
Definition: When a class have more than one method with same name and
different parameters then we say that the method is overloaded.
Overloaded method are invoked based on the arguments and the type of
arguments passed in the method call.
Overloading Methods
Example : Overloading Methods
Example : Overloading Methods
3.10.1 Constructor Overloading
Constructor Overloading:
1. Call-by-value
2. Call-by-reference
Like any other data datatype, a method can returns object. For example, in the
following program, the makeTwice( ) method returns an object in which the value
of instance variable is two times than it is in the invoking object.
Example for returning the object from method
3.14 Recursion
Recursion in java is a process in which a method calls itself continuously untill
and unless some specific condition is met. A method in java that calls itself is
called recursive method.
It makes the code compact but complex to understand.
Syntax:
returntype methodname( )
{
//code to be executed
methodname(); //calling same method
}
Program to find factorial of a number using recursion
3.15 Introducing Access Control
In Java, access modifiers (Access Controls) are used to set the accessibility
(visibility) of classes, interfaces, variables, methods, constructors, data members,
and the setter methods.
For example,
class Animal
{
public void method1() {...}
private void method2() {...}
}
default Access Control: If we do not explicitly specify any access modifier for
classes, methods, variables, etc, then by default the default access modifier is
considered.
For example:
TOPICS
Chapter: 08 Inheritance
3.20 Inheritance Basics
3.20.1 Member Access and Inheritance
3.20.2 A Superclass Variable Can Reference a Subclass Object
3.21 Using super
3.21.1 Using super to Call Superclass Constructors
3.21.2 A Second Use for super to access members of super class
3.22 Creating a Multilevel Hierarchy
3.23 When Constructors Are Called
3.24 Method Overriding
3.25 Dynamic Method Dispatch
3.25.1 Why Overridden Methods?
3.25.2 Applying Method Overriding
3.26 Using Abstract Classes
3.27 Using final with Inheritance
3.27.1 Using final to Prevent Overriding
3.27.2 Using final to Prevent Inheritance
3.28 The Object Class
3.20 Inheritance Basics
Inheritance: Inheritance is a mechanism in which one object acquires all the
properties and behaviors of a parent object.
Inheritance allows us to create a new class from the existing class.
When we inherit from an existing class, we can reuse methods and fields of the
parent class and also we can add new methods and fields in current class also.
Parent class: Parent class is also known as super class or Base class.
Child class: Child class is also known as sub class or Derived class.
In the above example the Animal class is having private variable called age.
Although Dog is a subclass it cannot access the private variable called age
3.20.2 Super class variable refer subclass Object
The super class reference variable can hold the sub class object actually, it is
widening in case of objects (Conversion of lower datatype to a higher datatype).
when the super class variable refer to the subclass object . Using the
super class variable we can access only the members of the super class. If we try
to access the members of the subclass then the compiler will give error.
In the below example a super class (Animal class) variable called a is referening
the sub class( Dog) object.
Animal a;
a = new Dog();
Super keyword in java is used to over come the confusion between the superclass
and subclass when there are same methods and variables in both the parent (super
) class and child(sub) class.
A subclass can call a constructor defined by its superclass by use of the following
form of super:
super(arg-list);
Here, arg-list specifies any arguments needed by the constructor in the superclass.
super( ) must always be the first statement executed inside a subclass’
constructor.
The below example demonstrate how super is used to call the super class
constructor.
Example: using super to call constructors of super class
3.21.2 using super to access members of super class
super is used to access the members of the super class when the members name
are same in parent class/ super class and child class/ subclass.
Syntax to access the super class members:
super.member_name;
order is the same whether or not super( ) is used. If super( ) is not used, then the
default or parameterless constructor of each superclass will be executed.
When an overridden method is called within the subclass object it always invoke
the method in the subclass. The method defined by the superclass will be hidden.
Abstract class: If the class is declared with the keyword abstract then the class is
called abstract class. Abstract class will give the provision for the subclasses to
implements the methods in their own.
abstract class Shape
{
int color;
// An abstract function
abstract void draw();
}
3.26 Using Abstract
Abstract class: If the class is declared with the keyword abstract then the class is
called abstract class. Abstract class will give the provision for the subclasses to
implements the methods in their own.
abstract class Shape
{
int color;
// An abstract function
abstract void draw();
}
important observations about abstract classes are as follows:
We can use the final keyword with Inheritance for two things.