Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 9 Thinking in Objects.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Java Programming static keyword.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
 A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined Data type and is Known as Reference.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Classes - Intermediate
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Topics Instance variables, set and get methods Encapsulation
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Methods and Classes. Method Overloading Two or more methods within the same class that share the same name but different parameters class OverloadDemo.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
BY:- TOPS Technologies
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Staples are our staple Building upon our solution.
Chapter 10 Thinking in Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
Chapter 10 Thinking in Objects
Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind.
using System; namespace Demo01 { class Program
Chapter 10 Thinking in Objects
Interface.
Internet and Java Foundations, Programming and Practice
Lecture 17: Polymorphism (Part II)
INHERITANCE IN JAVA.
CSC 113 Tutorial QUIZ I.
Class Inheritance (Cont.)
Chapter 9 Thinking in Objects
Interface.
Sampath Kumar S Assistant Professor, SECE
Interfaces.
Object Oriented Programming in java
METHOD OVERRIDING in JAVA
Assignment 7 User Defined Classes Part 2
S.VIGNESH Assistant Professor/CSE, SECE
S.VIGNESH Assistant Professor, SECE
Method Overriding in Java
Chapter 9 Thinking in Objects
Inheritance Cse 3rd year.
Sampath Kumar S Assistant Professor, SECE
class PrintOnetoTen { public static void main(String args[]) {
Constructors, GUI’s(Using Swing) and ActionListner
Scope of variables class scopeofvars {
Lecture 18: Polymorphism (Part II)
Inheritance and Polymorphism
Sampath Kumar S Assistant Professor, SECE
An Example of Inheritance
Chapter 9 Objects and Classes
Methods/Functions.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Java Programming Final Keyword In Java

final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context. Final can be: 1.variable 2.method 3.Class

The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword.

final variable If you make any variable as final, you cannot change the value of final variable(It will be constant). class Bike{ final int speedlimit=90;//final variable void run(){ speedlimit=400; } public static void main(String args[]) { Bike obj=new Bike(); obj.run(); } }

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(); }

final class If you make any class as final, you cannot extend it. Example of final class final class Bike{} 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(); } } final class Bike{} 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(); } }

Q) Is final method inherited? Ans) Yes, final method is inherited but you cannot override it. For Example: class Bike{ final void run(){System.out.println("running...");} } class Honda extends Bike{ public static void main(String args[]){ new Honda().run(); } class Bike{ final void run(){System.out.println("running...");} } class Honda extends Bike{ public static void main(String args[]){ new Honda().run(); }

Q) 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.

Example of blank final variable class Student{ int id; String name; final String PAN_CARD_NUMBER;... } class Student{ int id; String name; final String PAN_CARD_NUMBER;... }

Que) Can we initialize blank final variable? Yes, but only in constructor. For example: class Bike{ final int speedlimit;//blank final variable Bike(){ speedlimit=70; System.out.println(speedlimit); } public static void main(String args[]){ new Bike(); } } class Bike{ final int speedlimit;//blank final variable Bike(){ speedlimit=70; System.out.println(speedlimit); } public static void main(String args[]){ new Bike(); } }

static blank final variable A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block. Example of static blank final variable class A{ static final int data;//static blank final variable static{ data=50;} public static void main(String args[]){ System.out.println(A.data); } } class A{ static final int data;//static blank final variable static{ data=50;} public static void main(String args[]){ System.out.println(A.data); } }

Q) What is final parameter? If you declare any parameter as final, you cannot change the value of it. class Bike{ int cube(final int n){ n=n+2;//can't be changed as n is final n*n*n; } public static void main(String args[]){ Bike b=new Bike(); b.cube(5); } } class Bike{ int cube(final int n){ n=n+2;//can't be changed as n is final n*n*n; } public static void main(String args[]){ Bike b=new Bike(); b.cube(5); } }

Q) Can we declare a constructor final? No, because constructor is never inherited.