Posts

Showing posts with the label Java

Introducing Java Encapsulation Concept

Image
  Encapsulation Encapsulation  is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as  data hiding . To achieve encapsulation in Java − 1.) Declare the variables of a class as private. 2.) Provide public setter and getter methods to modify and view the variables values 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 ) { a...

Introducing Java Interfaces Concept

Image
Interfaces Another way to achieve Abstraction  in Java, is with interfaces. Is equivalent to a Class. But not a class. interface FirstInterface { public void myMethod ( ) ; // interface method } interface SecondInterface { public void myOtherMethod ( ) ; // interface method } class DemoClass implements FirstInterface , SecondInterface { public void myMethod ( ) { System . out . println ( "Some text.." ) ; } public void myOtherMethod ( ) { System . out . println ( "Some other text..." ) ; } } class Main { public static void main ( String [ ] args ) { DemoClass myObj = new DemoClass ( ) ; myObj . myMethod ( ) ; myObj . myOtherMethod ( ) ; } } Like  abstract classes , interfaces  cannot  be used to create objects (in the example above, it is not possible to create an "Animal" object in the MyMainClass) Interface methods do not have a body - the body is provided by the ...

Introducing Java Abstraction Concept

Image
Abstraction (Abstract Class) "Abstract" keyword is used to keep a method that cannot be given specific code (Meaningless) in a Super Class created according to a certain situation. Since there are several sub classes, the Super Class sets a Meaningless Method to override. 1. To keep Meaningless Method 2. Only an Abstract Class Method can hold an Abstract Method. 3. Abstract Class can hold any Abstract and Non-Abstract method. 4. Object and Constructor cannot be created from an Abstract Class. 5. Subclasses can use Super Keyword and create Method. abstract class Vehicle {  abstract void drive();  abstract void Man;  void Man() {  system.out.println("Man");  } } class Car extends Vehicle {  void drive() {  // car driven code  } } class Bike extends Vehicle {  void drive() {  // bike driven code  } }

Introducing Java Polymorphism Concept

Image
Polymorphism Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance l ets us inherit attributes and methods from another class.   Polymorphism   uses those methods to perform different tasks. This allows us to perform a single action in different ways. After overriding a method of a Super Class in a Sub Class, Upcasting and running the Sub Class Method through the Super Class Reference. class Biscuit extends Products {   void store() {     //store code   } } class Cake extends Products {   void store() {     //store code   } } class Chocolate extends Products {   void store() {     //store code   } } class SystemRun { psvm (String[]args) { Biscuit b = new Biscuit(); Cake c = new Cake(); Chocolate cl = new Chocolate(); 1.b.store(); 2.c.store(); 3.cl.store();   } } GitHub Projects

Introducing Java Upcasting And Downcasting Concepts

Image
  Upcasting And Downcasting Upcasting Upcasting  is a type of object typecasting in which a  child object  is typecasted to a  parent class object . By using the Upcasting, we can easily access the variables and methods of the parent class to the child class. Here, we don't access all the variables and the method. We access only some specified variables and methods of the child class.  Upcasting  is also known as  Generalization  and  Widening. In Casting, it is not possible to access big data from small data. But it is possible to access small data from big data. Putting something in the Super Class (Object, Variable, Method) in the Sub Class (Object, Variable, Method) is called "Upcasting". class A { } class B extends A { } class Test { A a = new A(); B b = new B(); b = a; a = b; } In Upcasting, what is simply done is to give a Sub Class Object to the Super Class Variable. Downcasting Turning the Sub Class Object cast into a Super Clas...

Introducing Java Method Overriding And Super Keyword

Image
Method Overriding Changing the Body of a Method from a Super Class to a Sub Class is "Method Overriding". If subclass (child class) has the same method as declared in the parent class, it is known as  method overriding in Java . In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding. Usage of Java Method Overriding Method overriding is used to provide the specific implementation of a method which is already provided by its superclass. Method overriding is used for runtime polymorphism Rules for Java Method Overriding The method must have the same name as in the parent class The method must have the same parameter as in the parent class. There must be an IS-A relationship (inheritance). In Overriding, only the Method related to the Object is always executed. Super Keyword The "Super Keyword" is used to call the constructor of the Super Class from a Sub Class....

Introducing Java Method Overloading Concepts

Image
 Method Overloading  Exceeding the size of a method is called Method Overloading. With  method overloading , multiple methods can have the same name with different parameters. class World { void countries(int x) { } void countries(string y) { } | Method Signature Method Overloading concept is used to create the same method again and again in the same class. Method Overloading can be done by using or changing parameters. It is called "Method Signature". Consider the following example, which has two methods that add numbers of different type. Instead of defining two methods that should do the same thing, it is better to overload one. GitHub Projects

Introducing Java Parameters, Arguments And Constructors

Image
Parameters And Arguments Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma. int        b     =    7; Integer   Variable      V alu e string str = "Java" ; Object? public static voiid main(string[]args){ } void my height(){ } return type methodname void add Numbers(){ int a = 6; int b = 4; int c = a = b; System.out.print(c); } return type - 9 The parameter is there to keep Input and Output. And the Return Type is there to return the Output. Constructor A Constructor is a Method. But this is a special method. 1. The constructor must be created with the same name as the class. 2. There is no return type. Ex:- class Java { Java () { - Constructor } } A constructor in Java is a  special method  that is used to initialize objects....

Introducing Java Object Creation and Data Types

Image
O bject Creation In Java, an object is created from a class. Only a Class can be converted into an Object. Oracle Corporation Company Oracle = new Oracle(); Class Name - Company Variable Name - oracle Assignment - = Object Generate Keyword - new Parameter List variable - () Line End - ; Data Types Primitive Data Types byte - integer char - integer short - integer int - integer long - integer float - floating point double - floating point Integer Float Binary 1/0 True/False Integer Hierarchy 1.Long 2.int 3.short 4.char 5.byte Floating Hierarchy 1.double 2.float int a = 5; integer long b = 7; a+b; (Any Arithmatic Expression) foat c = 7.8; float double d = 9; d-c; long e = 123456; various float f = 789.76; types e+f; Access is possible if the related Primitive Types are of the same type. If the types are different, it cannot be accessed directly.