0% found this document useful (0 votes)
24 views

Core Java Practial

The document discusses object-oriented programming concepts like classes, objects, methods, constructors, inheritance, polymorphism etc. in Java. It provides code examples to demonstrate class definition, object creation, parameterised and non-parameterised constructors, method overloading and the this keyword.

Uploaded by

Jalisa Firfire
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Core Java Practial

The document discusses object-oriented programming concepts like classes, objects, methods, constructors, inheritance, polymorphism etc. in Java. It provides code examples to demonstrate class definition, object creation, parameterised and non-parameterised constructors, method overloading and the this keyword.

Uploaded by

Jalisa Firfire
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

if we need stud data so we derive class and it sholud hav data n methods

___________________________________________________

1) package com.oops;

public class Stud {

int StudentId;
String StudentName;
String StudentCity;
// above are Data: data members or instance variable.

//now in below let c function or can call behaviour of data


public void study(){
System.out.println(StudentName+ "is studying");

}
public void ShowFullDetails()
{
System.out.println("My name is " +StudentName);
System.out.println("MY ID IS" +StudentId);
System.out.println("MY CITY IS" +StudentCity);
}

************************************************
WITH THE HELP OF ABOVE WE CAN CREATE THE MANY OBJECTS ABOVE ARE (LIKE TEMPLATE)
*********************************************************************************

package com.oops;

public class OBJECTDEMO {


public static void main(String[] args) {
// now creating Object of stud
Stud st1; // st1 is like container which is
used to store
object reference the data
st1 =new Stud(); // we used "new"
keyword to create
object memory
st1.StudentName= " navu "; // if we
put st1. then we are
able to get all stud lass function
like st1.city

st1.StudentId=123;
st1.StudentCity="raj";
st1.study(); //if we run is get output where we mention in stud class
st1.ShowFullDetails();
System.out.println("*******************************");
Stud st2= new Stud(); //with the help of
object(like //
st1,st2,st3...) we can create many stud info

st2.StudentName=" khushi ";


st2.StudentId = 112;
st2.StudentCity = " MHA ";
st2.study();
st2.ShowFullDetails();

___________________________________________________________________________________
_______________----

2) BANKING info
**************************************************************
public class Bank {
long CustACNOL;
String CustName;
double CustBalance;

public void custASKING() {


System.out.println("customer asking about balance" + CustBalance);

}
public void Custdetails(){
System.out.println("my name is" +CustName);
System.out.println(" my ACCOUNT NO is " +CustACNOL);
System.out.println( " my balnce is " +CustBalance);
}

___________________________________________________________________________________
___________________

public class OBJECT {


public static void main(String[] args) {
Bank B1 =new Bank();
B1.CustName = " RAVEENA";
B1.CustACNOL = 1234875121212121212L;
B1.CustBalance = 55525454545454.5445454454D;
B1.custASKING();
B1.Custdetails();
}

}
___________________________________________________________________________________
________________

// constructor is used to initialize / create the object


// constructor is used to initialize the data object
// constructor is similar to object but not same
// const name == class name is rule
//constructor does not return any value not even void
/* public stud() non parameterize const
public stud (int a,int b) // parameterize
when java create automatically- default constructor
**if class doesnot have any constructor thab java creates default const

*/
____________________________________________________________--
3) package com.oops;

public class Stud {

int StudentId;
String StudentName;
String StudentCity;
public Stud()
{
System.out.println("creating object"); // pass const run it
}

public void study(){


System.out.println(StudentName+ "is studying");

}
public void ShowFullDetails()
{
System.out.println("My name is " +StudentName);
System.out.println("MY ID IS" +StudentId);
System.out.println("MY CITY IS" +StudentCity);
}

}
___________________________________________________________________________________
_
constructor overloading
defining multiple const within a same class called const overloading

*******************************************************************
package com.oops;

public class Stud {

int StudentId;
String StudentName;
String StudentCity;
public Stud()
{
System.out.println("creating object");
}
public Stud(int st1){
System.out.println("parameterize const");
}

public void study(){


System.out.println(StudentName+ "is studying");

}
public void ShowFullDetails()
{
System.out.println("My name is " +StudentName);
System.out.println("MY ID IS" +StudentId);
System.out.println("MY CITY IS" +StudentCity);
}

}
______________________________________________________________________________
package com.oops;

public class OBJECTDEMO {


public static void main(String[] args) {
// now creating Object of stud
Stud st1; // st1 is like container which is
used to store object reference the data
st1 =new Stud(); // we used "new"
keyword to create object memory
st1.StudentName= " navu "; // if we
put st1. then we are able to get all stud lass function like st1.city

st1.StudentId=123;
st1.StudentCity="raj";
st1.study();
st1.ShowFullDetails();
System.out.println("*******************************");
Stud st2= new Stud(12); //overloading //with
the help of //object(like
st1,st2,st3...) we can create many stud
//information

st2.StudentName=" khushi ";


st2.StudentId = 112;
st2.StudentCity = " MHA ";
st2.study();
st2.ShowFullDetails();

}
___________________________________________________________________________________
________

4) package com.oops;

public class Stud {

int StudentId;
String StudentName;
String StudentCity;
public Stud()
{
System.out.println("creating object");
}
public Stud(int st1){
System.out.println("parameterize const");
}
public Stud(int i,String n, String C){ // passing parameter
StudentId=i;
StudentName=n;
StudentCity=C;
}

public void study(){


System.out.println(StudentName+ "is studying");

}
public void ShowFullDetails()
{
System.out.println("My name is " +StudentName);
System.out.println("MY ID IS" +StudentId);
System.out.println("MY CITY IS" +StudentCity);
}

}
___________________________________________________________________________
package com.oops;

public class OBJECTDEMO {


public static void main(String[] args) {

Stud st1;
st1 =new Stud( 74 ,"RAJ","MUMBAI");

st1.study();
st1.ShowFullDetails();}}
___________________________________________________________________________________
____
**********************************************************************************
_______________________________________________________________________________----
--

5) public class MOM {


String PENname;
int PENprice;
String Pencolor;
public MOM(){
System.out.println("print pen details");

}
public MOM(String n,int p, String c){
PENname=n;
PENprice=p;
Pencolor=c;

}
public void showdetails()
{
System.out.println("my pen name is"+PENname);
System.out.println("my pen PRICE"+PENprice);
System.out.println("MY PEN COLOR"+Pencolor);
}

//(above n below code in link each other)


###################################################################################
#########
public class object {
public static void main(String[] args) {
MOM m1=new MOM("cello",74,"red");
m1.showdetails();

___________________________________________________________________________________
______________

6) /* METHOD overloading:
having multiple method in same class with same name
num of argument different
types of argument different
order of argument may differnt
*/***************************************************************************

public class MOM {


String PENname;
int PENprice;
String Pencolor;
public MOM(){
System.out.println("print pen details");

}
public MOM(String n,int p, String c){
PENname=n;
PENprice=p;
Pencolor=c;

}
public void showdetails()
{
System.out.println("my pen name is"+PENname);
System.out.println("my pen PRICE"+PENprice);
System.out.println("MY PEN COLOR"+Pencolor);
}
public void showdetails(float f){

}
}

__________________________________________________________________________________
public class object {
public static void main(String[] args) {
MOM m1=new MOM("cello",74,"red");
m1.showdetails();
}

}
___________________________________________________________________________________
_____
7)
/* this keyword refer current invoking object
used to calling current class constructor from in side different constructor

*/

public class THISKEYWORD {


int x;
public THISKEYWORD(int y){
x=y;
}

public void show(){


System.out.println(x);
}
}

*****************************************************************************
public class THISOBJ {
public static void main(String[] args) {
THISKEYWORD T= new THISKEYWORD(10);
T.show();
}
}
**************************************************************************
8)
public class THISKEYWORD {
int x; // instance variable
public THISKEYWORD(int x){ // local variable
x=x; // run out will b 0 not define becoz it consider local variable n instance
are same
this.x =x; // with help of this we can defin both x are hv diff function
nnow we get 10 which we pass

public void show(){


System.out.println(x);
}
}
******************************************************************************
public class THISOBJ {
public static void main(String[] args) {
THISKEYWORD T= new THISKEYWORD(10);
T.show();
}
}
___________________________________________________________________________
9) inherit

