Java Basic day-2


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java Basic day-2

  1. 1. Static Keyword:-By default all the member of class are instance member. Toassociate with it class member we used static key wordInstance member- its represents attribute and behavior of objectClass member- it represents attribute and behavior of class.Simple def-we can call attributes & methods of a class withoutcreating objects.attributes = variables(int a) + Object variable (C1 o; )methods = Constructor + functions + Distructors in C++static data members not duplicate for each Object,act like a global variable.for declaring global variables in C & C++, we haveconcept of pre processor or declare variable outsidethe main methodbut in java we have only static declaration for globalvariables.static data members can access with class name, object& direct also but non static members only accesswith objects.* static can be variables, methods, objects, class& can be block also.* any concept use with class name that is static.static variable:- static variable will have a single copy perclassclassname.var - static variableex-Font.BOLD;static method:- when u want to call a method using class nameinstead of using object of classclassname.methodname() - static methodInteger.parseInt()static objectclassname.objectname - static objectSystem.out
  2. 2. static block: whenever u want to execute a code before executionof main method u can write a code using static blockstatic {} - static block.Qus- how to display welcome msd without using psvm-class Hello{static{System.out.println("Ha Ha Ha Ha He He He ");System.exit(0);}}static class -Inner class can be static, if inner class is static,then we can create the inner class object in Outerclass p s v m without using Outer class object - direct.*** outer class cant be static.public class Sta2{static class Inner{void disp(){System.out.println("I m inside inner class disp");}}public static void main(String[] args){Inner o = new Inner();o.disp();}}KeyWord/*final keyword - final that means final - fixed - cant const in C & can be variables, methods & can be class also.=> if variable is final then cant reassign any value.=> if method is final then cant override in Child class.=> if class is final then cant extends - cant inherit.- final variable String dsn = "jdbc:odbc:erp"; - now U cant change it */class F1{
  3. 3. final int a = 1000;void disp(){// a = 10; // error - cant assign any value to a final var aSystem.out.println("a = "+a);}public static void main(String[] args){F1 o = new F1();o.disp();}}/*super keyword - it is use for calling the super class variables ,methods & Constructors.super means one step up - just inherited class- super class variable calling*/class S{int a = 1000;}class S1 extends S // class S1 inherit the prop of class S{int a = 100;void disp(){int a = 10;System.out.println("a = "+a); // local a - 10System.out.println("a = "+this.a); // currentobject.a -global a - 100System.out.println("a = "+super.a); // super class acalling - 1000}public static void main(String[] args){S1 o = new S1();o.disp();}}this operator - its return the ref of current object. like thispointerin C++. it is for -* where we want to refer the current object.=>* which is the object variable & which is the argument/localvariable thenwe can use this operator.* to call the current class Constructor(one Constructor to anotherConstructorcalling without using new Operator), then we can use thisoperator.
  4. 4. (imp. for interview) , concept only supported by - Java , VC++ &C#.*/class Th2{int a = 100 ;void disp(){int a = 10;System.out.printf("a = %d n",a); // local - a - 10System.out.printf("a = %d n",this.a); // currentobject.a ,global a - 100}public static void main(String aa[]){Th2 o = new Th2();o.disp(); // a = 100}}super keyword - it is use for calling the super class variables ,methods & Constructors.super means one step up - just inherited class- super class variable calling*/class S{int a = 1000;}class S1 extends S // class S1 inherit the prop of class S{int a = 100;void disp(){int a = 10;System.out.println("a = "+a); // local a - 10System.out.println("a = "+this.a); // currentobject.a -global a - 100System.out.println("a = "+super.a); // super class acalling - 1000}public static void main(String[] args){S1 o = new S1();o.disp();}}
  5. 5. OPPS Concept:-ObjectClassEncapsulationInheritanceAbstractionPolymorphismAll pre define type are objectAll user define type are objectQus= Why java is not 100% OOPSAns:-1-everything in java is not consider as a Object. There areprimitive data type such as int , char , Booleans are not object2- all feature of oops language is not fully supported by java Ex:-Multiple inheritanceObject - it is a kind of variable that can hold the ref of a class.Object is the direct ref.C1 o; - in C++C1 o = new C1(); - in JAVAo.disp(); - C1 class disp.Class:- it is collection of object that share similar attribute, operators orrelasionship Class can exist without an object but reverse can notConstructor:- is the method, used for initializing the attribute of class.Attribute= variables + object variableConstructor has some rules & prop -rules -=> Constructor name & class name must be same.
  6. 6. => Constructor no return any value that means not void.prop -=> every class has one Constructor.=> if user does not define any Constructor in a class then defaultConstructor created.=> if class has any own Constructor then default Constructor is notcreated.=> if one class has more then one Constructor then that is called Constructoroverloading.Incapsulation:- Encapsulation is the technique of making the fields in a classprivate and providing access to the fields via public methods. If a field is declaredprivate, it cannot be accessed by anyone outside the class, thereby hiding the fieldswithin the class. For this reason, encapsulation is also referred to as data hidingThe main benefit of encapsulation is the ability to modify our implemented codewithout breaking the code of others who use our code. With this featureEncapsulation gives maintainability, flexibility and extensibility to our code./* */public class EncapTest{private String name;private String idNum;private int age;public int getAge(){return age;}public String getName(){return name;}public String getIdNum(){return idNum;}public void setAge( int newAge){age = newAge;}public void setName(String newName){name = newName;
  7. 7. }public void setIdNum( String newId){idNum = newId;}}////////////// */public class RunEncap{public static void main(String args[]){EncapTest encap = new EncapTest();encap.setName("James");encap.setAge(20);encap.setIdNum("12343ms");System.out.print("Name : " + encap.getName()+" Age : "+ encap.getAge());}}
  8. 8. Inheritance:- is the process by which object of one class acquire the properties ofobject of another class, a class that is inherited is called a super class and the classthat dose the inheriting is called subclass. It is done by using keyword extendstwo most common reason reasons to use inheritance are1- To promote code reuse2- To use polymorphismclass A {void disp(){System.out.println(" i am inside A display");}}class B extends A {/* void disp(){System.out.println(" i am inside B display");}*/public static void main(String[] args) {B o =new B();o.disp();} }
  9. 9. // Overriding:- if parent and child class has one methods with same name & andsame sign( argument and return type) that is called method overriding.class types in java –concrete class abstract class* only concrete methods * concrete + abstract methods.* can create object & inherit * cant create instance, inherit only.abstract class - if one class has one more abstract methods then thatis called an abstract class.Q- we can create an abstract class without any abstract methods or not?.ans - yes , just declare as abstract. but cant create the direct instance.adv - abstract classes used for memory management, memory for methodsprovided by the abstract class.
  10. 10. condition -* all abstract methods of an abstract class must be override in childclass else child class is also a kind of abstract class.future - abstract classes in used when need hierarchy of class like tree structureLimitation:- only single inheritance example:- one class can extends only oneabstract classFor multiple inheritance we use interfaceinterface - it is a kind of pure abstract class.// to create an simple abstract class.abstract class Calc{void sum(int a,int b){System.out.println("Sum is = "+(a+b));}abstract void sub(int a,int b);}save with
  11. 11. //class inherit with an abstract classclass MyCalc extends Calc{void sub(int a,int b) // method of Calc{ System.out.println("Sub is = "+(a-b)); }public static void main(String aa[]) {MyCalc o = new MyCalc();o.sum(100,20);o.sub(100,20);} }Diffrence b/w abstract and interface:-abstract class Interfaceconcrete + abstract methods. only absract methodsusing abstract classes java * usinginterfaces java supportsupport only single inheritanceusing interfaces java supportmultiple inheritance also.Constructor – Yes Constructor – no* all attributes default - normalEx:- int a = 10all attributes default - final& static. Ex:- final static int a = 10;default access modifier – default default is public.
  12. 12. extends keyword. implements keyword.class to class - extendsinterface to interface - extendsinterface to class - implements.Interface:- interface is collection of implicit abstract methods and static finaldata memberAdv:- interface are used for memory management ,Prop:- support multiple inheritanceConditions:- all interface methods must be override in child class else child classis a kind of abstract class .default access modifier in interface is publicvariable declare must be initializeex:- int a; - error;int a = 10; valid// interface cant haveinterface Prob
  13. 13. {// void disp(){} // error// Prob(){} // error// int a; // error// private void disp(); // error// protected void disp(); // error// final void disp(); // error// default in interfaceabstract void disp();public abstract void disp1();final int a = 10;static final int b = 10;public final int v = 10;public static final int c = 10;static int d = 10;}/////////////////interface Calc{void sum(int a,int b);void sub(int a,int b);}////interface Calc1{void multi(int a,int b);}// multiple inheritance with interfaceclass MyCalc1 implements Calc,Calc1{public void sum(int a,int b) // method of Calc{System.out.println("Sum is = "+(a+b));}public void sub(int a,int b) // method of Calc{System.out.println("Sub is = "+(a-b));}public void multi(int a,int b) // method of Calc1{System.out.println("Multi is = "+(a*b));}public static void main(String aa[]){MyCalc1 o = new MyCalc1();o.sum(100,20);o.sub(100,20);o.multi(100,20);}}
  14. 14. Polymorphism: can be implemented in java language in the form ofmultiple methods having same nameType:-1- Compile Time:- is done using method overloading , its alsocalled Early binding.2- Run Time Polymorphism :is done using inheritance. Its also calledLate binding// user define Constructor with Constructor overloadingclass C2{int a,b;C2() // no argument passing Constructor{a = b = 0;}C2(int x) // one argument passing Constructor{a = b = x;}C2(int x,int y) // two argument passing Constructor{a = x;b = y;}void disp(){System.out.printf("a = %d , b = %dn",a,b);}public static void main(String aa[]){C2 o1 = new C2(); // no argument passing Constructor callingC2 o2 = new C2(100); // one argument passing ConstructorcallingC2 o3 = new C2(10,20); // two argument passing Constructorcallingo1.disp(); // a = 0 , b = 0o2.disp(); // a = 100 , b = 100o3.disp(); // a = 10 , b = 20}}