Exploring Java: Static Variables, Methods, Final Concepts & Overriding

e content introduction to java n.w
1 / 18
Embed
Share

Dive into the world of Java programming with this comprehensive guide covering static variables, methods, final concepts, and method overriding. Understand the significance of each concept and how they contribute to object-oriented programming in Java.

  • Java Programming
  • Static Variables
  • Final Concepts
  • Method Overriding
  • Object-Oriented

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. E-Content Introduction to Java Shivneet Tripathi Department of Computer Application UIET , CSJM University, Kanpur

  2. Static Variables The static key word is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class. Static variables are also known as class variables. Local variables cannot be declared static.

  3. Static Methods The static key word is used to create methods that will exist independently of any instances created for the class. Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables. Class variables and methods can be accessed using the class name followed by a dot and the name of the variable or method.

  4. final Variables A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to an different object. However the data within the object can be changed. So the state of the object can be changed but not the reference. With variables, the final modifier often is used with static to make the constant a class variable.

  5. Final Methods A final method cannot be overridden by any subclasses. As mentioned previously the final modifier prevents a method from being modified in a subclass. The main intention of making a method final would be that the content of the method should not be changed by any outsider.

  6. Final Classes The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class.

  7. Overriding the Parent Class Method If a class inherits a method from its super class, then there is a chance to override the method provided that it is not marked final. The benefit of overriding is: ability to define a behavior that's specific to the sub class type. Which means a subclass can implement a parent calss method based on its requirement In object oriented terms, overriding means to override the functionality of any existing method To a reference variable of type parent class, an object reference of child class can be stored. During compilation only the syntax is checked and hence no error will be reported. During execution, JVM figures out the object type and would run the method that belongs to that particular object

  8. Rules for Overriding the Parent Class Method The argument list should be exactly the same as that of the overridden method. The return type should be the same or a subtype of the return type declared in the original overridden method in the super class. The access level cannot be more restrictive than the overridden method's access level. For example: if the super class method is declared public then the overridding method in the sub class cannot be either private or public. However the access level can be less restrictive than the overridden method's access level. Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited then it cannot be overridden. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected. An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method. Constructors cannot be overridden.

  9. Class-- objects are createdA class is a group of objects which have common properties. It is a template or blueprint from which. It is a logical entity. It can't be physical. A class in Java can contain: Fields Methods Constructors Blocks Nested class and interface class <class_name>{ field; method; }

  10. Object and Class Example: main within the class class Student{ //defining fields int id; //field or data member or instance variable String name; //creating main method inside the Student class public static void main(String args[]){ //Creating an object or instance Student s1; s1=new Student();//creating an object of Student //Printing values of the object System.out.println(s1.id);//accessing member through reference variable System.out.println(s1.name); } }

  11. Object and Class Example: main outside the class class Student{ int id; String name; } //Creating another class TestStudent1 which contains the main met hod class TestStudent1{ public static void main(String args[]){ Student s1=new Student(); System.out.println(s1.id); System.out.println(s1.name); } }

  12. Private class Employee{ int id; String name; float salary; void insert(int i, String n, float s) { id=i; name=n; salary=s; } void display(){ System.out.print(id); System.out.print(name); } } public class TestEmployee { public static void main(String[] args) { Employee e1=new Employee(); Employee e2=new Employee(); Employee e3=new Employee(); e1.insert(101,"ajeet",45000); e2.insert(102,"irfan",25000); e3.insert(103,"nakul",55000); e1.display(); e2.display(); e3.display(); } }

  13. Fibonacci Series class Fibonacci{ public static void main(String args[]) { int n1=0,n2=1,n3,i,count=10; System.out.print(n1+" "+n2);//printing 0 and 1 for(i=2;i<count;++i) n3=n1+n2; System.out.print(" "+n3); n1=n2; n2=n3; { } } }

  14. Armstrong Number class Armstrong{ public static void main(String[] args) { int c=0,a,temp; int n=153;//It is the number to check arm strong temp=n; while(n>0) { a=n%10; n=n/10; c=c+(a*a*a); } if(temp==c) System.out.println("armstrong number"); else System.out.println("Not armstrong numb er"); } }

  15. Method Overloading in Java If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. If we have to perform only one operation, having same name of the methods increases the readability of the program. Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.

  16. Method Overloading Public class Adder{ int add(int a,int b){ return a+b; } int add(int a,int b,int c){ return a+b+c; } } Public class TestOverloading1{ public static void main(String[] args){ Adder A = new Adder(); System.out.println(A.add(11,11)); System.out.println(A.add(11,11,11)); } }

  17. Method Overriding in Java If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding. Rules for Java Method Overriding The method must have the same name as in the parent class The method must have the same parameter as in the parent class. There must be an IS-A relationship (inheritance)

  18. Method Overriding class Vehicle{ //defining a method void run(){System.out.println("Vehicle is running");} } //Creating a child class class Bike2 extends Vehicle{ //defining the same method as in the parent class void run(){ System.out.println("Bike is running safely"); } public static void main(String args[]){ Bike2 obj = new Bike2();//creating object obj.run();//calling method } }

More Related Content