0% found this document useful (0 votes)
2 views24 pages

Core Java - 3

The document provides an overview of Core Java concepts, focusing on Object-Oriented Programming (OOP) principles such as classes, objects, inheritance, polymorphism, encapsulation, and abstraction. It explains Java methods, their types, method overloading, and constructors, including default, parameterized, and copy constructors. Additionally, it covers inheritance types and the use of the 'extends' keyword in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

Core Java - 3

The document provides an overview of Core Java concepts, focusing on Object-Oriented Programming (OOP) principles such as classes, objects, inheritance, polymorphism, encapsulation, and abstraction. It explains Java methods, their types, method overloading, and constructors, including default, parameterized, and copy constructors. Additionally, it covers inheritance types and the use of the 'extends' keyword in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Core Java

By Dinesh Kumar Kushwaha


OOPs
• Class
• Object
• Inheritance
• Polymorphism
• Encapsulation
• Abstraction
Java class Methods
• A method is a block of code which only runs when it is called.
• We can pass data , known as parameters into a method.
• Methods are used to perform certain actions and they are also known as
functions.
Why use methods ?-: To reuse code : define the code once , and use it
many times.
Create a method
<return type> function_Name(arguments)
{
------------
------------
}
Simple Example
class Main
{
static void myMethod()
{
[Link](“I just got executed”);
}
//inside main() , call myMethod()
public static void main(String args[])
{
myMethod();
}
}
Static vs Non-Static
• Note-: Java programs that have either static or public attributes and methods.
• We create a static method , which means that it can be accessed without creating an object of
the class , unlike public, which can only be accessed by objects.
Exaample
class Main{
static void myStaticMethod() //static method
{
[Link]("I am Static method");
}
public void myPublicMethod() //public method
{
[Link]("I public method");
}
public static void main(String[] args)
{
myStaticMethod();//call without object
Main m1=new Main();
[Link]();//call with object
}}
Using Multiple Classes
//save [Link] //[Link]
public class Main1
class Second1
{
{
public void carName()
public static void main(String
{ args[])
[Link]("BMW"); {
} Main1 m1=new Main1();
public void speed(int maxspeed) [Link]();
{ [Link](250);
[Link]("Max Speed is:"+maxspeed); }
} }
} C:\>javac [Link]
C:\>javac [Link]
Create user define function
• [Link] argument no return.
• [Link] but no return
• [Link] argument but return
• [Link] with return
NANR
This type of function can not take any argument from the call & could argument not
return any value to the function call.
class Calc
{
void sum()
{
int n1=90,n2=78;
int s=n1+n2;
[Link](“sum is”+s);
}
public static void main(String args[])
{
Calc c1=new Calc();
[Link]();
}
}
Argument but no return
This type of function take argument from the function call but could not return any value
to the function call.

class Calc
{
void sum(int n1,int n2)
{
int s=n1+n2;
[Link](“sum is”+s);
}
public static void main(String args[])
{
Calc c1=new Calc();
[Link](10,89);
}
}
NA but R
• It could not take any argument but return value to the function call.
In this type return type are that which type of value are returned by function.

class Calc
{
int sum()
{
int n1=100,n2=200;
int s=n1+n2;
return s;
}
public static void main(String args[])
{
Calc c1=new Calc();
int r=[Link]();
[Link](“Sum is=“+r);
}
}
A with R
It takes argument from the function call and after processing return the result to function call.
class Calc
{
int sum(int n1,int n2)
{
int s=n1+n2;
return s;
}
public static void main(String args[])
{
Calc c1=new Calc();
int r=[Link](100,200);
[Link](“Sum is=“+r);
}
}
Method Overloading
• Method overloading means creating same name method with different
signature.
• These signature differentiate functions are-
[Link] of argument
[Link] of argument
[Link] of argument
[Link] of arguments
class Myclass{
void sum(int n1,int n2)
{
int c=n1+n2;
[Link]("Sum is="+c);
}
void sum(int n1,int n2,int n3)
{
int c=n1+n2+n3;
[Link]("Sum is="+c);
}
public static void main(String args[]){
Myclass m=new Myclass();
[Link](20,80);
[Link](1,2,3);
}
}
[Link] of argument
class Myclass{
void sum(int n1,int n2)
{
int c=n1+n2;
[Link]("Sum is="+c);
}
void sum(String s1,String s2)
{
String str=s1+” “+s2;
[Link]("Sum is="+str);
}
public static void main(String args[]){
Myclass m=new Myclass();
[Link](20,80);
[Link](“UNITED”,”Prayagraj”);
}}
[Link] of argument
class Myclass{
void sum(int n1,float n2)
{
[Link]("Sum is="+(n1+n2));
}
void sum(float n1,int n2)
{
[Link]("Sum is="+(n1+n2));
}
public static void main(String args[]){
Myclass m=new Myclass();
[Link](20,8.5f);
[Link](4.14f,200);
}
}
Constructor in java
• Constructor is a special type of method which is used to initialize the class
member(object).
Rules_:
1. Constructor name and class name must be same.
2. A Constructor must have no explicit return type.
3. Constructor get automatically calls whenever object is created of that class.
4. The only modifiers applicable for constructor are public ,private, protected, default.
Types of constructor
[Link]
[Link]
[Link]
[Link]-Parameterized
[Link]
Syntax
class <class_Name> Example-:
class demo
{ {

class_Name()
{
--------------------
--------------------
}
}
Default constructor
• A constructor which does not have any parameter is called default
constructor.
Syntax: Note-:When we can not create any
class A constructor in a program compiler
automatically create a constructor is known as
{ default constructor.
A(){ class A
{
--------- int a;String s;
--------- void show()
} {
[Link](a+” “+s);
Note-: }
public static void main(String args[]))
Class A {
Class A { A a1=new A();
Compiler
{ A() [Link]()
{ }}
} } Output-: 0 null
}
Example
class A
{
int a;String s,Float f;
A()
{
a=101;
s=“D K Kushwaha”;
f=3.12f;
}
void show()
{
[Link](a+” “+s+” “+f);
}}
class B
{
public static void main(String args[])
{
A a1=new A();
[Link]();
}}
Parameterized Constructor
• A constructor through which we can pass one or parameters is called parameterized constructor.
class A
{
int x,y;
A(int a,int b)
{
x=a;
y=b;
}
void show()
{
[Link](x+" "+y);
}
public static void main(String args[]){
A a1=new A(100,200);
[Link]();
}
}
Copy Constructor

• Whenever we pass object reference to the constructor then it is called


copy constructor.
Syntax:
class class_name
{
class_name(object ref)
{
}
}
Example of copy constructor
class A{
int a;String s;
A()
{
a=100;s="D.K Kushwaha";
[Link](a+" "+s);
}
A(A ref)
{
a=ref.a;
s=ref.s;
[Link](a+" "+s);
}
public static void main(String args[]){
A r1=new A();
A r2=new A(r1);
}
}
Constructor overloading
class A{
A(int a)
{
[Link]("value is="+a);
}
A(int a,String s)
{
[Link](a+" "+s);
}}
class B
{
public static void main(String args[])
{
A a1=new A(100);
A a2=new A(200,"Dinesh");
}}
Inheritance in java
• It means transferring properties from one class to another in java ,
properties of one class can be transfer to another class is known as
inheritance.
• Extends keywords= is used to perform inheritance in java.
Types of inheritance
• [Link] Inheritance
• [Link] Inheritance
• [Link] Inheritance
• [Link]
• 5. Hybrid Inheritance

You might also like