0% found this document useful (0 votes)
8 views12 pages

OOPs

Hi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

OOPs

Hi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

OOPs

=====
---->object Oriented Programming Systems

concepts:
-------------------
1)object
2)class
3)Inheritance
4)Polymorphism
5)DataAbstraction
6)Encapsulation

1)object
--------------
---->it is a real world entity.
---->Object is defined by instance of the class.

EX:
-----
car,bike,laptop,mobilephone,house,..

properties:
------------------
EX:car
------
state --->color,model,price,size,..
behaviour --->drive,start,stop,...

2)class
---------------
--->collection of an object is called as a class.

class classname
{
//members(variables)
methods()

class Employee
{
int id,salary;
String name;
login()
logout()
}

class--->state+behaviour

Inheritance
-----------------------
Inheritance is a mechanism where a new class is derived from an existing class.
polymorphism
----------------------------
--->if single task is process by different ways.

DataAbstraction
------------------------------
--->It hiding internal details and show only functionality.

Encapsulation
-------------------------
---->Encapsulation is a process of wrapping data into single unit.

I.class and object


-------------------------------
types:
--------------
1)static method
------------------------------
A method as a static in this class,we are called by,
syntax:
------------------
classname.method_name();

EX:
-----
package BasicPrograms;

import java.util.Scanner;

class Besant
{
public static void First()//static method
{
System.out.println("This is Besant class");
}
}
public class BesantClass
{

public static void main(String[] args)


{
Besant.First();//classname.methodname();

2)non-static method
------------------------------------
A method as a non-static in this class,we are called by,

syntax:
------------------
classname obj=new class_name();

obj.methodname();
EX:
----
package BasicPrograms;

import java.util.Scanner;

class Besant
{
public void First()//non-static method
{
System.out.println("This is Besant class");
}
}
public class BesantClass
{

public static void main(String[] args)


{
Besant obj=new Besant();
obj.First();

//Besant.First();
}

}
-----------------------------------------------------------------------------------
-------------
Scanner

nextInt()
nextFloat()

String.join()
-----------------------------------------------------------------------------------
--
Inheritance
---------------------
Inheritance is a mechanism where a new class is derived from an existing class.

syntax:
-------------
class classname1 //parent class,base class,superclass
{

class classname2 extends classname1 //child class,sub class,derived class


{

EX:
-----
package BasicPrograms;
import java.util.Scanner;

class Besant
{
public void First()//non-static method
{
System.out.println("This is Besant class");
}
}

class A
{
public void Second()
{
System.out.println("This is A class");
}
}
public class BesantClass
{

public static void main(String[] args)


{
Besant obj=new Besant();
obj.First();

A obj1=new A();
obj1.Second();

//Besant.First();
}

types:
-------------
1)single Inheritance
2)multilevel Inheritance
3)hierarchical Inheritance
4)multiple Inheritance --->Not supported in java
5)hybrid inheritance---->not supported in java

1)single Inheritance
---------------------------------
A (parent)
|
B (Child)

EX:
----
package BasicPrograms;

import java.util.Scanner;

class Animal
{
public void walk()
{
System.out.println("Animal is Walking..");
}
}

class Bird extends Animal


