Programmer Coding

OOP’s in java

Object Oriented Programming is an ideal that gives a lot of concepts such as

inheritance, polymorphism, etc.

 

The programming idea where everything is circled as an object is known as an object-oriented programming language.

small-scale is informed as the first object-oriented programming language.

 

OOPs (Object Oriented Programming System)

 

Object means a real word things such as a pen, chair, table, etc. Object-Oriented Programming is an approach to drawing a program using classes and objects. It makes the software grow and nurture by giving some concepts:

 

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Conceptualism
  • Encapsulation

 

Object:-

 

Anything that has a state and way(method) is known as an object.

For example:-

                       chair, pen, table, keyboard, bike, etc.

                       It can be daily and natural.

 

Class:-

 

  • A set of objects is called
  • It is a natural

 

Inheritance:-

 

When one object gets all the resources and way(method)  of the root of the object i.e. known as inheritance, it gives code use a lot of time. It is used to gain runtime polymorphism.

 

Polymorphism

 

 

When one task is pulled off in different way(method) i.e. known as polymorphism.

 

 

we use way(method) overloading and way(method)  overriding to gain polymorphism.

Conceptualism

Hiding inner details and showing convenience is known as conceptual

 

we use conceptual class and borderline to gain conceptualion.

 

Encapsulation

 

Binding code and data together into a single unit is known as encapsulation

 

A java class is the example of encapsulation. Java bean is the full of encapsulated class because all the data sets are private here.

 

Benefits of Inheritance

 

  • One of the key benefits of inheritance is to minimize the amount of duplicate code in an application by sharing frequentcode amongst some subclasses. Where equivalent code exists in two related classes, the ranking can usually be modified to move the frequentcode up to a mutual base class. This also tends to result in a best organization of code and smaller, compilation
    • Inheritance can also make application code more changeability to change because classes that  inherit from a frequentbase class can be used inter changeably. If the return type of a way(method)  is base class
    • Reusability – facility to use public way(method) s of base class without revise the
    • Extensibility – extending the base class logic as per business logic of the derived

 

  • Data hiding – base class can decide to keep some data personal so that it cann’t be

alternate by the derived class.

 

Inheritance in Java

Inheritance in java is a tool in which one object get all the resources and way(method) ss of root of  object. Inheritance represents the IS-A correlation, also known as root of –

child correlation.

 

Why use inheritance in java
  • For Way(method) Overriding (so runtime polymorphism can be gaind).
  • For Code

 

Syntax of Java Inheritance
  1. class Subclass-name expandbase class-name 2. {
  2. //way(method) s and fields 4. }

 

The expand keyword indicates that you are making a new class that come  from an existing class. The meaning of “expand(add)” is to increase the convenience.

 

 

class programmer1{

float salary=40000;

}

class Programmer expand programmer1{

int bonus=10000;

public static void main(String args[]){ Programmer p=new Programmer();

System.out.println(“Programmer salary is:”+p.salary); System.out.println(“Bonus of Programmer is:”+p.bonus);

} }

Programmer salary is:40000.0

 

Bonus of programmer is:10000

 

Types of inheritance in java

 

Single Inheritance Example

 

File: programmercoding.java

 

class Animal{

void eat(){System.out.println(“program…”);}

}

class Dog expand Animal{

void bark(){System.out.println(“barking…”);}

}

class programmercoding {

public static void main(String args[]){ Dog d=new Dog();

d.bark();

d.eat();

}}

Output:

barking… program…

 

Multilevel Inheritance Example

 

File: programmercoding1.java

 

class Animal{

void eat(){System.out.println(“program…”);}

}

class Dog expand Animal{

void bark(){System.out.println(“barking…”);}

}

class BabyDog expand Dog{

void weep(){System.out.println(“codeing…”);}

}

class programmercoding1{

 

public static void main(String args[]){ BabyDog d=new BabyDog(); d.weep();

d.bark();

d.eat();

}}

 

Output:

codeing…

barking…

program…

 

Hierarchical Inheritance Example

File: programmercoding2.java

 

class Animal{

void eat(){System.out.println(“program…”);}

}

class Dog expand Animal{

void bark(){System.out.println(“barking…”);}

}

class Cat expand Animal{

void meow(){System.out.println(“coding1…”);}

}

class TestInheritance3{

public static void main(String args[]){ Cat c=new Cat();

c.meow();

c.eat();

//c.bark();//C.T.Error

}}

 

Output:

Coding1…

program…

 

Member access and Inheritance

 

A Subclass(class in side class) includes all of the members of its super class but it cannot access those members of the super class that have been declared as private(personal). Attempt to access a private(personal) variable would cause compilation error as it causes access violation. The variables declared as private(personal), is only  accessible by other members of its own class. Subclass(class in side class) have no access to it.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top