0% found this document useful (0 votes)
1 views

Abstract Class

The document explains the concept of abstraction in Java, highlighting the use of abstract classes and methods to hide implementation details while exposing functionality. It also covers the final keyword, its usage for variables, methods, and classes, and discusses access specifiers that determine method visibility. Additionally, it touches on type casting in Java, detailing both automatic and manual conversions between data types.

Uploaded by

nsrgperumal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Abstract Class

The document explains the concept of abstraction in Java, highlighting the use of abstract classes and methods to hide implementation details while exposing functionality. It also covers the final keyword, its usage for variables, methods, and classes, and discusses access specifiers that determine method visibility. Additionally, it touches on type casting in Java, detailing both automatic and manual conversions between data types.

Uploaded by

nsrgperumal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Abstract class

• Abstraction is a process of hiding the


implementation details and showing only
functionality to the user.
• Another way, it shows only essential things to
the user and hides the internal details
• Abstraction lets you focus on what
the object does instead of how it does it.
Ways to achieve Abstraction
• There are two ways to achieve abstraction in
java
– Abstract class (0 to 100%)
– Interface (100%)
Abstract class in Java
• A class which is declared as abstract is known as
an abstract class.
• It can have abstract and non-abstract methods.
• It needs to be extended and its method
implemented.
• It cannot be instantiated.
• An abstract class must be declared with an
abstract keyword.
• It can have constructors and static methods also.
• It can have final methods which will force the
subclass not to change the body of the method.
abstract class Bike{
abstract void run();
}
class Honda extends Bike
{
void run(){System.out.println("running safely");
}
public static void main(String args[])
{
Bike obj = new Honda();
obj.run();
}
}
Abstract Method in Java
• A method which is declared as abstract and does
not have implementation is known as an abstract
method.
Example of abstract method
• abstract void printStatus();
//no method body and abstract

Rule:
• If you are extending an abstract class that has an
abstract method, you must either provide the
implementation of the method or make this class
abstract.
abstract class Shape
{
abstract void draw();
}
class Rectangle extends Shape
{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
class TestAbstraction1
{
public static void main(String args[])
{
Shape s= new Circle1();
s.draw();
} }
Final Keyword In Java
• The final keyword in java is used to restrict
the user.
• Final can be:
– variable
– method
– Class
1) Java final variable
• If you make any variable as final, you cannot
change the value of final variable (It will be
constant).
Output:Compile Time Error

class Bike{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike obj=new Bike();
obj.run();
}
}
2) Java final method
• If you make any method as final, you cannot override it.
Example of final method
class Bike{
final void run(){System.out.println("running");}
}

class Honda extends Bike{


void run()
{
System.out.println("running safely with 100kmph");
}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
3) Java final class
• If you make any class as final, you cannot extend it.
Example of final class
final class Bike{}
class Honda1 extends Bike{
void run(){
System.out.println("running safely with 100kmph");
}
public static void main(String args[]){
Honda1 honda= new Honda1();
honda.run();
}
}
Is final method inherited?
Yes, final method is inherited but you cannot override
it.
Example:
class Bike{
final void run(){
System.out.println("running...");}
}
class Honda extends Bike{
public static void main(String args[]){
new Honda2().run();
}
}
What is blank or uninitialized final variable?
• A final variable that is not initialized at the
time of declaration is known as blank final
variable.
• If you want to create a variable that is
initialized at the time of creating object and
once initialized may not be changed, it is
useful.
• For example PAN CARD number of an
employee.
• It can be initialized only in constructor.
Access Specifier
• Access specifier or modifier is the access type of the
method.
• It specifies the visibility of the method.
• Java provides four types of access specifier:
• Public: The method is accessible by all classes when we use
public specifier in our application.
• Private: When we use a private access specifier, the method
is accessible only in the classes in which it is defined.
• Protected: When we use protected access specifier, the
method is accessible within the same package or subclasses
in a different package.
• Default: When we do not use any access specifier in the
method declaration, Java uses default access specifier by
default. It is visible only from the same package only.
Type Casting in Java
•In Java, type casting is a method or process that converts
a data type into another data type in both ways manually
and automatically.
•The automatic conversion is done by the compiler and
manual conversion performed by the programmer.
byte -> short -> char -> int -> long -> float -> double

double -> float -> long -> int -> char -> short -> byte

public class NarrowingTypeCastingExample


{
public static void main(String args[])
{
double d = 166.66;

long l = (long)d;

int i = (int)l;

System.out.println("Before conversion: "+d);

System.out.println("After conversion into long type: "+l);

System.out.println("After conversion into int type: "+i);


}
}

You might also like