Skip to main content

Posts

Showing posts with the label OOPS

OOPs 2 JAVA

What is OOPS? Ans:Lets say my college has 10 students and each students will have some property like age, name and roll Number. Now every student have these 3 properties , so what I will do in normal term , I'm creating a student 1 and I'll say student1 have these 3 property ,every time when I'll create a student I'll say every student have these 3 property but this is not a efficient solution, we dont want to add that. Instead of this what we will do will create a seperate class or just like a form and template , okay so student ? so whatever student is, his age will be, his name will be, his roll number will be. Now you want to create another students lets use this template to use these copy. So hamne ek blueprint or template banake rakh diya hai which we have going to create actual student object this is just a formula of a student Like uski property. In object oriented programming will say class , This is a class each of these individual instance is a OOPS.

Generic Method In Oops

public class Generic { public static < T > void printArray ( T a [] ){ for ( int i = 0 ; i < a . length ; i ++){ System . out .println( a [ i ]) ; } } public static void main ( String [] args ){ Integer a [] = new Integer[ 10 ] ; for ( int i = 0 ; i < a . length ; i ++){ a [ i ] = i + 1 ; } printArray ( a ) ; } } this is the way to create Generic Generic will determine data type automatically 

Interface in OOPS

 What is interface ? Ans:an interface is a class with all its functions as abstract . interface A{ public void method(); } class One{ public void method(){ System.out.println("Class One method"); } } class Two extends One implements A{ public void method(){ System.out.println("Class Two method"); } } In this example u can see first class is not completed and second class (two) is completed just bcse we are mention implements  Why to use interface? Ans: Interface ka use tab krte hai jab koi page me hame mandatory feature rkhna hai bina uske complete na ho to us case me interface ka use kiya jata hai.

Abstract Oops

 What is Abstract on OOPS ? Ans:  Java mein abstract class ka use karke aap ek class ko define kar sakte hain jiska object directly create nahi kiya ja sakta, lekin jise aap dusre classes ke liye blueprint ke roop mein istemal kar sakte hain. Yeh abstract classes inheritance ke through ek hierarchy ko define karne mein bhi madad karte hain. Niche ek simple example diya gaya hai: // Abstract class abstract class Shape { // Abstract method abstract void draw() ; // Non-abstract method void display() { System.out.println( "This is a shape." ) ; } } // Concrete class extending abstract class class Circle extends Shape { // Implementing abstract method void draw () { System . out .println( "Drawing a circle" ) ; } } // Concrete class extending abstract class class Rectangle extends Shape { // Implementing abstract method void draw() { System.out.println( "Drawing a rectangle" ) ; } } public cla...

Constructor OOPS in java

What is Constructor ? Ans: Constructor is a special type of method whose name is name is same as per class name. What is Main purpose of use Constructor ? Ans: The main purpose of constructor is initialize the object. Evey java class has a Constructor with default type. A constructor is is automatically called at the time of object creation. a constructor never contain any return-type including voi. 

OOPS Java

package oOps ; import java . util .*; public class StudentUse { public static void main ( String [] args ) { // TODO Auto-generated method stub Scanner scan = new Scanner ( System . in ) ; Student s1 = new Student () ; s1 . name = "Divyanshu" ; s1 . rollNumber = 123 ; Student s2 = new Student () ; System . out . println ( s1 . name ) ; } } Question: What is oops and why to use oops ? Ans: OOPs stands for object oriented programming language, the main purpose of oops is to deal with real world entity using programming language . or memory management . OOPs ek code likhne ka tarika hai OOPs Features : Class - Class ek template hota hai jisme sirf ham logic define krte hai, class koi memory nhi leta hai  object - Kisi ke class ka behaviour object hota hai object memory occupy krta hai. Inheritance - Jitni bar chahe classes ko extend karke reuse kr skte hai. Polymorphism - Ek hi object ka behaviour multiple ho skta hai . E...