Você está na página 1de 21

Table of Contents

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");
}
}

public static void main(String[] args) {


System.out.println("main program starting");
Animal a =new Animal();
a.eat();
a.run();
a.see();
a.smelling();
Birds b=new Birds();
b.eat();
b.run();
b.see();
b.smelling();
}
}

2. A class that Initialize Object, by reference variable


class Birds{
String eats,runs,sees,smellings;
}
class Animal{
String eat,run,see,smelling;

public static void main(String[] args) {


System.out.println("main program starting");
Animal a =new Animal();
a.eat="animal eating";
a.run="animal running";
a.see="animal looking";
a.smelling="animal smelling";
System.out.println(a.eat);
System.out.println(a.run);
System.out.println(a.see);
System.out.println(a.smelling);
Birds b=new Birds();
b.eats="Birds eating";
b.runs="Birds running";
b.sees="Birds looking";
b.smellings="Birds smelling";
System.out.println(b.eats);
System.out.println(b.runs);
System.out.println(b.sees);
System.out.println(b.smellings);
}
}

A class that Initialize Object, by using constructors.


class animal{
String eats , runs, sees, smellings;
animal(String eats ,String runs, String sees, String smellings){
this.eats=eats;
this.runs=runs;
this.sees=sees;
this.smellings=smellings;
}
public static void main (String[] args) {
animal a=new animal("animal eating","animal running","animal looking","animal smelling");
Birds b=new Birds("Birds eating","Birds running","Birds looking","Birds smelling");
System.out.println(a.eats);
System.out.println(a.runs);
System.out.println(a.sees);
System.out.println(a.smellings);
System.out.println(b.eat);
System.out.println(b.run);
System.out.println(b.see);
System.out.println(b.smelling);
}
}
class Birds{
String eat , run, see, smelling;

Birds (String eat ,String run, String see, String smelling){


this.eat=eat;
this.run=run;
this.see=see;
this.smelling=smelling;
}
}
Inheritance

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");
}

public static void main(String[] args) {

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)

Compile Time Poly morphism ( Method overloading)

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);
}
}

Run time Poly morphism ( Method overriding)

i. class A{
public void show()
{
}
}
class B extends A {

public static void main (String[] args){


B obj=new B();
obj.show();
}
}

ii. class A{
public void show()
{
}
}
class B extends A {

public void show()


{
}

public static void main (String[] args){


B obj=new B();
obj.show();
}
}

This Keyword

Using of This keyword:

this keyword is used to invoke current class instance variable.

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.
}

this keyword is used to invoke current class method (implicit).

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.
}
}

public class Main


{
public static void main(String args[]){
test t= new test();
t.show();
}
}

this() keyword is used to invoke current class constructor.

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");
}
}

public class Main


{
public static void main(String args[]){
test t= new test();
}
}

this keyword can be used to pass as an argument in the method call.

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):
}
}

public class Main


{

public static void main(String args[]){


test t= new test();
t.m2();
}
}

this keyword can be used to pass as an argument in the constructor call .

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;
}
}

public class Main


{
public static void main(String args[]){
demo t= new demo();
t.m1();
}
}

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);
}
}

Super keyword can be used to invoke immediate parent class method.

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();
}
}

Super() can be used to invoke immediate parent class constructor.

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

Main Points That Should be Remember

1. A method without body ( no implementation) is known as abstract method.


2. A method with body is known as concrete method.
3. A method must always be declared in an abstract class, or we can say that if a class has an abstract
method, it should be declared abstract as well.
4. If a regular class extends an abstract class, then the the class must have to implement all the abstract
methods of abstract parent class or it has to be declared abstract as well.
5. Abstract methods in an abstract class are meant to be overridden in derived concrete classes otherwise
compile time error.
6. Abstract classes cannot be instantiated, means we can’t create an object of abstract class.

Now, we clear all point from following examples.


i.
class vehicle
{
void start();
}

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 vehicle


{
abstract void start();
}
o/p : processed.

Abstract class have abstract method and as well as concrete method.

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[]){
}
}

A simple program of abstract.

abstract class vehicle // External view


{
abstract void start();
}
class cars extends vehicle // Internal view , you can say overridden concept is here.
{
void start()
{
System.out.println("cars start with key ");
}
}
class bike extends vehicle // similarly, etc.
{
void start(){
System.out.println("bike start with key and kick");
}
}
public class m{
public static void main (String args[]){
cars st= new cars(); // here you cannot create abstract class object due to abstract method.
st.start();
bike bk=new bike();
bk.start();
}
}
Interfaces
Interface is a mechanism to achieve abstraction in java.
Interace is similar to abstract class but hav9ng all the methods of abstract type i.e. it cannot have
amethod body.
Since java 8, we can have default and static methods in an interface.
Since java 9, we can have private methods in an interface.
Why we use interface?
It is used to achieve abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling. ( once change at one place does’nt effect the whole places.)
Syntax
interfac InterfaceName
{
methods (public abstract or concrete methods)
fields (public , static, final)
}

Now, you will clear all points in this example:

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[]){
}
}

A simple program of interface.


1.
interface it{
public abstract void show(); // public is only supportable in abstract method.
}
class msc implements it{
public void show(){ // there is must be public in definition Of abstract method.
System.out.print("welcome");
}
}
public class m{
public static void main (String args[]){
msc obj=new msc(); // here, we can’t create interface object.
obj.show();
}
}

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();
}
}

Defference between Abstract class and interfaces.


1. Abstract class can have instance methods that implements
1. aMethods of a java interface are implicitly
default behavior. abstract and cannot have implementations.
2. An abstract class may contain non-final variables. 2. Interface contains public, static and final
3. Methods and variables can have any access modifier i.e.variables only.
public, protected, default and private. 3. Methods and variable are always public
4. Java abstract class should be extended using keyword 4. Java interface should be implemented using
“extends”. keyword “implements”
5. An abstract class can extend another java class 5. andAn interface can extend another java interface
implement multiple java interfaces. only.

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.

Steps to achieve encapsulation.


1. Declare the variables of a class as private.
2. Provide public setter and getter methods to modify and view the variables values.

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[]){
}
}

Now, we do one program from different approaches ( simple and encapsulation/security).

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);
}
}

Another way by encapsulation,

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

Example of Accessing or importing User Defined Package in Java Programming

Você também pode gostar