Posts

Showing posts with the label oops

Can we override static method in java ?

No ! we cannot override the static methods. Since static method belongs to class level not object level. class StaticExe1 { public static void run() { System.out.println("run of StaticExe1"); } public  void fly() { System.out.println("fly of non-static Exe1"); } } class StaticExe2 extends StaticExe1 { public static void run() { System.out.println("run of StaticExe2"); } public  void fly() { System.out.println("fly of non-static Exe2"); } } class StaticExe3 extends StaticExe2 { public static void run() { System.out.println("run of StaticExe3"); } public  void fly() { System.out.println("fly of non-static Exe3"); } } public class StaticMethod { public static void main(String[] args) { StaticExe2 ex = new StaticExe3 (); ex.run(); ex.fly(); } }

What is the output of the program ?

class SampleExec1  { void run() { System.out.println("SampleExec1 run"); } } class SampleExec2 extends SampleExec1 { void run() { System.out.println("SampleExec2 run"); } } public class SampleExecution { public static void main(String[] args) { SampleExec2 ex = new SampleExec1(); ex.run(); } } Output : compile time error at line 15. Type mismatch: cannot convert from SampleExec1 to SampleExec2

Advantages of OOP

OOPs provides code reusability OOP provides a clear modular structure for programs OOP makes it easy to maintain and modify existing code Objects created for Object Oriented Programs can easily be reused in other programs Security(abstraction), modularity, sharing (Inheritance) High Cohesion(Peace of code apparently will tell what its actual work) Low Coupling (Depends on another module)

Inheritance in java

Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. Benefits of inheritance reuse of methods  reuse of fields of parent class  We can add new methods and fields Syntax public class className extends ParentclassName   {      //methods and fields   }  

Polymorphism in Java

Polymorphism (from the Greek meaning "having multiple forms") Polymorphism means one thing in many form. an object to have more than one form. Polymorphism is capability of one object to behave in multiple ways. There are following types of polymorphism : Static polymorphism (compile time) function overloading :-      Method Overloading is a feature that allows more than two methods having same name in a class with different parameters. Example: public class Calculator{     void add(int a,int b)   {   System.out.println(a+b);   }     void add(int a,int b,int c)   {   System.out.println(a+b+c);   }     

Data abstraction

Abstraction is the process is used to hide certain details and only show the essential features of the object. It is class level design that hiding the complexity of the implementation and offered by an API / design / system, in a sense simplifying the 'interface' to access the underlying implementation. Example: public class Employee {    private String name;    private String address;    private int sal;    public Employee(String name, String address, int sal)    {       this.name = name;       this.address = address;       this.sal = sal;    }

Java Encapsulation

Encapsulation        Encapsulation is a mechanism of binding the data and function into a single unit. Encapsulation essentially has both information hiding and implementation hiding. Encapsulation provides a way for abstraction. Example: class Encapsulation {     //Restrict direct access, hiding Data     private int a;     private int b;          public void setA(int a){      this.a=a;    }      public void setB(int b){         this.b=b;    }     //hiding implementation     public int sum(){         int total=a+b;         //  Other Implementation         return total;     } }

Java Class concept

Class        A class is a blueprint or prototype from which objects are created. Class that contains data type, Object, function methods, properties, Classes and constructor. Example:               For example our college, Our college contain different different classes and classes contain student. Programming Language Example: class MyClass{     String name= "";     String address= "";     int age= 0;     void setName(String name) {          this.name = name;     }     void setAddress(String address) {          this.address = address;     }     void setAge(int age) {          this.age = age;     }     void print() {          System.out.println(name +" "+Address+" "+age);     } }

Java Object Concept

 Object        Object is representative of the class and is responsible for memory allocation of its data members and member functions.An object is a real world entity having attributes (State / data type) and behaviors (functions). An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object behavior. Example:               In our class room, All students are an individual Object. They have different               name and color. Programming Language Example:  All are the Different different object int a=10; float b1=20.10f; String s1="Name"; MyClass x;

Object oriented programming concepts (OOPs)

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects". Re-usability is the main concept of Oops. There are few major principles of object-oriented paradigm. If any programming Language supports this principles that programming language called Object-oriented programming. Below are object oriented programming concepts : 1. Object        Read More about Object     http://theprogrammersfirst.blogspot.in/2016/07/java-object-concept.html