Tuesday, 30 August 2016

Class syntax

Containers in java
Container is java feature, it includes other java programming elements like variables, method, constructor ………………
There are three types of container in java
1.       class
2.       Abstract class
3.       Interface 
Date:  july-14-2016

1.       class
class is a java feature. It able to represent all the real world entities like Student, Employee, Customer, Product, Account ……..
Syntax:-

[Access_modifiers] class class_Name[extends Super_Class_Name][implements Interface_List]

{
        --Variables--
        --Methods--
        --Constructors--
        --blocks--
        --classes--
        --abstract classes--
        --interfaces--
        --enums--
}

There are two types of Access Modifiers in java
1.  To define scopes to the programming elements like variables, methods, classes,
We have to use the following list of Access Modifiers
1.       public
2.       protected
3.       <Default>
4.       private
where “private” members are available up to the respective class where the members are declared.
Where “<default>” member are available up to the current packages not available to outside of the current packages.
Where “protected” member are available up to the present package and up to the child classes available in other packages
Where “public” member are available throughout the application or throughout the system.































From the above access modifiers, only public and <default> access modifiers are allowed in class syntax, but inner classes are able to allow all the above access modifiers.
Note :-
The access  modifiers like private and protected are defined on the bounded of the classes, so that, these access modifiers is not applicable for classes, but, these access modifiers an applicable for members and the classes including inner classes.
2.       To defined some extra nature to the programming elements like variables, methods, classes ,… we have to use the following list of access modifiers.

static
final
abstract
native
volatile
transient
synchronized
strictfp
----
----
From the above list of access modifiers only final, abstract and scriptfp access modifiers are allowed in class syntax, but inner classes are able to allow the access modifiers like static, final, abstract, strictfp
Note:-
In java application, “static” keyword is defined is on the boundaries of the classes, so that, it can not be used for classes, we can use static keyword for members of the classes including inner classes.
Where “class” is a java keyword, it can be used to represent “class” object oriented features and it can be used to declare classes in java application.
Where “Class_Name” is on identifiers to recognized the classes independently.
Where “extends” is a java keyword, it can be used to provide a super class name in the class syntax in order to reuse variable and methods of the specified super class in the present java class .

 Date:  july-15-2016
Note 1:-
In class syntax, “extend” keyword is able to allow only are super class name, it will not allow more then one super class name. if we provide more then our super class name along with extends keyword then it will represented multiple inheritance, it is not possible in java.
Where “implements” is a java keyword, it can be used to specify one or more number of interface names to implements interface methods in the present java class.
Note 2:-
In class syntax, “implements” keyword is able to allow more then one interface name.
Note 3:-
In class syntax, both extends and implements keywords are optional, we can  write a class with out extends keyword and with implemented keyword, we can write a class with out implemented keyword and with extended keyword, we can write a class with out to provide both extends and implement keywords. First we have to provide extends keyword then we have to provide implements keywords.
1. public class A{ }--->Valid
2. public class A{ }--->Invalid
3. class A{ }--->Valid
4. private class A{ }--->Invalid
5. class A{private class B{}}-->Valid
6. class A{public class B{}}---->Valid
7. class A{protected class B{}}-->Valid
8. static class A{}--->Invalid
9. final class A{}---->Valid
10. abstract class A{}--->Valid
11. strictfp class A{}-->Valid
12. class A{static class B{}}--->Valid
13. class A{transient class B{}}-->Invalid
14. class A extends B{}-->Valid
15. class A extends B,C{}--->Invalid
16. class A extends B extends C{}-->Invalid
17. class A implement I{}-->Valid
18. class A implement I1,I2{}-->Valid
19. class A implement I extends B{}-->Invalid
20. class A extends B implement I{}-->Valid
Process to use classes in java application
a.       Declare a class by using “class” keyword.
b.      Declare variable and method as per the application requirements in user defined class.
c.       In main class, in main() method, create object  for the respective class.
d.      Access the members of the respective class by using the generated reference variable.
Ex code:-

class Employee
{
                String eid="E-111";
                String ename="Sai";
                float esal=50000.0f;
                String eadd="Hyderabad";
                String eemail="jntusuperfastupdates@gmail.com";
                String emobile="91-9553667049";
                public void getEmployeeDetails()
                {
                                System.out.println("Employee details");
                                System.out.println("___________________");
                                System.out.println("Employee Id         :"+eid);
                                System.out.println("Employee Name       :"+ename);
                                System.out.println("Employee Salary     :"+esal);
                                System.out.println("Employee Address    :"+eadd);
                                System.out.println("Employee Email      :"+eemail);
                                System.out.println("Employee Mobile     :"+emobile);
                }
}
class Test
{
                public static void main(String[] args)
                {
                                Employee emp=new Employee();
                                emp.getEmployeeDetails();
                }
}

Output:-
Employee details
___________________
Employee Id         :E-111
Employee Name       :Sai
Employee Salary     :50000.0
Employee Address    :Hyderabad
Employee Email      :jntusuperfastupdates@gmail.com
Employee Mobile     :91-9553667049



No comments:

Post a Comment