Introduction to OOPs Concept in Java With Example

To understand this better, let’s take an example of Dog.A dog has many states such as Color, name, breed as well as behaviors — Wagging the Tell, Barking, Eating, etc.Way to Create an Object:-‘new’ keyword is used to create new objects.Example :-public class Test{public test(String name)//This constructor has one parameter.{System.out.println(“Hello :” +name);}public static void main(string args[]){// following statement would create an object mytest.Test mytest = new Test(“Anjali”);}}OUTPUT:-Hello: AnjaliClass: A class can be defined as blueprint/template that describes the behavior/states that the object of its type support. The class is a collection of similar type of object. Once you create a class, so you can define a No. of objects.A class can contain the following variable types.a. Local Variable:- Local Variables are defined in methods, constructors or blocks. within the method, the variable will be declared and initialized, and the variable will be destroyed when the method has completed.b. Instance variables:- Instance variables are declared within a class and initialized when the class is instantiated.c. Class variables:- Class variables are declared within a class and outside any method with the static keyword.2. Inheritance:When properties of one class inherited by other class, this concept is known as Inheritance. The concept of inheritance provides code reusability feature. When a class inherits the properties is known as a child class whereas a class whose properties are inherited is known as Parent class.Here is 4 type of Inheritance in Java. These area). Single Inheritance: In this, one class properties inherited by other.Syntax:Class A{}Class B extends Class A{}b). MultilevelInheritance: When a class is extending from a class which also extends from another class, or a class having more than one parent class but at different stages, this type of inheritance is called Multilevel Inheritance.Syntax:Class A{}Class B extends Class A{}Class C extends Class B{}c). HierarchyInheritance: When a parent class having one or more child class, this is known as Hierarchy inheritance.Syntax:Class A{}Class B extends Class A{}Class C extends Class A{}d)..HybridInheritance: This type of inheritance is only achieved through Interfaces..Because this is the combination of multiple and multilevel inheritances and multiple is not supported in java Because of Ambiguity.3..Encapsulation:The Process of wrapping data members and abstraction together in a unit, it is known as Encapsulation..In Java Encapsulation, every class is an example of Encapsulation..In other words, it’s just like capsule medicine.Encapsulation = Data Hiding + AbstractionIt simply means that any component is following abstraction and data hiding then it is also following Encapsulation.How to achieve Encapsulation?1..To achieve encapsulation, provide getter( ) and setter( ) method in the class to set the variables.2..Declaring private to the instance variables of the class.4..Abstraction:Abstraction is the mechanism to hiding the unnecessary things from the users and provide only functionality.There is a two way to achieve Abstraction:1..By Abstract Class — Through “abstract” keyword, we can declare a class is abstract..The abstract class cannot be instantiated.2..By Interface — In an interface, each method is public and abstract, but it does not contain any constructor.ABSTRACT METHODS:-if you want the actual implementation of that method which is determined by the child class, you must declare a class is abstract in a parent class.5.. More details

Leave a Reply