{"id":615,"date":"2012-07-21T13:26:52","date_gmt":"2012-07-21T13:26:52","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=112"},"modified":"2012-07-21T13:26:52","modified_gmt":"2012-07-21T13:26:52","slug":"java-basics-part1","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/java-basics-part1\/","title":{"rendered":"Java Basics Part1"},"content":{"rendered":"<p><strong>Welcome to Java<\/strong><\/p>\n<pre>\npublic class Sample \n{\t\n  public static void main(String args[])\n  {\n    System.out.println(\"Welcome Back to Java\");\n  }\n}\n<\/pre>\n<p><strong>Assigning Variables<\/strong><\/p>\n<pre>\npublic class Sample \n{\t\n  public String strName;\n\t\n  public static void main(String args[])\n  {\n    Sample1 objSample1 =  new Sample1();\n    objSample1.strName = \"MugilVannan\";\n    System.out.println(objSample1.strName);\n  }\n}\n<\/pre>\n<p>A Class with default access can be seen by class in same package.If classA and classB are in different package and classA has default access classA wont be visible to class B<\/p>\n<p><strong>Class Sample1.java has a class sample3 with default access applied to it.<\/strong><\/p>\n<p><strong>Sample1.java<\/strong><\/p>\n<pre>\npackage pkgSampler;\n\npublic class Sample1 \n{\n\tpublic static void main(String[] args) \n\t{\n\t  SampleA objSampleA =  new SampleA();\n\t  System.out.println(objSampleB.strName);\n\t}\n}\n\nclass SampleA\n{\n\tpublic String strName;\n\t\n\tSampleA()\n\t{\n\t  this.strName = \"Mugil\";\t\n\t}\n}\n\n<\/pre>\n<p><strong>Some Other Class in Same Package Accessing Variable with in Class with Default Access<\/strong><br \/>\n<strong>SampleB.java<\/strong><\/p>\n<pre>\npackage pkgSampler;\n\npublic class SampleB \n{\n  public static void main(String[] args) \n  {\n\tSampleA objSampleA =  new SampleA();\n\tSystem.out.println(objSampleA.strName);\n  }\n}\n\n<\/pre>\n<p>The Methods in Class SampleA with default access in one package will only be visible to SampleB when it is Declared as Public.Otherwise it will compilation error.<\/p>\n<p><strong>Package1 with Class SampleA <\/strong><\/p>\n<pre>\n package Package1;\n class SampleA{}\n<\/pre>\n<p><strong>Package2 with Class SampleB <\/strong><\/p>\n<pre>\n package Package2;\n import Package1.SampleA;\n class SampleB extends SampleA{}\n<\/pre>\n<p>Classes Declared as Final cannot be inherited.Nor its methods and variable can be overridden or extended.<\/p>\n<pre>\n package Pack1;\n public final class SampleA\n {\n  public void Sampler(){}\n }\n<\/pre>\n<p>Now ClassB tries to access classA<\/p>\n<pre>\n package Pack2;\n import Pack1;\n public class SamplerNew extends Sampler{}\n<\/pre>\n<p>The above code will result in error<\/p>\n<p><strong>Abstract Classes<\/strong><br \/>\n1.Abstract classes cant be instantiated.It could be only extended. <\/p>\n<p>2.Methods marked as abstract should end with semicolon at its end.<\/p>\n<p>eg:<\/p>\n<pre>\n  abstract class absFood\n  {\n     public abstract void SouthIndianThali();\n     public abstract void Chinese(); \n  } \n<\/pre>\n<p>3.Even if Single method is Abstract the whole class must be Abstract.You can put non abstract method in abstract class.<\/p>\n<p>4.A Class can&#8217;t be abstract and Final<\/p>\n<p><strong>Interface<\/strong><br \/>\n1.Interface is like contract which says what a class can do without telling how the class is going to do that.<br \/>\n2.Interface are like abstract class but abstract class can have both abstract and non abstract methods whereas interface can have only abstract methods.<\/p>\n<p>3.Other Rules for Interfaces are<br \/>\n  a.Interface Methods Must not be Final<br \/>\n  b.Interface Methods Must not be Static<br \/>\n  c.Variables in Interface should be public, Static and Final.<br \/>\n  d.Methods in Interface are Implicitly Public and Abstract<br \/>\n  e.Interface can only extend other interface<br \/>\n  f.Interface cannot implement another interface.<\/p>\n<p>4.Public modifier is required if you want teh interface to have public rather than default access.<\/p>\n<p>5.All Interfaces methods are Implicitly public and abstract<\/p>\n<p>e.g<\/p>\n<pre>\npublic interface bounceable\n{\n   void Running();\n   void setRunningSpeed(int Speed);\n}\n<\/pre>\n<p>The Interface methods above are public and abstract as it used to be by default.The following applies the same for the below method.<\/p>\n<pre>\nvoid runnable();                      \/\/Public \npublic void runnable();               \/\/Abstract     \nabstract void runnable();             \/\/Public \nabstract public void runnable();    \npublic abstract void runnable();    \n<\/pre>\n<p>The following interface method declaration wont compile <\/p>\n<pre>\nfinal void runnable()         \/\/Interface shld nt be final\nstatic void runnable()        \/\/Interface define instance Methods\nprotected void runnable()     \/\/Interface methods are public  \nprivate void runnable()\n<\/pre>\n<p>Interface constants are <strong>public static final<\/strong>.But there is no need to declare them.<\/p>\n<p>Assigning Constant in Interface<\/p>\n<pre>\n  interface Run\n  {\n    int BAR = 42;          \/\/Public final and Static by Default\n    void go(); \n  }\n\nclass Marathon implements Run\n{\n  public void go()\n  {\n   BAR = 27;             \/\/Not Allowed Interface \n                         \/\/Constant value should not be changed\n  }\n}\n<\/pre>\n<p><strong>Access Modifiers<\/strong><br \/>\nA class can use only two(public, default) of four modifiers.The four modifiers are<br \/>\n  1.Public<br \/>\n  2.protected<br \/>\n  3.default<br \/>\n  4.static <\/p>\n<p><strong>Class Zoo<\/strong><\/p>\n<pre>\nclass Zoo\n{\n  public string coolMethod()\n  { \n     return \"I am Cool\";\n  }\n}\n<\/pre>\n<p><strong>Class Animal<\/strong><\/p>\n<pre> \nclass Animal extends coolMethod\n{\n  public void coolAnimalList()\n  {\n    \/\/The below code works because of inheritance\n    System.out.println(this.coolMethod());\n\n    \/\/The below code works because of Instance Objects\n    coolMethod objcoolMethod = new coolMethod();\n    System.out.println(objcoolMethod.coolMethod());\n  }\n}\n<\/pre>\n<p><strong>Note<\/strong><br \/>\nAlways look for Access level of Class.If the class itself is not accessible no wonder the methods would be.<\/p>\n<p><strong>Protected vs Default<\/strong><br \/>\nProtected and default access control levels are almost identical.A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed(through inheritance)  by a subclass even if subclass in different package.<\/p>\n<p><strong><br \/>\nClass declared as protected in one package can be accessed only through inheritance in another class in other package<br \/>\n<\/strong><\/p>\n<p><strong>Default Access<\/strong><br \/>\nDefault members are visible to subclasses only if those subclasses are in same package as the superclass.<\/p>\n<p><strong>Note <\/strong>: Access modifiers can&#8217;t be applied to local variables<\/p>\n<p>Local Variable can take only one modifier &#8211; <strong>final<\/strong><\/p>\n<p><strong>Final Methods<\/strong><br \/>\nFinal methods prevents methods being overridden in a subclass and is often used to enforce the API functionality of method<\/p>\n<p><strong>Abstract Methods<\/strong><br \/>\nAn abstract method is a method that&#8217;s been declared but not implemented.The method contains no functional code.When a method is marked abstract the subclasses should provide the implementation.<\/p>\n<p>If you declare a abstract method in class then the class should also be abstract. <\/p>\n<p>There are three ways you could tell whether the method is abstract or not.<\/p>\n<ul>\n<li>The method is not marked abstract <\/li>\n<li>The method declaration includes curly braces<\/li>\n<li>The method provides actual implementation code.<\/li>\n<\/ul>\n<p><strong>The First Concrete method of an abstract class must implement all abstract methods of the superclass<\/strong><\/p>\n<p>Concrete means Nonabstract class<\/p>\n<p>eg.<\/p>\n<pre>\npublic abstract class Vehicle\n{\n public abstract goUpHill();\n}\n\npublic abstract class car extends Vehicle\n{\n  public abstract goUpHill();\n\n  public void dpThings()\n  {\n    ...\n    ...\n  }\n}\n\n\/\/First Concrete class since the above two classes are abstract\npublic class Mini extends Car\n{  \n  public void goUpHill()\n  {\n     \n  }\n}\n<\/pre>\n<p><strong>A Abstract Method can Never be marked as both abstract and Final or both abstract and Private<\/strong><\/p>\n<p><strong>Abstract Modifier can never be combined with Static modifier<\/strong><\/p>\n<p>Abstract means the super class does not know anything about how the subclasses should behave in that method where as final means the super class knows every things about the subclasses.<\/p>\n<p><strong>Constructors<\/strong><\/p>\n<p>Every Class has a Constructor, although if you don&#8217;t create one explicitly, the compiler will build one for you.<\/p>\n<p>1.Constructor can&#8217;t ever, ever have return type.<br \/>\n2.Constructors can&#8217;t be marked static, they can&#8217;t be marked final or abstract.<\/p>\n<p><strong>Primitives<\/strong><br \/>\nEight Types &#8211; int, float, long, char, boolean, double, short, byte<\/p>\n<p>For the Integers Small to Big Would be<br \/>\n<strong>byte, short, int, long, and doubles, floats<\/strong><\/p>\n<p>Instance Variables can be any of four access levels, final and transient  <\/p>\n<p>Instance Variables can&#8217;t be  abstract, synchronized, strictfp, native and static<\/p>\n<p><strong>Local Variables<\/strong><br \/>\nLocal Variables are variables declared within a method.Local Variables life begins and Ends within a method.<\/p>\n<p><strong>Local variables are always on the stack and not on the heap<\/strong><\/p>\n<p>Local Variables cannot be public, transient, volatile, abstract, or static but local variables are final.<\/p>\n<p><strong>Shadowing:Declaring a local variable with the same name as instance variable<\/strong><\/p>\n<p><strong>How to Declare a Array in Java<\/strong><br \/>\ne.g<br \/>\nint[] key;  \/\/Recommended<br \/>\nint key []; \/\/Legal but less readable<\/p>\n<p><strong>It is not legal to add size of array in your declaration<\/strong><\/p>\n<p>int[5] names; \/\/The code wont compile<\/p>\n<p><strong>Final Object Reference<\/strong><br \/>\nAn object marked as final can never be reassigned to different object.The data within the object can be modified but the reference variable cannot be changed.<\/p>\n<p><strong>There are no final objects but only final references.<\/strong><\/p>\n<p>1.Final Class Cannot be Subclassed<br \/>\n2.Final Method Cannot be Overridden<br \/>\n3.Final Variable Cannot be assigned a new value.<\/p>\n<p><strong>Transient Variable<\/strong> Skip the variable when you attempt to serialize it.<\/p>\n<p><strong>Volatile Variable<\/strong> Tells the JVM that a thread accessing the variable must always reconcile its private copy of the variable with the master copy in memory.<\/p>\n<p><strong>Static methods and members <\/strong><br \/>\nStatic members exists even before you ever make a new instance of  a class and there will be only one copy of static member regardless of number of instances of that class.In other words all instances of the class share the same value for the given static variable.<\/p>\n<p>Classes, Constructors, Interfaces, Method, inner class methods and instances variables, local variables  cannot be Static.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to Java public class Sample { public static void main(String args[]) { System.out.println(&#8220;Welcome Back to Java&#8221;); } } Assigning Variables public class Sample { public String strName; public static void main(String args[]) { Sample1 objSample1 = new Sample1(); objSample1.strName = &#8220;MugilVannan&#8221;; System.out.println(objSample1.strName); } } A Class with default access can be seen by class&hellip; <a href=\"https:\/\/codethataint.com\/blog\/java-basics-part1\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-615","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=615"}],"version-history":[{"count":0,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/615\/revisions"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}