{
public void fly()
{
System.out.println("A Bird fly in the Sky...");
}
}
public class BesantClass
{

public static void main(String[] args)


{
Bird b1=new Bird();
b1.fly();
b1.walk();

}
-----------------------------------------------------------------------------
Multilevel Inheritance
---------------------------------------
A (Grand Parent)
|
B (Parent)
|
C (Child)
|
D (grand child)

EX:
----
package BasicPrograms;

import java.util.Scanner;

class Book
{
public void Book_details(String Bname)
{
System.out.println("Book name is "+Bname);
}
}

class Author extends Book


{
public void Author_details(String Aname) {
System.out.println("Author name is "+Aname);
}
}

class Price extends Author


{
public void Price_Details(int price)
{
System.out.println("Book Price is "+price);
}
}
public class BesantClass
{

public static void main(String[] args)


{
Price p1=new Price();

p1.Book_details("Java");
p1.Author_details("John");
p1.Price_Details(500);
}

EX:
-----
package BasicPrograms;

import java.util.Scanner;

class Book
{
public void Book_details(String Bname)
{
System.out.println("Book name is "+Bname);
}
}

class Author extends Book


{
public void Author_details(String Aname) {
System.out.println("Author name is "+Aname);
}
}

class Price extends Author


{
public void Price_Details(int price)
{
System.out.println("Book Price is "+price);
}
}
public class BesantClass
{

public static void main(String[] args)


{
String Bn,An;
int p;

Scanner sc=new Scanner(System.in);


System.out.println("Enter the Details");
Bn=sc.nextLine();
An=sc.nextLine();
p=sc.nextInt();
Price p1=new Price();

p1.Book_details(Bn);
p1.Author_details(An);
p1.Price_Details(p);
}

}
----------------------------------------------------------------------
Hierarchical Inheritance
-----------------------------------------
A
|
---------------------------
| | | |
B C D E

Ex:
------
package BasicPrograms;

import java.util.Scanner;

class Shape
{
public void Shape_details()
{
System.out.println("Display the Shape Details");
}
}

class Rectangle extends Shape


{
public void Rect_details(int l,int b)
{
System.out.println("Area of Rectangle is "+(l*b));
}
}

class Triangle extends Shape


{
public void Tri_details(int b1,int h)
{
System.out.println("Area of Triangle is "+(0.5f*b1*h));
}
}

class Circle extends Shape


{
public void circle_details(int r)
{
System.out.println("Area of Circle is "+(3.14f*r*r));
}
}
public class BesantClass
{

public static void main(String[] args)


{
Circle c1=new Circle();
c1.Shape_details();
c1.circle_details(8);
}

}
----------------------------------------------------------------------
Multiple Inheritance
-----------------------------------
A B
| |
----------------
|
C

Hybrid Inheritance
---------------------------------
--->combination of hierarchical Inheritance and multiple Inheritance

A
|
----------------------
| |
B C
| |
--------------------------
|
D

-----------------------------------------------------------------------------------
--------
Polymorphism
---------------------------
Poly --->Many
morphism --->differnt forms

12+3=15

"12"+"3"=123

types
----------
1)compile time polymorphism(or)method overloading
2)Run time Polymorphism(or)method overriding

1)compile time polymorphism(or)method overloading


-----------------------------------------------------------------------------------
--------
--->Same method name but different arguments.
--->it works with in a class
Ex:
------
package BasicPrograms;

import java.util.Scanner;

class Compiletime
{
public void test()
{
System.out.println("No Arguments");
}

public void test(int a)//one arg should be passed


{
System.out.println(a*10);
}

public void test(int a1,int b1)


{
System.out.println(a1+b1);

}
public void test(int x,int y,int z)
{
System.out.println(x+y+z*3);
}
}

public class BesantClass


{

public static void main(String[] args)


{

Compiletime c1=new Compiletime();


c1.test();
c1.test(12);
c1.test(56,3);
c1.test(1, 2, 3);
}

Ex:
-------
package BasicPrograms;

import java.util.Scanner;

class Compiletime
{
public void test()
{
System.out.println("No Arguments");
}

public void test(int a)//one arg should be passed


{
System.out.println(a*10);
}

public void test(float a1)//one arg should be passed but different data type
{
System.out.println(a1);

}
public void test(int x,int y,int z)
{
System.out.println(x+y+z*3);
}
}

public class BesantClass


{

public static void main(String[] args)


{

Compiletime c1=new Compiletime();


c1.test();
c1.test(12);
c1.test(56.6f);
c1.test(1, 2, 3);
}

}
-----------------------------------------------------------------------------------
----------

You might also like