Posts

Encapsulation

Image
Encapsulation is simply "the data protection".  So encapsulation is used to protect data in variables. We can restrict data by using the "private" modifier. And then we can use get and set methods to access those data. As variables are private,we should use public methods to access them. In encapsulation,we access protected variables by using get and set methods. eg :- class Book{     private int number;     private String name;     private String author;     }     class Run{     psvm(String[]args){     Book b = new Book();     b.number=12345;     b.name="harry potter";     b.author="J.K.Rowlin";     }     }     public void setnum(int number) {     this.number=number;     }     public int getnum() {     return number;     }          output - 12345

Variable and Method types in JAVA

Image
  Variable types..... 1) Local variables/method local they are inside a method. eg :- class A {         void a () {         String x ;         }           } 2) Instance variables the variables inside a class. but cannot be inside a method. eg :- class B {          String x ;         } 3) Static variables/class variables Static variables must be in a class. but it cannot be inside a method. eg :- class A {          static String x ;         }            Method types..... 1) Pre - defined methods. - pre built methods in JDK - public static  void main (String[]args) {} - print (); - get (); - set (); 2) User - defined methods. - methods created by the user. -2 types static methods - static void draw () {} non static nethods - void eat () {} / void write () {}   ...

Conditions and Loop

Image
 Conditions..... Java has 3 types of conditional statements. If else else if If a specified condition is true, we use "if" to execute a code block  under it. If the same condition is false, we use "else" to specify a block of code to be executed. And then we can use "else if" to specify a new condition to test if the 1st condition is false. The above conditions return only boolean values.(true/false) eg :- class A{             if(true){                sout ("A"); }             else {                sout ("B"); }          } eg :- class A{             if(false){                sout ("A"); }             else if{                sout ("B"); }           ...

Interface

Image
  What is an interface according to your knowledge ? Is it a thing that a user can see ? or is it the thing we interact when we use various software applications and systems ? But "Interface" in JAVA is totally different from that. Interface is same as a class in JAVA but it is not a class.  We use this concept because we haven't any way to do multiple inheritance. So that we can use "Interfaces" to inherit many classes. Like we use "extends" in classes,we use the keyword "implements" in Interfaces. Create an Interface..... interface Human { } Inherit an Interface..... eg  :- class Human {           }          interface Man{          }          class Student extends Human implements Man{          } eg :- class A{}      interface C{}      interface D{}      interface E{}      class B extends...

Abstraction

Image
What is a meaningless method ? when we inherit sub classes and super classes,we have to create a meaningless method/s to override. meaningless/blank methods haven't a body. It only used to override methods as our wish and we cannot include specific methods inside meaningless methods as it is only used to overriding. We write our codes inside the methods of sub classes according to our necessity after overriding. When we use "Abstraction" ? When we are unable to give specific codes inside a method that is being created in a certain instance(Meaningless methods) ,we use "Abstraction" to keep that type of methods. For that, "Abstract" keyword is used. Keypoints  We use abstraction to keep meaningless methods. Only abstract class can keep abstract methods. Abstract class can contain both abstract methods and non-abstract methods. Objects cannot be created by using abstract classes. Therefore constructors cannot be created. But we can run methods by using t...

Polymorphism

Image
 Polymorphism The process of calling the sub class methods by using the super class reference with the help of Overriding,Inheritance and Casting concepts. After we override a method of a super class inside a subclass,we can do casting and then through the super class reference,we can call the sub class method. We can perform many tasks in different classes by using this concept. This simply means "Many Forms". Assume that there is a company.It stores Biscuits and Cakes seperately.For that we have to develop a system to store them in the correct manner. As Java is a compiler,it executes its codes line by line. Now if we have either cakes or biscuits,the should have to store in their correct places. So for that we can use polymorphism with casting,inheritance and method overriding concepts. We can create seperate objects for those sub classes. Super class is called and then the sub class is executed. This is a simple idea about how we can use polymorphism practically. Let us d...

Casting

Image
Casting Integer Hierarchy long int short char byte Floating Hierarchy   double float According to the above sequence the data types that are in the highest point have the highest power than lowers. Upcasting We can understand upcasting simply as giving the subclass object to the super class variable. In upcasting we cannot access higher types by lower types. But lower types can be accessed by higher types. Downcasting Converting the sub class object added to the super class variable back into sub class variable. This is simply giving the super class variable to the sub class variable. For this,sub class object is used as parameter.