Escolar Documentos
Profissional Documentos
Cultura Documentos
classes ........................................................................................................................................................................ 2
An object can be initialize by three ways in the class: ........................................................................................... 2
A class that Initialize Object, by using method .................................................................................................. 2
2. A class that Initialize Object, by reference variable ..................................................................................... 3
A class that Initialize Object, by using constructors. ........................................................................................ 4
Single Inheritance .................................................................................................................................................. 5
Multilevel Inheritance ............................................................................................................................................ 5
Hierarchal inheritance ........................................................................................................................................... 7
Poly morphism (many behaviors) .............................................................................................................................. 8
Compile Time Poly morphism ( Method overloading) ........................................................................................... 8
Run time Poly morphism ( Method overriding) ..................................................................................................... 8
This Keyword .............................................................................................................................................................. 9
Using of This keyword: ............................................................................................................................................... 9
this keyword is used to invoke current class instance variable. ........................................................................... 9
this keyword is used to invoke current class method (implicit). ........................................................................ 10
this() keyword is used to invoke current class constructor. ............................................................................... 10
this keyword can be used to pass as an argument in the method call. ............................................................... 11
this keyword can be used to pass as an argument in the constructor call .......................................................... 11
this can be used to return the current class instance from the method ............................................................. 12
Super keyword ......................................................................................................................................................... 12
Super keyword is a reference variable that refer to the immediate parent class instance variable. ................. 12
Super keyword can be used to invoke immediate parent class method. ............................................................ 13
Super() can be used to invoke immediate parent class constructor. .................................................................. 14
Abstraction ............................................................................................................................................................... 14
Interfaces ................................................................................................................................................................. 17
Defference between Abstract class and interfaces. ................................................................................................ 19
Encapsulation/(secure) ............................................................................................................................................ 19
Package .................................................................................................................................................................... 21
Example of Accessing or importing User Defined Package in Java Programming ................................................... 21
classes
An object can be initialize by three ways in the class:
A class that Initialize Object, by using method
class Animal{
public void eat(){
System.out.println("animal eating");
}
public void run(){
System.out.println("animal running");
}
public void see(){
System.out.println("animal looking");
}
public void smelling(){
System.out.println("animal smelling");
}
class Birds{
public void eat(){
System.out.println("Birds eating");
}
public void run(){
System.out.println("Birds running");
}
public void see(){
System.out.println("Birds looking");
}
public void smelling(){
System.out.println("Birds smelling");
}
}
Types of Inheritance
Single Inheritance
class A
{
void showA()
{
System.out.println("a class method");
}
}
class B extends A
{
void showB()
{
System.out.println("b class method");
}
A obj1=new A();
obj1.showA();
//obj2.showB(); Error
B obj2=new B();
obj2.showA();
obj2.showB();
}
}
Multilevel Inheritance
class A
{
void showA()
{
System.out.println("a class method");
}
}
class B extends A
{
void showB()
{
System.out.println("b class method");
}
}
class C extends B
{
void showC()
{
System.out.println("c class method");
}
public static void main(String[] args) {
A obj1=new A();
obj1.showA();
//obj2.showB(); Error
B obj2=new B();
obj2.showA();
obj2.showB();
//obj2.showC(); Error
C obj3=new C();
obj3.showA();
obj3.showB();
obj3.showC();
}
}
Hierarchal inheritance
class A
{
void showA()
{
System.out.println("a class method");
}
}
class B extends A
{
void showB()
{
System.out.println("b class method");
}
}
class C extends A
{
void showC()
{
System.out.println("c class method");
}
public static void main(String[] args) {
A obj1=new A();
obj1.showA();
//obj2.showB(); Error
B obj2=new B();
obj2.showA();
obj2.showB();
//obj2.showC(); Error
C obj3=new C();
obj3.showA();
// obj3.showB(); ERROR
obj3.showC();
}
}
Poly morphism (many behaviors)
class A{
public void show()
{
}
public void show(int i)
{
}
public void show(float j)
{
}
public static void main (String[] args) {
A obj=new A();
obj.show(1f);
}
}
i. class A{
public void show()
{
}
}
class B extends A {
ii. class A{
public void show()
{
}
}
class B extends A {
This Keyword
class test{
int i; //here i is instance variable and it is for object.
void setvalues(int i){ // Here i is local variable
this.i=i; //this refer to current class instance variable
}
void show(){
System.out.print(i);
}
}
public class Main
{
public static void main(String args[]){
test t= new test();
t.setvalues(10); // 10 is pass to local variable i
t.show();} // it display 10.
}
class test{
void display(){
System.out.print("Hello");
}
void show(){
this.display(); //If you don’t use the this keyword, compiler automatically adds this keyword while invoking the method.
}
}
class test{
test(){
this(10); // it will display parameterized constructor.
System.out.println("no argument constructor");
}
test(int a){
this(); // it will display no argument constructor.
System.out.println("parameterized constructor");
}
}
class test{
void m1(test op){
System.out.print("I am in m1");
}
void m2(test oasdfp){
System.out.print("I am in m2");
}
void m3(){
m1(this); // this keyword can be used to pass as an argument in the method call.
m2(this):
}
}
class test{
test(demo td){
System.out.print("test class constructor");
}
}
class demo{
void m1(){
test t= new test(this); // t object is create of test class, in method of demo class, not a main class due to static error
}
}
public class Main
{
public static void main(String args[]){
demo t= new demo();
t.m1();
}
}
this can be used to return the current class instance from the method.
class demo{
demo m1(){
return this;
}
}
Super keyword
Using of Super keyword
Super keyword is a reference variable that refer to the immediate parent class instance variable.
class A{
int a=10;
}
class B extends A
{
int a=20;
void show(int a){
System.out.println(a); // 5
System.out.println(this.a); //20, this keyword is reference variable refer to current class instance
var.
System.out.println(super.a); //10, super keyword is reference variable refer to parent class variable.
}
}
public class Main{
public static void main (String[] args) {
B obj= new B();
obj.show(5);
}
}
class A{
void m1(){
System.out.print("i am in M1");
}
}
class B extends A
{
void m1(){
System.out.println(" i am in class b m1");
}
void m2()
{
m1(); // simply invoke current class metod.
super.m1(); // invoke parent class method.
}
}
public class Main{
public static void main (String[] args) {
B obj= new B();
obj.m2();
}
}
class A{
A(){
System.out.print("i am in class A");
}
}
class B extends A
{
B(){
super(); // if you don not use super() then compiler take it automatically.
System.out.println(" i am in class B");
}
}
public class Main{
public static void main (String[] args) {
B obj= new B();
}
}
Abstraction
o/p : error
reason : the body of method should be compulsory or it should be abstract method.
Means,
ii.
class vehicle
{
abstract void start();
}
o/p : error
reason: if the class consist of abstract method, then compulsory should be abstract
means,
abstract class A{
abstract void g(); // abstract method.
void f() // concrete method
{
System.out.print("hello");
}
}
public class m{
public static void main (String args[]){
}
}
interface it{
public abstract void show(); // public abstract by default compiler take if you don’t write it.
public static final int a=10; // public static final is by default in variable otherwise error.
default void display() //concrete method.
{
System.out.print("Hello");
}
public static void num(){ // you can use static in interface.
System.out.print("world");
}
}
public class m{
public static void main (String args[]){
}
}
2.
interface it{
void show();
}
interface cs{
void display();
}
class msc implements it,cs // interface supports multiple inheritance.
{
public void show(){
System.out.print("welcome it");
}
public void display(){
System.out.print("welcome cs");
}
}
public class m{
public static void main (String args[]){
msc obj=new msc();
obj.show();
obj.display();
}
}
Encapsulation/(secure)
Encapsulation in java is a mechanism of wrapping the data ( variables) and code acting on the data (
methods) together as a single unit. In encapsulation, the variables of a class will e hiddn from other
classes, and can be accessed only through the methods of their current class. The concept is known as
data hiding.
class employee
{
private int emp_id; // Declare the variables of a class as private. (data hiding)
public void setEmp_id( int emp_id1)
{
emp_id=emp_id1; // setter
}
public int getEmpId()
{
return emp_id; // getter
}
}
public class m{
public static void main (String args[]){
}
}
Simple
class employee{
int emp_id;
}
public class m{
public static void main (String args[]){
employee i= new employee();
i.emp_id=100;
System.out.print(i.emp_id);
}
}
class employee
{
private int emp_id; // data hiding
public void setEmp_id( int emp_id1)
{
emp_id=emp_id1; // data setting or saving
}
public int getEmpId()
{
return emp_id; // Data getting way
}
}
public class m{
public static void main (String args[]){
employee e=new employee();
e.setEmp_id(100);
// System.out.print(e.setEmpId()); // if this code, then error
System.out.print(e.getEmpId()); // this will display the value
}
}
Package
A package is a collection of related classes and interfaces providing access protection and namespace
management ( same class but different package).
Types of package
Built-in Package
-java.util.Scanner
User-defined Package
-package mypack