Sampath Kumar S Assistant Professor, SECE

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
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!
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
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.,
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 CS2136: Paradigms of Computation Class 14: Static & Final Parameters Overloading, Overriding, and Polymorphism Copyright 2001, 2002, 2003 Michael J.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
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.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Methods and Classes. Method Overloading Two or more methods within the same class that share the same name but different parameters class OverloadDemo.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
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.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Modern Programming Tools And Techniques-I
GC211 Data structure Lecture 3 Sara Alhajjam.
OOP: Encapsulation &Abstraction
Static data members Constructors and Destructors
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.
Chapter 11 Inheritance and Polymorphism
Lecture 17: Polymorphism (Part II)
INHERITANCE IN JAVA.
Programming in Java Text Books :
Modern Programming Tools And Techniques-I Inheritance
OOP’S Concepts in C#.Net
Chapter 9 Inheritance and Polymorphism
CSC 113 Tutorial QUIZ I.
Interface.
An Introduction to Java – Part I, language basics
Sampath Kumar S Assistant Professor, SECE
Interfaces.
Inheritance.
Sampath Kumar S Assistant Professor, SECE
Object Oriented Programming in java
Sampath Kumar S Assistant Professor, SECE
METHOD OVERRIDING in JAVA
S.VIGNESH Assistant Professor/CSE, SECE
Sampath Kumar.S Assistant Professor/IT, SECE
S.VIGNESH Assistant Professor, SECE
Method Overloading in JAVA
Sampath Kumar S Assistant Professor, SECE
Inheritance Inheritance is a fundamental Object Oriented concept
Method Overriding in Java
Sampath Kumar S Assistant Professor, SECE
Graphics Programming - Frames
Inheritance Cse 3rd year.
class PrintOnetoTen { public static void main(String args[]) {
Scope of variables class scopeofvars {
Lecture 18: Polymorphism (Part II)
Final Jim Brucker.
Inheritance and Polymorphism
Sampath Kumar S Assistant Professor, SECE
Chapter 11 Inheritance and Polymorphism Part 1
Sampath Kumar S Assistant Professor, SECE
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:

Sampath Kumar S Assistant Professor, SECE Final Keyword Sampath Kumar S Assistant Professor, SECE

Final Keyword In Java 1/18/2019 The final keyword in java is used to restrict the user. The final keyword can be used in many context. Final can be: variable method 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. Sampath Kumar S, AP

1) final variable 1/18/2019 If you make any variable as final, you cannot change the value of final variable (It will be constant). Sampath Kumar S, AP

Example of final variable 1/18/2019 class Bike{ final int speedLimit=90; //final variable void run(){ speedLimit=400; } public static void main(String args[]){ Bike obj=new Bike(); obj.run(); } Output: Compile Time Error Sampath Kumar S, AP

1/18/2019 2) final method If you make any method as final, you cannot override it. Sampath Kumar S, AP

Example of final method 1/18/2019 Example of final method class Bike{ final void run(){ System.out.println(“Bike Class"); } } class Honda extends Bike{ void run(){ System.out.println(“Honda Class"); public static void main(String args[]){ Honda honda= new Honda(); honda.run(); } Output: Compile Time Error Sampath Kumar S, AP

3) final class If you make any class as final, you cannot extend it. 1/18/2019 3) final class If you make any class as final, you cannot extend it. Sampath Kumar S, AP

1/18/2019 Example of final class final class Bike{ } class Honda extends Bike{ void run(){ System.out.println(" Honda Class "); public static void main(String args[]){ Honda honda= new Honda(); honda.run(); } Output: Compile Time Error Sampath Kumar S, AP

final method inherited? 1/18/2019 final method inherited? class Bike{ final void run(){ System.out.println(“Bike Class"); } class Honda extends Bike{ public static void main(String args[]){ new Honda().run(); } Note: final method can be inherited but it cannot be overridden. Output: Bike Class Sampath Kumar S, AP

Blank or uninitialized final variable 1/18/2019 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. Sampath Kumar S, AP

Example of blank final variable 1/18/2019 Example of blank final variable class Student{ int id; String name; final String PAN_CARD_NUMBER; } Sampath Kumar S, AP

Initialize blank final variable 1/18/2019 Initialize blank final variable class Bike{ final int speedLimit;//blank final variable Bike(){ speedLimit=70; System.out.println(speedLimit); } public static void main(String args[]){ new Bike(); } } Note: Blank final variable can be initialized with in constructor only. Output: 70 Sampath Kumar S, AP

static blank final variable 1/18/2019 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. Sampath Kumar S, AP

Ex. of static blank final variable 1/18/2019 Ex. 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); } Output: 50 Sampath Kumar S, AP

1/18/2019 final parameter If you declare any parameter as final, you cannot change the value of it Sampath Kumar S, AP

Example of final parameter 1/18/2019 Example of final parameter class Bike{ int cube(final int n){ n=n+2; //can't be changed as n is final } public static void main(String args[]){ Bike b=new Bike(); b.cube(5); } } Output: :Compile Time Error Sampath Kumar S, AP

1/18/2019 Sampath Kumar S, AP

1/18/2019 Thank You  Sampath Kumar S, AP