/* this is a mechanism in java by which one class is allow


to the feature of another class
denote in extend

means suppose dog is child class also called subclass ,dervied class, who work to
take function(like eat ,color) from parents class
Animal is parent class here work to give function is also called Super class and
base class.

child can make his properties /function and also take from animal i.e. from parents
class
*/
***************************88888888888888888********************************
package com.inherit;

public class ANIMALPARENT {


String COLOR = "BLUE";
{
System.out.println("my color is blue");
}
public void eating(){
System.out.println("i love to eat");
}
}

___________________________________________________________________________
package com.inherit;

public class Dogchild extends ANIMALPARENT{

public void display()


{
System.out.println("i m dog name sheru");
}

public static void main(String[] args) {


Dogchild D=new Dogchild();
D.display();
D.eating();
}
}
___________________________________________________________________________________
___________

10)
**************************************

and
*********************************
package SingleInherit;

public class B extends A{


public void methodB()
{
System.out.println("B class method");
}

public static void main(String[] args) {


B obj= new B();
obj.methodA(); // creating super class metod
obj.methodB(); // calling local class
}
}

___________________________________________________________________________________
public class C extends B{
public void methodC()
{
System.out.println("multilevel");
}

public static void main(String[] args) {


C ob=new C();
ob.methodA();
ob.methodB();
ob.methodC();

}
}
__________________________________________________________________________________-
11)

You might also like