Object Oriented Programming is a concept based on class and objects. A Class is like a repository which contains all data members and methods. On the other hand, an Object is an instance of a class which acquires all the properties(data members) and behavior(methods) of the class.
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
-
Abstraction
public abstract class Shape { public abstract void area(int var); // Abstract method } public class Circle extends Shape { int radius; double pi=3.14; public void area(int var) { radius=var; System.out.println("Arear of Circle: "+(pi*radius*radius)); } } public class Square extends Shape { int side; public void area(int var) { side=var; System.out.println("Area of Square: "+side*side); } } public class Result { public static void main(String[] args) { Shape obj=new Square(); obj.area(4); Shape obj1=new Circle(); obj1.area(3); } }
- An abstract class would be declared using ‘abstract’ keyword.
- Creation of object is not possible of an abstract class.
- It may contain data members, methods, abstract methods, constructors.
- Data members can’t be abstract.
- It must be inherited by the subclass(es) using ‘extends’ keyword.
- The subclass should implement each and every abstract method declared in Super Class. Otherwise, subclass would act like abstract class which needs to be extended further.
- An abstract method can be placed within an abstract Class only.
- It would be declared using ‘abstract’ keyword.
- The abstract method should be declared only without any implementation.
- It can be implemented only in the subclass(concrete class) using ‘extends’ or ‘implements’.
- Created by using ‘interface’ keyword.
- All the methods would be abstract by default.
- Interfaces can be implemented by subclass using ‘implements’.
- All the methods should be implemented in subclass(concrete class).
-
Encapsulation
package EncapsulationExample; public class ABCBank { private String BankName="ABC Bank Pvt. Ltd."; private static String BankAddress="Plot No. 6, New Delhi."; protected void setBankAddress(String newAddress) // protected accessifier { BankAddress=newAddress; System.out.println("Address changed."); } public static void getBankAddress() // public accessifier { System.out.println("Bank Address: "+BankAddress); } } package EncapsulationExample; // Access details within package public class AdminUser extends ABCBank { public static void main(String[] args) { // Protected Methods can be accessed by subclass within and outside the package using Inheritance. // Outside the package(Without inheritance) the user can only access public methods. String newAddress="Plot No. 7, New Delhi."; ABCBank obj=new AdminUser(); obj.setBankAddress(newAddress); obj.getBankAddress(); } } package Encapsulation_Use; // Access the protected method outside package. import EncapsulationExample.ABCBank; // Use Encapsulation package for normal Bank user. public class ABCBank_User { public static void main(String[] args) { ABCBank obj=new ABCBank(); obj.getBankAddress(); // User has rights to access only public methods. } }
-
Inheritance
package InheritanceExample; public class CompanyDetails { // Defining Parent Class which have some data members and Methods. String CompanyName="XYZ PVT. Ltd."; String CompanyAddress="Sec-58, New Delhi."; String CompanyPhone="02145879352"; public void displayCompanyDetails() { System.out.println(CompanyName); System.out.println(CompanyAddress); System.out.println(CompanyPhone); } } package InheritanceExample; public class EmployeeInfo extends CompanyDetails { // Defining Child class which extends the Parent, so that we can use methods from Parent class as well. String EmployeeCode="12345"; String EmployeeDept="Sales"; public void displayEmployeeInfo() { System.out.println(EmployeeCode); System.out.println(EmployeeDept); } public static void main(String[] args) { EmployeeInfo obj=new EmployeeInfo(); obj.displayCompanyDetails(); // Method from Parent Class. obj.displayEmployeeInfo(); // Method from Child Class. } }
- To increase the reusability of the code and feature.
- To support runtime polymorphism
1. Single
Class Parent
Class Child extends Parent
2. Multilevel
Class SupParent
Class Parent extends SupParent
Class Child extends Parent
3. Hierarchical
Class Parent
Class Child_2 extends Parent
4. Multiple (Not supported in JAVA)
Class Parent_1
Class Parent_2
Class Child extends Parent_1, Parent_2
5. Hybrid (Not supported in JAVA)
Class SupParent
Class Parent_1 extends SupParent
Class Parent_2 extends SupParent
Class Child extends Class Parent_1, Class Parent_2
To avoid the ambiguity and complexity java doesn’t support Multiple and Hybrid inheritance.
-
Polymorphism
package PolymorphismExample; public class StaticPolymorphism { public void getArea(int side) { System.out.println("Area of Square: "+side*side); } public void getArea(int length,int breadth) { System.out.println("Area of Rectangle: "+length*breadth); } public static void main(String[] args) { StaticPolymorphism obj=new StaticPolymorphism(); obj.getArea(5); obj.getArea(5, 3); } }
package PolymorphismExample; public class DynamicExample { public void area(int side) { System.out.println("Area of Square: "+side*side); } } package PolymorphismExample; public class DynamicExampleResult extends DynamicExample { public void area(int radius) { System.out.println("Area of Circle: "+(3.14*radius)); } public static void main(String[] args) { DynamicExample obj=new DynamicExampleResult(); // Upcasting obj.area(5); // It will call above method mentioned in 'DynamicExampleResult' class. // If we comment the area() method in this class, then it will call area() from Superclass(DynamicExample). } }
Class Parent { /// Lines of code. } Class Child extends Parent { Parent obj=new Child();// Upcasting child object to parent Child objb=new Class Parent()// Error }
Java Basics:
- Basic Java – 1 || Understand Java before start learning JAVA.
- Basic Java – 2 || Variables and Data Types used in JAVA.
- Basic Java – 3 || Understanding Class, Objects, Methods in Java.
- Basic Java – 4 || More on methods(Return Type and Parameters)
- Basic Java – 5 || Methods- Call by Value and Call by Reference in Java.
- Basic Java – 6 || Understanding of Constructor and Destructor in JAVA.
- Basic Java – 7 || Static Variables and Methods.
- Basic Java – 8 || Lets learn about Arrays in Java.
- Basic Java – 9 || Performing multiple operations using Java Operators.
- Basic Java – 10 || Conditions (If and Switch) in JAVA.
- Basic Java – 11 || for and for-each in Java. (Loops Part-1)
- Basic Java – 12 || Alternate looping concepts while and do-while. (Loops Part-2)
- Basic Java – 13 || Decimal values v/s Octal base(8) values in JAVA.
- Basic Java – 14 || Learn about String literals in Java.
- Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).
- Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).
- Basic Java – 17 || Runtime User Input using Console Class (Part-3).
- Basic Java – 18 || Difference between break and continue keywords.
- Basic Java – 19 || Sending Email using Java (Part-1).
- Basic Java – 20 || Sending Email with attachment using Java (Part-2).
- Basic Java – 21 || Stack memory and Heap memory in Java.
- Basic Java – 22 || Let’s learn more about String.
- Basic Java – 23 || String, StringBuffer & StringBuilder in Java.
- Basic Java – 24 || Exception Handling using Try Catch.
Computer Basics:
- Computer Basics -1 || Introduction and Structure of Computer.
- Computer Basics -2 || Types of Computers and Usage.
- Computer Basics -3 || What are the different types of Software?
- Computer Basics -4 || Importance of Operating System(OS).
- Computer Basics -5 || Understanding of Number System.
- Computer Basics -6 || Understanding MS-DOS Commands.
- Computer Basics -7 || Important Features of MS-Word.
- Computer Basics -8 || Let’s learn the usage of MS-Excel.
- Computer Basics -9 || Understand and Implement Data Validation in Excel.
- Computer Basics -10 || How to apply Filter in a data set in Excel?
- Computer Basics -11 || Using Charts in place of Data Tables in Excel.
- Computer Basics -12 || Advantages of PivotCharts over Simple Charts in Excel.
- Computer Basics -13 || Creating pivot charts/tables in Excel.
- Abbreviations to Full-Forms in Computer Basics.
TestNG:
- TestNG – 1 || Introduction and benefits of TestNG Framework.
- TestNG – 2 || Installation process and a sample program of TestNG.
- TestNG – 3 || Create and execute multiple Test Cases.
- TestNG – 4 || Let’s understand @Test Annotation and attributes.
- TestNG – 5 || Understand Assertion in TestNG.
- TestNG – 6 || Use of @BeforeMethod and @AfterMethod.
- TestNG – 7 || Use of @BeforeClass and @AfterClass.
- TestNG – 8 || Creation and execution of Test Suites.
- TestNG – 9 || Let’s move deep into the Test Suites.
- TestNG – 10 || Use @BeforeTest and @AfterTest Annotations.
- TestNG – 11 || Groups attribute with @Test Annotation.
- TestNG – 12 || Use of @BeforeGroups & @AfterGroups.
- TestNG – 13 || Use of @BeforeSuite & @AfterSuite.
- TestNG – 14 || DataProvider annotation & attribute.
- TestNG – 15 || DataProvider with parameters.
- TestNG – 16 || Access data from Excel sheet using DataProvider.
- TestNG – 17 || Passing multiple Parameters in testng xml.
- TestNG – 18 || Multiple Browser and Parallel Execution in TestNG.
- TestNG -19 || Concept of Parallel Execution.
- TestNG – 20 || Run TestNG Program using main() method.
- File Handling | Reading data from word document(.doc or .docx) in JAVA.
- File Handling | Reading data from Excel files (.xls or .xlsx) using JAVA.
- File Handling | Writing data into an Excel(.XLSX or .XLS) File.
- File Handling | Implement formatting in Excel using Java.
- File Handling | Copy existing data from one workbook to another workbook in Java.
- File Handling | Reading data from PDF file using JAVA.
- File Handling || Traverse folders and subfolders in Java.
- File Handling || Reading and Writing data from a text file.
- File Handling || Multiple file creation using template based input data.
- Framework || Simple example of Key Driven Framework using excel sheet in Selenium(JAVA).
- QnA || How to use Constructors in Abstract class?
- QnA | Difference between Integer and int keywords.
- QnA | Can main method be overloaded?
- QnA | How do I reverse a String/Sentence in Java?
- QnA | Perform Multiplication and Division without * or / or % operators.
- QnA | How do I get the default value of data type?
- QnA | How to split String if it contains period symbol (.) in between?
- Different ways to Reverse a String in Java.
- Copy formatting & style of cells from one sheet to another.
- Getting IP address and Hostname using InetAddress Class.
- User inputs via Command Prompt using arguments of main() method of a class.
- Program for List and ArrayList in Java.
- Useful methods and implementation under Scanner Class.
- Swapping two variable values without using any third variable.
- Difference between int x= 10 and y=010 in Java.
- Parameterized Constructors v/s Setter and Getter function in JAVA.
- Override a Static Method.
- Selenium-12 || Select Class to handle drop-down.
- Selenium-11 || Use of Actions and Action Classes.
- Selenium-10 || Taking Screenshots using Selenium
- Selenium-9 || Understanding WebDriver API.
- Selenium-8 || Implementing Wait(s) in Selenium.
- Selenium-7 || Let’s learn to create complex XPath.
- Selenium-6 || XPath is the best way to locate web elements.
- Selenium-5 || Locating web elements using various type of Locators.
- Selenium-4 || Handling multiple web browsers.
- Selenium-3 || First program using Selenium Web Driver.
- Selenium-2 || Let’s learn Selenium IDE.
- Selenium-1 || Understanding Selenium and Selenium WebDriver.
- JIRA Tutorials-2 || Implement Search and Filter on JIRA Issues.
- JIRA Tutorials-1 || Basic understanding of JIRA.
Sir plz write a blog on downcasting.
very good article sir ji….
i really want to learn more from you.
Thanks once again….