Posts

Showing posts from April, 2023

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.

Method Overriding & Super Keyword

Image
  Method Overriding Simply,method overriding is changing the body of a method that is coming from super class to a a sub class . It occurs when the sub class has the same method as in the super class. In the " inheritance " article we could see that a method in the super class can be accessed by the sub class. But if the both classes have 2 methods with the same name? What can we do? In such instances,the method in the sub class overrides the method in the super class.That concept can be called as " Method Overriding in JAVA ". In overriding,the methods that are related to the object are executed always. Let me explain it by using the following example. When studying the above code,we can identify the class Student as the super class and the class Child as the sub class. And also,the method learn() is included in both classes. Here we have created an object called " baby " by using the sub class Child. So when we call the method learn() by using the o...