How to use super keyword static keyword enum keyword in java

How to use super keyword static keyword enum keyword in java
How to use super keyword static keyword enum keyword in java


How to use super keyword static keyword enum keyword in java

Super keyword: A Super is a keyword that refers to the object of the current base class. It can be used to replace derived class variables with base class level variables. It is also used to call parent/base class method and function. It is also used to call parent/base class overloaded constructor.

Super Keyword Variable: A Super keyword can be replaced by the derived class variable with a base class variable.

How To Use Super Keyword Variable in Java.

class Parameter 

{

 int height=10;

int width=20;

}

 class Rectangle extends Parameter

{

 int height=5;

int width=6;

public void area()

{

int a;

a=height*width;

System.out.println("Area Without Super Keyword:"+a);

area=super.height*super.width;

System.out.println("Area With Super Keyword:"+a);

}

}

 class icoderweb

{

 public static void main(String[] args)

{

 Rectangle obj=new Rectangle();

obj.area();

 }

 }

*****OUTPUT***** 

 Area Without Super Keyword:30

Area With Super:200


Super Keyword Function: Super Keyword can also call the base class function from a derived class.


How To Use Super Keyword With Function in Java?

class Square

{

 void area(int side)

{

int a=side*side;

System.out.println("Area of  Square:"+a);

}

}

 class Rectangle extends Square

{

 int height=10;

int width=5;

public void area()

{

int a;

a=height*width;

System.out.println("Area of  Rectangle:"+a);

super.area(20);

 }

 }

 class icoderweb

{

public static void main(String[] args)

{

Rectangle obj=new Rectangle();

obj.area();

}

}

*****OUTPUT***** 

 Area of  Rectangle:50

Area of  Square:400


Super Keyword Constructor: A Super keyword can also call the constructor of the base class function from the derived class.

How To Use Super Keyword With Constructor in Java?

class Square

{

 Square(int side)

{

int a=side*side;

System.out.println("Area of  Square:"+a);

}

}

class Rectangle extends Square

{

int height=5;

int width=10;

Rectangle()

{

super(20);

 int a;

a=height*width;

System.out.println("Area of  Rectangle:"+a);

}

}

class icoderweb

{

 public static void main(String[] args)

{

Rectangle obj=new Rectangle();

}

}

*****OUTPUT***** 

Area of  Square:400

Area of  Rectangle:50


Static Keyword: A Static keyword is an important element in java that is mainly used for memory management. Static keywords can be used with variable functions and blocks. Static members belong to the class rather than the object of the class so they can be called by class name directly.

👉Polynomials in C++ - Encapsulation, Data Abstraction, and Inheritance

Static Variable Keyword: The variable which is declared with static keyword is called a static variable. It is also called class level variable because it is common to all instances(object). A static variable can be accessed anywhere in the program using the class name.

How To Use Static Variable Keyword in java?

class Rectangle

{

static int height,width,a;

void area()

{

a=height*width;

System.out.println("Area of  Rectangle:"+a);

}

 }

 class icoderweb

{

 public static void main(String[] args)

{

Rectangle.height=20;

Rectangle.width=20;

Rectangle obj=new Rectangle();

obj.area();

}

}

*****OUTPUT***** 

Area of  Rectangle:400


Static Function: A function that is declared with a static keyword is called a static function or method. The static function is also called by class name directly because it belongs to the class rather than an object.

How To Use Static Function in Java?

class Rectangle

{

 static int height,width,a;

 static void area()

{

 a=height*width;

 System.out.println("Area of  Rectangle:"+a);

}

}

class icoderweb

{

public static void main(String[] args)

{

 Rectangle.height=20;

Rectangle.width=20;  

Rectangle.area();

}

}

*****OUTPUT*****  

Area of  Rectangle:400


Static Block: It is such a type of block that executes before the main function.

How To Use Static Block in Java?

class icoderweb         

{

static

{

System.out.println("I am inside static block");

}

public static void main(String[] args)

{

System.out.println("I am inside main function");

}

}

*****OUTPUT***** 

 I am inside static block

I am inside the main function


Enum Keyword:  Enum keyword is a collection of named constants. It is declared with an enum keyword. Each enumeration constant is public, static, and final by default. the constant value can't be changed once it is defined.

👉Python Class Or Object 

It is also called a user-defined data type. Value of enum should be in uppercase letters but not compulsory. the new keyword is not used to create a variable of an enum. it can access the value of enum by using the dot(.) operator.

Syntax of enum in java

enum enum_name { value 1,value 2,value 3,value 4.....,value n }


How To Use Enum Keyword in Java?

enum Language

{

C, C++,JAVA, PYTHON

}

class icoderweb

{

 public static void main(String[] args)

{

Language data = Languange.PYTHON;

System.out.println(“Programming Language:+data);

}

}

*****OUTPUT*****  

Programming Language: PYTHON


How To Use Enum with Class in Java?

class Program

{

 enum Language

{

C,C++, JAVA, PYTHON

}

 public static void main(String[] args)

{

Language data = Language.PYTHON;

System.out.println(“Programming Language:”+data);

}

}

*****OUTPUT***** 

Programming Language:PYTHON


How To Use Enum With Switch in Java?

enum Language

{

 C, C++,JAVA, PYTHON

}

class Program

{

 public static void main(String[] args)

{

Language data = Language.PYTHON;

switch(data)

{

case C:

System.out.println(" C Language Fees:"+3500);

break; case C++:

System.out.println(" C++ Language Fees:"+6000);

break;

case JAVA:

System.out.println("Java Language Fees:"+8000);

break;

case PYTHON:

System.out.println(" Python Language Fees:"+10000);

break;

}

}

}

*****OUTPUT***** 

Programming Language Fees:10000


Enum Constructor: The enum has also a constructor-like class but the enum constructor is called separately for each constant value of the enum.

How To Use Enum With Constructor in Java?

enum Language

{

 C,C++,JAVA, PYTHON;

private Language()

{

 System.out.println("called for: "+this.toString());

}

}

class Program

{

 public static void main(String[] args)

{

Language data = Language.C++;

System.out.println(data);

}

}

*****OUTPUT***** 

called for: C

called for: C++

called for: JAVA

called for: PYTHON C++


Enum Function: We can also define a function inside enum like a normal function and we can call it by using enum variable and dot(.) operator.

How To Use Enum With Function in Java?

enum Language

{

 C,C++,JAVA, PYTHON;

public void fun()

{

 System.out.println("called for: "+this.toString());

}

}

class Program

{

public static void main(String[] args)

{

Language data = Language.JAVA;

data.fun();

}

}

*****OUTPUT***** 

called for: JAVA

 

Enumeration interface: It is a predefined interface. It is used to access and retrieve the data from any collection like vector stack, etc.
👉Python File Handling


There are two main methods of enumeration interface

1. has more elements(): It returns The boolean value true and false. It returns true if the enumeration contains at least one or more than one element otherwise returns false.

2. nextElement():It returns the next element of enumeration.

How To Use NextElement in Java?

import java.util.Vector;

import java.util.Enumeration;

class Program

{

 public static void main(String args[])

{

 Enumeration courses;

Vector courseName = new Vector();

courseName.add("C");

courseName.add("C++");

courseName.add("JAVA");

courseName.add("PYTHON");  

courses = courseName.elements();

while (courses.hasMoreElements())

{

System.out.println(courses.nextElement());

}

}

}

*****OUTPUT***** 

C

C++

JAVA

PYTHON


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.