0% found this document useful (0 votes)
7 views18 pages

Introduction To Course PT5: @garima Sharma, Geu

The document provides an overview of abstract classes and methods in Java, explaining their purpose and rules, including restrictions on instantiation and the requirement for subclasses to implement abstract methods. It also discusses the final keyword, its usage in variables, methods, and classes, and the implications of combining final and abstract. Additionally, the document covers constructors, their types, rules, and the concepts of method overloading and overriding, highlighting their differences and significance in Java programming.
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)
7 views18 pages

Introduction To Course PT5: @garima Sharma, Geu

The document provides an overview of abstract classes and methods in Java, explaining their purpose and rules, including restrictions on instantiation and the requirement for subclasses to implement abstract methods. It also discusses the final keyword, its usage in variables, methods, and classes, and the implications of combining final and abstract. Additionally, the document covers constructors, their types, rules, and the concepts of method overloading and overriding, highlighting their differences and significance in Java programming.
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
You are on page 1/ 18

TCS 608

INTRODUCTION TO COURSE – PT5

@GARIMA SHARMA, GEU 1


Abstract keyword

abstract is a non-access modifier in java applicable for classes,


methods but not variables.

It is used to achieve abstraction which is one of the pillar of


ObjectOriented Programming(OOP).
Abstract Class
The class which is having partial implementation(i.e. not all methods present in the class have
method definition).
To declare a class abstract, use this general form :

Due to their partial implementation, we cannot instantiate abstract classes.


Any subclass of an abstract class must either implement all of the abstract methods in the
super-class, or be declared abstract itself.
Some of the predefined classes in java are abstract.
They depends on their sub-classes to provide complete implementation.
For example, java.lang.Number is an abstract class.
More on Abstract Class
Following are some important observations about abstract classes in Java.
1. An instance of an abstract class can not be created.
2. Constructors are allowed.
3. We can have an abstract class without any abstract method.
4. Abstract classes can not have final methods because when you make a method final you can
not override it but the abstract methods are meant for overriding.
5. We are not allowed to create object for any abstract class.
6. We can define static methods in an abstract class
Abstract Methods
Sometimes, we require just method declaration in super-classes.
This can be achieve by specifying the abstract type modifier.
These methods are sometimes referred to as subclasser responsibility because they have no
implementation specified in the super-class.
Thus, a subclass must override them to provide method definition.
To declare an abstract method, use this general form:

As no method body is present.Any concrete class(i.e. class without abstract keyword) that
extends an abstract class must overrides all the abstract methods of the class.
A few important rules
•Any class that contains one or more abstract methods must also be declared abstract
•The following are various illegal combinations of other modifiers for methods with respect
to abstract modifier :
• final
• abstract native
• abstract synchronized
• abstract static
• abstract private
• abstract strictfp
Final Keyword
The final keyword in java is used to restrict the user.
The java final keyword can be used in many context such as :
1. variable
2. method
3. class
Final Keyword usage
FINAL VARIABLE :
If you make any variable as final, you cannot change the value of final variable.
It will act like a constant.

FINAL METHOD:
If you make any method as final, you cannot override it.

FINAL CLASS:If you make any class as final, you cannot extend it.
Abstract and Final cannot be seen
together why??
In java, you will never see a class or method declared with both final and abstract keywords.
For classes, final is used to prevent inheritance whereas abstract classes depends upon their
child classes for complete implementation.
In cases of methods, final is used to prevent overriding whereas abstract methods needs to be
overridden in sub-classes.
Constructors in Java
• 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 in the memory.
• It is a special type of method which is used to initialize the object.
• Every time an object is created using the new() keyword, ie. at least one constructor is called.
• It calls a default constructor if there is no constructor available in the class.
• In such case, Java compiler provides a default constructor by default.
Two types of Constructors
• No- argument Constructor(Default Constructor)
• Parameterized Constructor
Important Rules for Constructors
1. Constructor name must be the same as its class name.
2. A Constructor must have no explicit return type.
3. A Java constructor cannot be abstract, static, final, and synchronized.
4. We can use access modifiers while declaring a constructor.
5. It controls the object creation. In other words, we can have private, protected, public or
default constructor in Java.
Default Constructor Sample Program
Parametrized
Constructor
Sample
• A constructor which has a specific
number of parameters is called a
parameterized constructor.

• The parameterized constructor is


used to provide different values to
distinct objects.

• However, you can provide the same


values also.
Overriding and Overloading
When two or more methods in the same class have the same name but different
parameters, it’s called Overloading.
When the method signature (name and parameters) are the same in the superclass and
the child class, it’s called Overriding.
Overloading vs Overriding
1.Overriding implements Runtime Polymorphism whereas Overloading implements
Compile time polymorphism.
2.The method Overriding occurs between superclass and subclass. Overloading occurs
between the methods in the same class.
3.Overriding methods have the same signature i.e. same name and method arguments.
Overloaded method names are the same but the parameters are different.
4.With Overloading, the method to call is determined at the compile-time. With overriding,
the method call is determined at the runtime based on the object type.
5.If overriding breaks, it can cause serious issues in our program because the effect will
be visible at runtime. Whereas if overloading breaks, the compile-time error will come
and it’s easy to fix.
Overloading and Overriding Example
Constructing
Overloading
In Java, a constructor is just like a method but
without return type. It can also be overloaded
like Java methods.
Constructor overloading in Java is a technique of
having more than one constructor with different
parameter lists.
They are arranged in a way that each
constructor performs a different task.
They are differentiated by the compiler by the
number of parameters in the list and their types.

You might also like