Posts

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 ) { age = newAge ;

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 &q

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 Class Variable back into a Sub Class Va

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 Inheritance Concepts

Image
  Inheritance   Java Inheritance (Subclass and Superclass) In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories. subclass  (child) - the class that inherits from another class superclass  (parent) - the class being inherited from To inherit from a class, use the 'extends' keyword. class BMWCar() { tires; engine; lights; doors; break liner; seat; steering wheel; drive() {} reverse() {} break() {} signal light() {} horns() {} } class BMWModernCar() { } Taking the things of one class to another class is called Inheritance. It needs to create a relationship between the two classes. Then the first class is called "Super Class" and the new class is called "Sub Class". GitHub Projects